├── 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 ├── 11-print_to_98.c ├── 2-print_alphabet_x10.c ├── 3-islower.c ├── 4-isalpha.c ├── 5-sign.c ├── 6-abs.c ├── 7-print_last_digit.c ├── 8-24_hours.c ├── 9-times_table.c ├── README.md ├── _putchar.c └── 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 ├── _putchar.c └── main.h ├── 0x05-pointers_arrays_strings ├── 0-reset_to_98.c ├── 1-swap.c ├── 100-atoi.c ├── 101-keygen.c ├── 2-strlen.c ├── 3-puts.c ├── 4-print_rev.c ├── 5-rev_string.c ├── 6-puts2.c ├── 7-puts_half.c ├── 8-print_array.c ├── 9-strcpy.c ├── README.md ├── _putchar.c └── main.h ├── 0x06-pointers_arrays_strings ├── 0-strcat.c ├── 1-strncat.c ├── 100-rot13.c ├── 101-print_number.c ├── 102-magic.c ├── 103-infinite_add.c ├── 104-print_buffer.c ├── 2-strncpy.c ├── 3-strcmp.c ├── 4-rev_array.c ├── 5-string_toupper.c ├── 6-cap_string.c ├── 7-leet.c ├── README.md ├── _putchar.c └── main.h ├── 0x07-pointers_arrays_strings ├── 0-memset.c ├── 1-memcpy.c ├── 100-set_string.c ├── 101-crackme_password ├── 2-strchr.c ├── 3-strspn.c ├── 4-strpbrk.c ├── 5-strstr.c ├── 7-print_chessboard.c ├── 8-print_diagsums.c ├── README.md ├── _putchar.c └── main.h ├── 0x08-recursion ├── 0-puts_recursion.c ├── 1-print_rev_recursion.c ├── 100-is_palindrome.c ├── 101-wildcmp.c ├── 2-strlen_recursion.c ├── 3-factorial.c ├── 4-pow_recursion.c ├── 5-sqrt_recursion.c ├── 6-is_prime_number.c ├── README.md ├── _putchar.c └── main.h ├── 0x09-static_libraries ├── 0-isupper.c ├── 0-isupper.o ├── 0-memset.c ├── 0-memset.o ├── 0-strcat.c ├── 0-strcat.o ├── 1-isdigit.c ├── 1-isdigit.o ├── 1-memcpy.c ├── 1-memcpy.o ├── 1-strncat.c ├── 1-strncat.o ├── 100-atoi.c ├── 100-atoi.o ├── 2-strchr.c ├── 2-strchr.o ├── 2-strlen.c ├── 2-strlen.o ├── 2-strncpy.c ├── 2-strncpy.o ├── 3-islower.c ├── 3-islower.o ├── 3-puts.c ├── 3-puts.o ├── 3-strcmp.c ├── 3-strcmp.o ├── 3-strspn.c ├── 3-strspn.o ├── 4-isalpha.c ├── 4-isalpha.o ├── 4-strpbrk.c ├── 4-strpbrk.o ├── 5-strstr.c ├── 5-strstr.o ├── 6-abs.c ├── 6-abs.o ├── 9-strcpy.c ├── 9-strcpy.o ├── README.md ├── _putchar.c ├── _putchar.o ├── create_static_lib.sh ├── libmy.a └── main.h ├── 0x0A-argc_argv ├── 0-whatsmyname.c ├── 1-args.c ├── 100-change.c ├── 2-args.c ├── 3-mul.c ├── 4-add.c ├── README.md └── main.h ├── 0x0B-malloc_free ├── 0-create_array.c ├── 1-strdup.c ├── 100-argstostr.c ├── 101-strtow.c ├── 2-str_concat.c ├── 3-alloc_grid.c ├── 4-free_grid.c ├── README.md └── main.h ├── 0x0C-more_malloc_free ├── 0-malloc_checked.c ├── 0x0C-more_malloc_free ├── 1-string_nconcat.c ├── 100-realloc.c ├── 101-mul.c ├── 2-calloc.c ├── 3-array_range.c ├── README.md ├── _putchar.c ├── git.sh └── 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 └── main.h ├── 0x0E-structures_typedef ├── 1-init_dog.c ├── 2-print_dog.c ├── 4-new_dog.c ├── 5-free_dog.c ├── README.md └── dog.h ├── 0x0F-function_pointers ├── 0-print_name.c ├── 1-array_iterator.c ├── 100-main_opcodes.c ├── 2-int_index.c ├── 3-calc.h ├── 3-get_op_func.c ├── 3-main.c ├── 3-op_functions.c ├── README.md └── function_pointers.h ├── 0x10-variadic_functions ├── 0-sum_them_all.c ├── 1-print_numbers.c ├── 2-print_strings.c ├── 3-print_all.c ├── README.md └── variadic_functions.h ├── 0x12-singly_linked_lists ├── 0-print_list.c ├── 1-list_len.c ├── 100-first.c ├── 101-hello_holberton.asm ├── 2-add_node.c ├── 3-add_node_end.c ├── 4-free_list.c ├── README.md └── lists.h ├── 0x13-more_singly_linked_lists ├── 0-print_listint.c ├── 1-listint_len.c ├── 10-delete_nodeint.c ├── 100-reverse_listint.c ├── 101-print_listint_safe.c ├── 2-add_nodeint.c ├── 3-add_nodeint_end.c ├── 4-free_listint.c ├── 5-free_listint2.c ├── 6-pop_listint.c ├── 7-get_nodeint.c ├── 8-sum_listint.c ├── 9-insert_nodeint.c ├── README.md └── lists.h ├── 0x14-bit_manipulation ├── 0-binary_to_uint.c ├── 1-print_binary.c ├── 100-get_endianness.c ├── 101-password ├── 2-get_bit.c ├── 3-set_bit.c ├── 4-clear_bit.c ├── 5-flip_bits.c ├── README.md ├── _putchar.c └── main.h ├── 0x15-file_io ├── 0-read_textfile.c ├── 1-create_file.c ├── 100-elf_header.c ├── 2-append_text_to_file.c ├── 3-cp.c ├── README.md └── main.h ├── 0x17-doubly_linked_lists ├── 0-print_dlistint.c ├── 1-dlistint_len.c ├── 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-strncat.c ├── 100-atoi.c ├── 100-operations.so ├── 100-tests.py ├── 101-make_me_win.sh ├── 2-strchr.c ├── 2-strlen.c ├── 2-strncpy.c ├── README.md └── main.h ├── 0x1A-hash_tables ├── 0-hash_table_create.c ├── 1-djb2.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 ├── README.md ├── m.h ├── main.c └── school.c ├── 0x1E-search_algorithms ├── 0-linear.c ├── 1-binary.c ├── 100-jump.c ├── 101-O ├── 102-interpolation.c ├── 103-exponential.c ├── 104-advanced_binary.c ├── 105-jump_list.c ├── 106-linear_skip.c ├── 107-O ├── 108-O ├── 2-O ├── 3-O ├── 4-O ├── 5-O ├── 6-O ├── README.md └── search_algos.h └── README.md /0x00-hello_world/0-preprocessor: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -E $CFILE -o c 3 | -------------------------------------------------------------------------------- /0x00-hello_world/1-compiler: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -c $CFILE 3 | -------------------------------------------------------------------------------- /0x00-hello_world/100-intel: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -S -masm=intel $CFILE 3 | -------------------------------------------------------------------------------- /0x00-hello_world/101-quote.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * main - print quote 6 | * 7 | * Description: 'print dora's quote' 8 | * 9 | * Return: Always 1 (success) 10 | */ 11 | 12 | int main(void) 13 | { 14 | write(2, "and that piece of art is useful\" - Dora Korpar, 2015-10-19\n", 59); 15 | return (1); 16 | } 17 | -------------------------------------------------------------------------------- /0x00-hello_world/2-assembler: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -S $CFILE 3 | -------------------------------------------------------------------------------- /0x00-hello_world/3-name: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -c $CFILE -o cisfun 3 | -------------------------------------------------------------------------------- /0x00-hello_world/4-puts.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Entry point 5 | * 6 | * Description: 'main function is used as an entry point in all c programs' 7 | * 8 | * Return: Always 0 (Success) 9 | */ 10 | 11 | int main(void) 12 | { 13 | puts("\"Programming is like building a multilingual puzzle"); 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x00-hello_world/5-printf.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Entry point 5 | * 6 | * Description: 'main function is used as an entry point in all c programs' 7 | * 8 | * Return: Always 0 (Success) 9 | */ 10 | 11 | int main(void) 12 | { 13 | printf("with proper grammar, but the outcome is a piece of art,\n"); 14 | 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x00-hello_world/6-size.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Entry point 5 | * Return: Always 0 (Success) 6 | */ 7 | int main(void) 8 | { 9 | int a; 10 | long int b; 11 | long long int c; 12 | char d; 13 | float f; 14 | 15 | printf("Size of a char: %lu byte(s)\n", (unsigned long)sizeof(d)); 16 | printf("Size of an int: %lu byte(s)\n", (unsigned long)sizeof(a)); 17 | printf("Size of a long int: %lu byte(s)\n", (unsigned long)sizeof(b)); 18 | printf("Size of a long long int: %lu byte(s)\n", (unsigned long)sizeof(c)); 19 | printf("Size of a float: %lu byte(s)\n", (unsigned long)sizeof(f)); 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /0x00-hello_world/README.md: -------------------------------------------------------------------------------- 1 | [![Typing SVG](https://readme-typing-svg.herokuapp.com?font=Fira+Code&weight=600&size=25&pause=1000&color=00C7F7&width=435&lines=HELLO+WORLD+!!)](https://git.io/typing-svg) 2 | 3 | This is the C programming project 4 | 1. Write a script that runs a C file through the preprocessor and save the result into another file. 5 | 2. Write a script that compiles a C file but does not link. 6 | 3. Write a script that generates the assembly code of a C code and save it in an output file. 7 | 4. Write a script that compiles a C file and creates an executable named cisfun. 8 | 5. Write a C program that prints exactly "Programming is like building a multilingual puzzle, followed by a new line. 9 | 6. Write a C program that prints exactly with proper grammar, but the outcome is a piece of art,, followed by a new line. 10 | 7. Write a C program that prints the size of various types on the computer it is compiled and run on. 11 | 8. Write a script that generates the assembly code (Intel syntax) of a C code and save it in an output file. 12 | 9. Write a C program that prints exactly and that piece of art is useful" - Dora Korpar, 2015-10-19, followed by a new line, to the standard error. 13 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/0-positive_or_negative.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /** 6 | * main - 0-positive_or_negative.c 7 | * 8 | * Description: This is the code for random numbers generating 9 | * 10 | * Return: Always 0 (success) 11 | */ 12 | 13 | int main(void) 14 | { 15 | int n; 16 | 17 | srand(time(0)); 18 | n = rand() - RAND_MAX / 2; 19 | if (n > 0) 20 | { 21 | printf("%i is positive\n", n); 22 | } 23 | else if (n == 0) 24 | { 25 | printf("%i is zero\n", n); 26 | } 27 | else if (n < 0) 28 | { 29 | printf("%i is negative\n", n); 30 | } 31 | return (0); 32 | } 33 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/1-last_digit.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /** 6 | * main - entry point 7 | * 8 | * Description: srand generate random seed numbers 9 | * in the c program for the project 10 | * main is the entry point 11 | * 12 | * Return: Always 0 (success) 13 | * 14 | */ 15 | 16 | int main(void) 17 | { 18 | int n; 19 | char str[] = "Last digit of"; 20 | int lastDigit; 21 | 22 | srand(time(0)); 23 | n = rand() - RAND_MAX / 2; 24 | lastDigit = n % 10; 25 | if (lastDigit > 5) 26 | { 27 | printf("%s %d is %d and is greater than 5\n", str, n, lastDigit); 28 | } 29 | else if (lastDigit == 0) 30 | { 31 | printf("%s %d is %d and is 0\n", str, n, lastDigit); 32 | } 33 | else if (lastDigit < 6) 34 | { 35 | printf("%s %d is %d and is less than 6 and not 0\n", str, n, lastDigit); 36 | } 37 | return (0); 38 | } 39 | 40 | -------------------------------------------------------------------------------- /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 | #include 3 | #include 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Alway 0 (Success) 9 | */ 10 | int main(void) 11 | { 12 | char low; 13 | 14 | for (low = 'a'; low <= 'z'; low++) 15 | putchar(low); 16 | printf("\n"); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/3-print_alphabets.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /** 6 | * main - entry point 7 | * 8 | * Description: code that print object in lowercase and uppercase 9 | * 10 | * Return: Always 0 (success) 11 | * 12 | */ 13 | 14 | int main(void) 15 | { 16 | char low; 17 | char high; 18 | 19 | for (low = 'a'; low <= 'z'; low++) 20 | putchar(low); 21 | for (high = 'A'; high <= 'Z'; high++) 22 | putchar(high); 23 | printf("\n"); 24 | return (0); 25 | } 26 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/4-print_alphabt.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /** 6 | * main - entry point 7 | * 8 | * Description: Print all alphabet in lowercase except q and e 9 | * 10 | * Return: Always 0 (success) 11 | * 12 | */ 13 | 14 | int main(void) 15 | { 16 | char low, e, q; 17 | e = 'e'; 18 | q = 'q'; 19 | for (low = 'a'; low <= 'z'; low++) 20 | { 21 | if (low != e && low != q) 22 | putchar(low); 23 | } 24 | printf("\n"); 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/5-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /** 6 | * main - entry point 7 | * 8 | * Description: a program that prints all single 9 | * digit numbers of base 10 starting from 0 10 | * 11 | * Return: Always 0 (success) 12 | * 13 | */ 14 | 15 | int main(void) 16 | { 17 | int n; 18 | 19 | for (n = 0; n < 10; n++) 20 | putchar(n); 21 | putchar("\n"); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/6-print_numberz.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always 0 (Success) 9 | */ 10 | int main(void) 11 | { 12 | int d; 13 | 14 | for (d = '0'; d <= '9'; d++) 15 | putchar(d); 16 | putchar('\n'); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/7-print_tebahpla.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /** 6 | * main - entry point 7 | * 8 | * Description: a program that prints the lowercase alphabet in reverse 9 | * 10 | * Return: Always 0 (success) 11 | * 12 | */ 13 | 14 | int main(void) 15 | { 16 | char low; 17 | 18 | for (low = 'z'; low >= 'a'; low--) 19 | putchar(low); 20 | putchar('\n'); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/8-print_base16.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /** 6 | * main - entry point 7 | * 8 | * Description: a program that prints all the numbers of base 16 in lowercase 9 | * 10 | * Return: Always 0 (success) 11 | * 12 | */ 13 | 14 | int main(void) 15 | { 16 | int low; 17 | 18 | for (low = '0'; low <= '9'; low++) 19 | { 20 | putchar(low); 21 | } 22 | for (low = 'a'; low <= 'f'; low++) 23 | { 24 | putchar(low); 25 | } 26 | putchar('\n'); 27 | return (0); 28 | } 29 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/9-print_comb.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always 0 (Success) 9 | */ 10 | int main(void) 11 | { 12 | int d; 13 | 14 | for (d = '0'; d <= '9'; d++) 15 | { 16 | putchar(d); 17 | if (d != '9') 18 | { 19 | putchar(','); 20 | putchar(' '); 21 | } 22 | } 23 | putchar('\n'); 24 | return (0); 25 | } 26 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/README.md: -------------------------------------------------------------------------------- 1 | [![Typing SVG](https://readme-typing-svg.herokuapp.com?font=Fira+Code&weight=600&size=25&pause=1000&color=00C7F7&width=435&lines=VARIABLES_IF_ELSE_WHILE)](https://git.io/typing-svg) 2 | 3 | C PROGRAMMING 4 | Variables, if, else, while 5 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/0-putchar.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - Entry point. 5 | * Description: write a program that prints "_putchar" followed by a new line. 6 | * Return: Always 0 (Success.) 7 | */ 8 | 9 | int main(void) 10 | { 11 | _putchar('_'); 12 | _putchar('p'); 13 | _putchar('u'); 14 | _putchar('t'); 15 | _putchar('c'); 16 | _putchar('h'); 17 | _putchar('a'); 18 | _putchar('r'); 19 | _putchar('\n'); 20 | 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/1-alphabet.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_alphabet - Entry point 5 | * 6 | * Description: a function that prints the alphabet, in lowercase, 7 | * followed by a new line. 8 | * 9 | * Return: Always 0 (success) 10 | * 11 | */ 12 | 13 | void print_alphabet(void) 14 | { 15 | char parv = 'a'; 16 | 17 | while (parv <= 'z') 18 | { 19 | _putchar(parv); 20 | parv++; 21 | 22 | } 23 | _putchar('\n'); 24 | 25 | } 26 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/10-add.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * add - returns the sum of its parameters 5 | * 6 | * @a: int type number 7 | * 8 | * @b: int type number 9 | * 10 | * Return: Always 0 (success) 11 | * 12 | */ 13 | 14 | int add(int a, int b) 15 | { 16 | return (a + b); 17 | } 18 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/100-times_table.c: -------------------------------------------------------------------------------- 1 | i#include "main.h" 2 | 3 | /** 4 | * print_times_table - prints the times table 5 | * @n: integer for which the times table will be printed 6 | * 7 | * Description: prints the times table 8 | * 9 | * Return: void 10 | */ 11 | 12 | void print_times_table(int n) 13 | { 14 | int row, column, product; 15 | 16 | if (n >= 0 && n < 15) 17 | { 18 | for (row = 0; row <= n; row++) 19 | { 20 | for (column = 0; column <= n; column++) 21 | { 22 | product = row * column; 23 | 24 | if (column == 0) 25 | _putchar('0'); 26 | else if (product < 10) 27 | { 28 | _putchar(','); 29 | _putchar(' '); 30 | _putchar(' '); 31 | _putchar(' '); 32 | _putchar(product % 10 + '0'); 33 | } 34 | else if (product >= 10 && product < 100) 35 | { 36 | _putchar(','); 37 | _putchar(' '); 38 | _putchar(' '); 39 | _putchar((product / 10) % 10 + '0'); 40 | _putchar(product % 10 + '0'); 41 | } 42 | else if (product > 99 && product < 1000) 43 | { 44 | _putchar(','); 45 | _putchar(' '); 46 | _putchar(product / 100 + '0'); 47 | _putchar((product / 10) % 10 + '0'); 48 | _putchar(product % 10 + '0'); 49 | } 50 | } 51 | _putchar('\n'); 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/101-natural.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * main - main block 6 | * 7 | * Description: computes and prints the sum of all the multiples of 3 or 8 | * 5 below 1024 (excluded), followed by a new line 9 | * 10 | * Return: Always 0 (success) 11 | * 12 | */ 13 | 14 | int main(void) 15 | { 16 | int c = 0; 17 | int sum = 0; 18 | 19 | while (c < 1024) 20 | { 21 | if (c % 3 == 0 || c % 5 == 0) 22 | { 23 | sum += c; 24 | } 25 | 26 | c++; 27 | } 28 | printf("%i\n", sum); 29 | return (0); 30 | } 31 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/102-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - main block 5 | * 6 | * Description: computes and prints the sum of all the multiples of 3 or 7 | * 5 below 1024 (excluded), followed by a new line 8 | * 9 | * Return: Always 0 (success) 10 | * 11 | */ 12 | 13 | int main(void) 14 | { 15 | int i = 0; 16 | long int a = 0, b = 1, next; 17 | 18 | while (i < 50) 19 | { 20 | next = a + b; 21 | a = b; 22 | b = next; 23 | printf("%lu", next); 24 | 25 | if (i < 49) 26 | { 27 | printf(", "); 28 | } 29 | i++; 30 | } 31 | putchar('\n'); 32 | return (0); 33 | } 34 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/103-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - main block 5 | * 6 | * Description: computes and prints even number < 4,000,000 7 | * 5 below 1024 (excluded), followed by a new line 8 | * 9 | * Return: Always 0 (success) 10 | * 11 | */ 12 | 13 | int main(void) 14 | { 15 | int a = 0, b = 1, next = 0; 16 | int sum = 0; 17 | 18 | while (next < 4000000) 19 | { 20 | next = a + b; 21 | a = b; 22 | b = next; 23 | if (next % 2 == 0) 24 | sum += next; 25 | } 26 | printf("%i\n", sum); 27 | return (0); 28 | } 29 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/104-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - finds and prints the first 98 Fibonacci numbers, 5 | * starting with 1 and 2 6 | * followed by a new line 7 | * Return: ALways 0 (Success) 8 | */ 9 | int main(void) 10 | { 11 | unsigned long int i, j, k, j1, j2, k1, k2; 12 | 13 | j = 1; 14 | k = 2; 15 | 16 | printf("%lu", j); 17 | 18 | for (i = 1; i < 91; i++) 19 | { 20 | printf(", %lu", k); 21 | k = k + j; 22 | j = k - j; 23 | } 24 | 25 | j1 = j / 1000000000; 26 | j2 = j % 1000000000; 27 | k1 = k / 1000000000; 28 | k2 = k % 1000000000; 29 | 30 | for (i = 92; i < 99; ++i) 31 | { 32 | printf(", %lu", k1 + (k2 / 1000000000)); 33 | printf("%lu", k2 % 1000000000); 34 | k1 = k1 + j1; 35 | j1 = k1 - j1; 36 | k2 = k2 + j2; 37 | j2 = k2 - j2; 38 | } 39 | 40 | printf("\n"); 41 | 42 | return (0); 43 | } 44 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/11-print_to_98.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * print_to_98 - print to 98 6 | * 7 | * Description: function that prints all natural numbers from n to 98, 8 | * 9 | * @n : number to start from 10 | * 11 | * Return: Always 0 or 1 (success) 12 | * 13 | */ 14 | 15 | void print_to_98(int n) 16 | { 17 | while (n < 98) 18 | { 19 | printf("%i, ", n); 20 | n++; 21 | } 22 | while (n > 98) 23 | { 24 | printf("%i, ", n); 25 | n--; 26 | } 27 | printf("98"); 28 | putchar('\n'); 29 | } 30 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/2-print_alphabet_x10.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_alphabet_x10 - Entry point 5 | * 6 | * Description: a function that prints 10 times the alphabet, in lowercase, 7 | * followed by a new line. 8 | * 9 | * Return: Always 0 (success) 10 | * 11 | */ 12 | 13 | void print_alphabet_x10(void) 14 | { 15 | char p; 16 | int i = 0; 17 | 18 | while (i < 10) 19 | { 20 | p = 'a'; 21 | 22 | while (p <= 'z') 23 | { 24 | _putchar(p); 25 | p++; 26 | } 27 | _putchar('\n'); 28 | i++; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/3-islower.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _islower - checks for a lower case character 5 | * 6 | * Description: a function that checks for lowercase character. 7 | * @c : character to check the case 8 | * 9 | * Return: Always 0 or 1 (success) 10 | * 11 | */ 12 | 13 | int _islower(int c) 14 | { 15 | return (c >= 97 && c <= 122); 16 | } 17 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/4-isalpha.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isalpha - checks for alphabetic characters 5 | * 6 | * Description: a function that checks for alphabetic character. 7 | * 8 | * @c : character to check the case 9 | * 10 | * Return: Always 0 or 1 (success) 11 | * 12 | */ 13 | 14 | int _isalpha(int c) 15 | { 16 | return ((c >= 97 && c <= 122) || (c >= 65 && c <= 90)); 17 | } 18 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/5-sign.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_sign - prints the sign of a number 5 | * 6 | * Description: a function that prints the sign of a number. 7 | * 8 | * @n : number to check 9 | * 10 | * Return: Always 0 or 1 (success) 11 | * 12 | */ 13 | 14 | int print_sign(int n) 15 | { 16 | int parv; 17 | 18 | if (n > 0) 19 | { 20 | parv = 1; 21 | _putchar ('+'); 22 | } 23 | else if (n == 0) 24 | { 25 | parv = 0; 26 | _putchar ('0'); 27 | } 28 | else 29 | { 30 | parv = -1; 31 | _putchar ('-'); 32 | } 33 | return (parv); 34 | } 35 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/6-abs.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _abs - Checks for the absolute value 5 | * 6 | * Description: a function that computes the absolute value of an integer. 7 | * 8 | * @n : number to check 9 | * 10 | * Return: Always 0 or 1 (success) 11 | * 12 | */ 13 | 14 | int _abs(int n) 15 | { 16 | return (n >= 0 ? n : n * -1); 17 | } 18 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/7-print_last_digit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_last_digit - Entry point 5 | * 6 | * @r: int input 7 | * 8 | * Description: prints the last digit of a number 9 | * 10 | * Return: last digit of number r 11 | * 12 | */ 13 | 14 | int print_last_digit(int r) 15 | { 16 | int n; 17 | 18 | if (r < 0) 19 | { 20 | n = -1 * (r % 10); 21 | } 22 | else 23 | { 24 | n = r % 10; 25 | } 26 | _putchar((n % 10) + '0'); 27 | return (n % 10); 28 | } 29 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/8-24_hours.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * jack_bauer - prints every minute of the day of Jack Bauer 5 | * Return:void 6 | */ 7 | 8 | void jack_bauer(void) 9 | { 10 | int h1; 11 | int h2; 12 | int m1; 13 | int m2; 14 | int a = 9; 15 | 16 | h2 = 0; 17 | while (h2 <= 2) 18 | { 19 | if (h2 == 2) 20 | { 21 | /*Restrain to 23h, not 29*/ 22 | a = 3; 23 | } 24 | h1 = 0; 25 | while (h1 <= a) 26 | { 27 | m2 = 0; 28 | while (m2 <= 5) 29 | { 30 | m1 = 0; 31 | while (m1 <= 9) 32 | { 33 | _putchar('0' + h2); 34 | _putchar('0' + h1); 35 | _putchar(':'); 36 | _putchar('0' + m2); 37 | _putchar('0' + m1); 38 | _putchar('\n'); 39 | m1++; 40 | } 41 | m2++; 42 | } 43 | h1++; 44 | } 45 | h2++; 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/9-times_table.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * times_table - prints times table 5 | * 6 | * Return: void 7 | * 8 | */ 9 | 10 | void times_table(void) 11 | { 12 | int i, j, k; 13 | 14 | for (i = 0; i <= 9; i++) 15 | { 16 | _putchar('0'); 17 | _putchar(','); 18 | _putchar(' '); 19 | for (j = 1; j <= 9; j++) 20 | { 21 | k = (i * j); 22 | if ((k / 10) > 0) 23 | { 24 | _putchar((k / 10) + '0'); 25 | } 26 | else 27 | { 28 | _putchar(' '); 29 | } 30 | _putchar((k % 10) + '0'); 31 | 32 | if (j < 9) 33 | { 34 | _putchar(','); 35 | _putchar(' '); 36 | } 37 | } 38 | _putchar('\n'); 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/README.md: -------------------------------------------------------------------------------- 1 | [![Typing SVG](https://readme-typing-svg.herokuapp.com?font=Fira+Code&weight=600&size=25&pause=1000&color=00C7F7&width=435&lines=FUNCTIONS_NESTED+LOOPS)](https://git.io/typing-svg) 2 | 3 | Alx project on Functions, nested loops 4 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _putchar - writes the character c to stdout 5 | * @c: The character to print 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | int _putchar(char c) 11 | { 12 | return (write(1, &c, 1)); 13 | } 14 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/main.h: -------------------------------------------------------------------------------- 1 | #ifndef main_h 2 | #define main_h 3 | 4 | int _putchar(char c); 5 | void print_alphabet(void); 6 | void print_alphabet_x10(void); 7 | int _islower(int c); 8 | int _isalpha(int c); 9 | int print_sign(int n); 10 | int _abs(int); 11 | int print_last_digit(int); 12 | void jack_bauer(void); 13 | void times_table(void); 14 | int add(int, int); 15 | void print_to_98(int n); 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /0x03-debugging/0-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - tests function that prints if integer is positive or negative 5 | * Return: 0 6 | */ 7 | 8 | int main(void) 9 | { 10 | int i; 11 | 12 | i = 0; 13 | positive_or_negative(i); 14 | 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x03-debugging/1-main.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 && a >= c) 16 | { 17 | largest = a; 18 | } 19 | else if (b >= a && b >= c) 20 | { 21 | largest = b; 22 | } 23 | else 24 | { 25 | largest = c; 26 | } 27 | 28 | return (largest); 29 | } 30 | -------------------------------------------------------------------------------- /0x03-debugging/3-print_remaining_days.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "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 | [![Typing SVG](https://readme-typing-svg.herokuapp.com?font=Fira+Code&weight=600&size=25&pause=1000&color=00C7F7&width=435&lines=THE+BUGGING+!!+(DEBUGGING))](https://git.io/typing-svg) 2 | 3 | ALX project on Debugging a code 4 | 5 | This is the section of debugging. It entails all the task on the debugging project. 6 | Debugging is the process of resolving errors which are called bugs in the computer world. 7 | Debugging helps to produce the expected outcome from our source code. 8 | -------------------------------------------------------------------------------- /0x03-debugging/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | #include 5 | 6 | void positive_or_negative(int i); 7 | int largest_number(int a, int b, int c); 8 | int convert_day(int month, int day); 9 | void print_remaining_days(int month, int day, int year); 10 | 11 | #endif /* MAIN_H */ 12 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/0-isupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isupper - check uppercase 5 | * @c: character to be checked 6 | * Return: 1 if upper, 0 otherwise 7 | */ 8 | 9 | int _isupper(int c) 10 | { 11 | if (c >= 'A' && c <= 'Z') 12 | { 13 | return (1); 14 | } 15 | else 16 | { 17 | return (0); 18 | } 19 | _putchar('\n'); 20 | } 21 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/1-isdigit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isdigit - checks if a character is a digit 5 | * 6 | * Description: Write a function that checks for a digit (0 through 9). 7 | * @c: character to check 8 | * 9 | * Return: 0 if not a digit and 1 if it a digit 10 | * 11 | */ 12 | 13 | int _isdigit(int c) 14 | { 15 | if (c >= '0' && c <= '9') 16 | { 17 | return (1); 18 | } 19 | else 20 | { 21 | return (0); 22 | } 23 | _putchar('\n'); 24 | } 25 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/10-print_triangle.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_triangle - print the shape of a triangle 5 | * 6 | * Description: a function that prints a triangle, followed by a new line. 7 | *@size: parameter 8 | * 9 | * Return: Always 0 (success) 10 | * 11 | */ 12 | 13 | void print_triangle(int size) 14 | { 15 | int i, j, k; 16 | 17 | if (size <= 0) 18 | { 19 | _putchar('\n'); 20 | } 21 | else 22 | { 23 | for (i = 1; i <= size; i++) 24 | { 25 | for (k = size - i; k >= 1; k--) 26 | { 27 | _putchar(' '); 28 | } 29 | for (j = 1; j <= i; j++) 30 | { 31 | _putchar('#'); 32 | } 33 | _putchar('\n'); 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/100-prime_factor.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Prints out the larget prime factor of 612852475143 5 | * 6 | * Return: Always 0 (success) 7 | */ 8 | 9 | int main(void) 10 | { 11 | long i; 12 | long num = 612852475143; 13 | 14 | for (i = 2; i < num; i++) 15 | { 16 | if (num % i == 0) 17 | num /= i; 18 | } 19 | printf("%li\n", num); 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/101-print_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_number - prints an integer 5 | * @n: integer to be printed 6 | */ 7 | 8 | void print_number(int n) 9 | { 10 | unsigned int n1; 11 | 12 | if (n < 0) 13 | { 14 | n1 = -n; 15 | _putchar('-'); 16 | } else 17 | { 18 | n1 = n; 19 | } 20 | 21 | if (n1 / 10) 22 | { 23 | print_number(n1 / 10); 24 | } 25 | 26 | _putchar((n1 % 10) + '0'); 27 | } 28 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/2-mul.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * mul - multiplies two numbers a and b 5 | * 6 | * Description: Write a function that multiplies two integers a and b. 7 | * @a: character to multiply 8 | * @b: character to multiply 9 | * 10 | * Return: Always 0 (success) 11 | * 12 | */ 13 | 14 | int mul(int a, int b) 15 | { 16 | return (a * b); 17 | } 18 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/3-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_numbers - print number 0 to 9 5 | * 6 | */ 7 | 8 | void print_numbers(void) 9 | { 10 | char i; 11 | 12 | for (i = '0'; i <= '9'; i++) 13 | { 14 | _putchar(i); 15 | } 16 | _putchar('\n'); 17 | } 18 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/4-print_most_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_most_numbers - print all numbers except excluded ones 5 | * 6 | * Description: PRINT ALL NUMBERS EXCEPT A & B 7 | * 8 | * Return: Always 0 (success) 9 | * 10 | */ 11 | 12 | void print_most_numbers(void) 13 | { 14 | int a; 15 | 16 | for (a = '0'; a <= '9'; a++) 17 | { 18 | if (a != '2' && a != '4') 19 | { 20 | _putchar(a); 21 | } 22 | } 23 | _putchar('\n'); 24 | } 25 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/5-more_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * more_numbers - prints the range 0-14 ten times. 5 | * 6 | * Return: 0. 7 | * 8 | */ 9 | 10 | void more_numbers(void) 11 | { 12 | int i, j; 13 | 14 | i = j = 0; 15 | while (i < 10) 16 | { 17 | while (j <= 14) 18 | { 19 | if (j >= 10) 20 | _putchar(j / 10 + '0'); 21 | _putchar(j % 10 + '0'); 22 | ++j; 23 | } 24 | _putchar('\n'); 25 | j = 0; 26 | i++; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/6-print_line.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_line - draws a straight line (n) characters long. 5 | * @n: the number of underscores to print. 6 | */ 7 | 8 | void print_line(int n) 9 | { 10 | int i; 11 | 12 | if (n > 0) 13 | for (i = 0; i < n; i++) 14 | _putchar('_'); 15 | _putchar('\n'); 16 | } 17 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/7-print_diagonal.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_diagonal - draws a diagonal line across (n) terminal lines. 5 | * @n: the number of lines to print 6 | */ 7 | 8 | void print_diagonal(int n) 9 | { 10 | int i, j; 11 | 12 | if (n > 0) 13 | { 14 | for (j = 1; j <= n; j++) 15 | { 16 | for (i = 1; i <= j; i++) 17 | { 18 | if (i != 1) 19 | _putchar(' '); 20 | if (i == j) 21 | { 22 | _putchar('\\'); 23 | _putchar('\n'); 24 | } 25 | } 26 | } 27 | } 28 | else 29 | _putchar('\n'); 30 | } 31 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/8-print_square.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_square - draws a square using multiple terminal lines and #'s. 5 | * @n: the number of # to print in each line. 6 | */ 7 | 8 | void print_square(int n) 9 | { 10 | int i, j; 11 | 12 | if (n > 0) 13 | { 14 | for (i = 0; i < n; i++) 15 | { 16 | for (j = 0; j < n; j++) 17 | _putchar('#'); 18 | _putchar('\n'); 19 | } 20 | } 21 | else 22 | _putchar('\n'); 23 | } 24 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/9-fizz_buzz.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - Entry Point 6 | * Description: Fizz for 3, Buzz for 5 and FizzBuzz for 3 and 5 7 | * Return: Always 0 (Success) 8 | */ 9 | 10 | int main(void) 11 | { 12 | int i; 13 | 14 | for (i = 1; i <= 100; i++) 15 | { 16 | if (i % 15 == 0) 17 | { 18 | printf("FizzBuzz"); 19 | } 20 | else if (i % 3 == 0) 21 | { 22 | printf("Fizz"); 23 | } 24 | else if (i % 5 == 0) 25 | { 26 | printf("Buzz"); 27 | } 28 | else 29 | { 30 | printf("%i", i); 31 | } 32 | if (i < 100) 33 | { 34 | printf(" "); 35 | } 36 | } 37 | printf("\n"); 38 | return (0); 39 | } 40 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/README.md: -------------------------------------------------------------------------------- 1 | [![Typing SVG](https://readme-typing-svg.herokuapp.com?font=Fira+Code&weight=600&size=25&pause=1000&color=00C7F7&width=435&lines=MORE+FUNCTIONS_NESTED+LOOPS)](https://git.io/typing-svg) 2 | 3 | ALX Project on C functions and nested loops 4 | Tasks: 5 | 1. Write a function that checks for uppercase character. 6 | 2. Write a function that checks for a digit (0 through 9). 7 | 3. Write a function that multiplies two integers. 8 | 4. Write a function that prints the numbers, from 0 to 9, followed by a new line. 9 | 5. Write a function that prints the numbers, from 0 to 9, followed by a new line. 10 | 6. Write a function that prints 10 times the numbers, from 0 to 14, followed by a new line. 11 | 7. Write a function that draws a straight line in the terminal. 12 | 8. Write a function that draws a diagonal line on the terminal. 13 | 9. Write a function that prints a square, followed by a new line. 14 | 10. Write a program that prints the numbers from 1 to 100, followed by a new line. 15 | 11. Write a function that prints a triangle, followed by a new line. 16 | 12. Write a program that finds and prints the largest prime factor of the number 612852475143, followed by a new line. 17 | 13. Write a function that prints an integer. 18 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _putchar - writes a character c to stdout 5 | * 6 | * @c : character to print 7 | * 8 | * Return: Always 1 (success) 9 | * 10 | * On error -1 is returned, and errno is set appropriately 11 | * 12 | */ 13 | 14 | int _putchar(char c) 15 | { 16 | return (write(1, &c, 1)); 17 | } 18 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | int _putchar(char c); 5 | int _isupper(int c); 6 | int _isdigit(int c); 7 | int mul(int a, int b); 8 | void print_numbers(void); 9 | void print_most_numbers(void); 10 | void more_numbers(void); 11 | void print_line(int n); 12 | void print_diagonal(int n); 13 | void print_square(int size); 14 | void print_triangle(int size); 15 | void print_number(int n); 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/0-reset_to_98.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * reset_to_98 - updates the value of n to 98 5 | * @n: integer pointer to a variable to be updated 6 | * 7 | * Return: nothing 8 | */ 9 | 10 | void reset_to_98(int *n) 11 | { 12 | *n = 98; 13 | } 14 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/1-swap.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * swap_int - swaps the values of two integers 5 | * 6 | * Description: a function that swaps the values of two integers. 7 | *@a: first integer to swap 8 | *@b: seconf integer to swap 9 | * 10 | * Return: nothing 11 | * 12 | */ 13 | 14 | void swap_int(int *a, int *b) 15 | { 16 | *a += *b; 17 | *b = *a - *b; 18 | *a = *a - *b; 19 | } 20 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/100-atoi.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _atoi - convert a string to an integer. 5 | * @s: the string to be comverted. 6 | * 7 | *Return: The integer value of the comverted string. 8 | */ 9 | 10 | int _atoi(char *s) 11 | { 12 | int sign = 1; 13 | unsigned int num = 0; 14 | 15 | do { 16 | 17 | if (*s == '-') 18 | sign *= -1; 19 | 20 | else if (*s >= '0' && *s <= '9') 21 | num = (num * 10) + (*s - '0'); 22 | 23 | else if (num > 0) 24 | break; 25 | } while (*s++); 26 | 27 | return (num * sign); 28 | } 29 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/101-keygen.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /** 6 | * main - generates random passwords for 101-crackme 7 | * Return: zero 8 | */ 9 | 10 | int main(void) 11 | { 12 | int sum; 13 | char c; 14 | 15 | srand(time(NULL)); 16 | while (sum <= 2645) 17 | { 18 | c = rand() % 128; 19 | sum += c; 20 | putchar(c); 21 | } 22 | putchar(2772 - sum); 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/2-strlen.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strlen - print the length of a string 5 | * 6 | * Description: a function that returns the length of a string. 7 | * 8 | * @s: string variable 9 | * 10 | * Return: nothing 11 | * 12 | */ 13 | 14 | int _strlen(char *s) 15 | { 16 | int length = 0; 17 | 18 | while (s[length]) 19 | length++; 20 | return (length); 21 | } 22 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/3-puts.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _puts - prints a string and a new line 5 | * @str: pointer to a string to print 6 | * 7 | * Return: 0 8 | */ 9 | 10 | void _puts(char *str) 11 | { 12 | int i = 0; 13 | 14 | while (str[i]) 15 | { 16 | _putchar(str[i]); 17 | i++; 18 | } 19 | _putchar('\n'); 20 | } 21 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/4-print_rev.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_rev - prints a string in reverse 5 | * @s: the used string reference pointer 6 | * Return: 0 7 | */ 8 | 9 | void print_rev(char *s) 10 | { 11 | int i = 0; 12 | 13 | while (s[i]) 14 | i++; 15 | while (i--) 16 | _putchar(s[i]); 17 | _putchar('\n'); 18 | } 19 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/5-rev_string.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * rev_string - prints a string in reverse 5 | * @s: the used string to beb reversed 6 | * Return: 0 7 | */ 8 | 9 | void rev_string(char *s) 10 | { 11 | int len, i, half; 12 | char temp; 13 | 14 | for (len = 0; s[len] != '\0'; len++) 15 | ; 16 | i = 0; 17 | half = len / 2; 18 | 19 | while (half--) 20 | { 21 | temp = s[len - i - 1]; 22 | s[len - i - 1] = s[i]; 23 | s[i] = temp; 24 | i++; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/6-puts2.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * puts2 - prints xters of a string 5 | * @str: the used string reference 6 | * Return: 0 7 | */ 8 | 9 | void puts2(char *str) 10 | { 11 | int i = 0; 12 | 13 | while (str[i] != '\0') 14 | { 15 | if (i % 2 == 0) 16 | { 17 | _putchar(str[i]); 18 | } 19 | i++; 20 | } 21 | _putchar('\n'); 22 | } 23 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/7-puts_half.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * puts_half - prints the second half of the string 5 | * @str: the string reference 6 | * Return: 0 7 | */ 8 | 9 | void puts_half(char *str) 10 | { 11 | int i; 12 | 13 | for (i = 0; str[i] != '\0'; i++) 14 | ; 15 | i++; 16 | for (i /= 2; str[i] != '\0'; i++) 17 | { 18 | _putchar(str[i]); 19 | } 20 | _putchar('\n'); 21 | } 22 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/8-print_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * print_array - print `n` elements of an array of integers 6 | * @a: int type array pointer 7 | * @n: int type integer 8 | * Description: Numbers must be separated by comma and space. 9 | * Numbers should be displayed in the same order they are stored in array. 10 | * You can only use _putchar to print. 11 | */ 12 | 13 | void print_array(int *a, int n) 14 | { 15 | int i; 16 | 17 | i = 0; 18 | for (n--; n >= 0; n--, i++) 19 | { 20 | printf("%d", a[i]); 21 | if (n > 0) 22 | { 23 | printf(", "); 24 | } 25 | } 26 | printf("\n"); 27 | } 28 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/9-strcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * *_strcpy - copies the string pointed to by src 5 | * @dest: char type string 6 | * @src: char type string 7 | * Description: Copy the string pointed to by pointer `src` to 8 | * the buffer pointed to by `dest` 9 | * Return: Pointer to `dest 10 | */ 11 | 12 | char *_strcpy(char *dest, char *src) 13 | { 14 | int i = -1; 15 | 16 | do { 17 | i++; 18 | dest[i] = src[i]; 19 | } while (src[i] != '\0'); 20 | 21 | return (dest); 22 | } 23 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | [![Typing SVG](https://readme-typing-svg.herokuapp.com?font=Fira+Code&weight=900&pause=1000&width=435&lines=C+POINTERS%2C+ARRAYS+AND+STRINGS)](https://git.io/typing-svg) 2 | 3 | DETAILS 4 | 1. Write a function that takes a pointer to an int as parameter and updates the value it points to to 98. 5 | 2. Write a function that swaps the values of two integers. 6 | 3. Write a function that returns the length of a string. 7 | 4. Write a function that prints a string, followed by a new line, to stdout. 8 | 5. Write a function that prints a string, in reverse, followed by a new line. 9 | 6. Write a function that reverses a string. 10 | 7. Write a function that prints every other character of a string, starting with the first character, followed by a new line. 11 | 8. Write a function that prints half of a string, followed by a new line. 12 | 9. Write a function that prints n elements of an array of integers, followed by a new line. 13 | 10. Write a function that copies the string pointed to by src, including the terminating null byte (\0), to the buffer pointed to by dest. 14 | 11. Write a function that convert a string to an integer. 15 | 12. Create a program that generates random valid passwords for the program 101-crackme. 16 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _putchar - writes the character c to stdout 5 | * @c: The character to print 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | 11 | int _putchar(char c) 12 | { 13 | return (write(1, &c, 1)); 14 | } 15 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | #ifndef main_h 2 | #define main_h 3 | 4 | int _putchar(char c); 5 | void reset_to_98(int *n); 6 | void swap_int(int *a, int *b); 7 | int _strlen(char *s); 8 | void _puts(char *str); 9 | void print_rev(char *s); 10 | void rev_string(char *s); 11 | void puts2(char *str); 12 | void puts_half(char *str); 13 | void print_array(int *a, int n); 14 | char *_strcpy(char *dest, char *src); 15 | int _atoi(char *s); 16 | 17 | 18 | #endif 19 | 20 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/0-strcat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strcat - concatenates two strings 5 | * @dest: input value 6 | * @src: input value 7 | * Return: void 8 | */ 9 | 10 | char *_strcat(char *dest, char *src) 11 | { 12 | int i; 13 | int j; 14 | 15 | i = 0; 16 | while (dest[i] != '\0') 17 | { 18 | i++; 19 | } 20 | j = 0; 21 | while (src[j] != '\0') 22 | { 23 | dest[i] = src[j]; 24 | i++; 25 | j++; 26 | } 27 | dest[i] = '\0'; 28 | return (dest); 29 | } 30 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/1-strncat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strncat - concatenates two strings, 5 | * @dest: destination path. 6 | * @src: source. 7 | * @n: amount of bytes from src. 8 | * Return: the pointer to destination. 9 | */ 10 | 11 | char *_strncat(char *dest, char *src, int n) 12 | { 13 | int count = 0, count2 = 0; 14 | 15 | while (*(dest + count) != '\0') 16 | { 17 | count++; 18 | } 19 | 20 | while (count2 < n) 21 | { 22 | *(dest + count) = *(src + count2); 23 | if (*(src + count2) == '\0') 24 | break; 25 | count++; 26 | count2++; 27 | } 28 | return (dest); 29 | } 30 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/100-rot13.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * rot13 - encodes a string using rot13 5 | * @s: input string. 6 | * Return: the pointer to dest. 7 | */ 8 | 9 | char *rot13(char *s) 10 | { 11 | int count = 0, i; 12 | char alphabet[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; 13 | char rot13[] = "nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM"; 14 | 15 | while (*(s + count) != '\0') 16 | { 17 | for (i = 0; i < 52; i++) 18 | { 19 | if (*(s + count) == alphabet[i]) 20 | { 21 | *(s + count) = rot13[i]; 22 | break; 23 | } 24 | } 25 | count++; 26 | } 27 | 28 | return (s); 29 | } 30 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/101-print_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_number - prints an integer. 5 | * @n: input integer. 6 | * Return: no return. 7 | */ 8 | 9 | void print_number(int n) 10 | { 11 | unsigned int m, d, count; 12 | 13 | if (n < 0) 14 | { 15 | _putchar(45); 16 | m = n * -1; 17 | } 18 | else 19 | { 20 | m = n; 21 | } 22 | 23 | d = m; 24 | count = 1; 25 | 26 | while (d > 9) 27 | { 28 | d /= 10; 29 | count *= 10; 30 | } 31 | 32 | for (; count >= 1; count /= 10) 33 | { 34 | _putchar(((m / count) % 10) + 48); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/102-magic.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | int n; 6 | int a[5]; 7 | int *p; 8 | 9 | a[2] = 1024; 10 | p = &n; 11 | /* 12 | * write your line of code here... 13 | * Remember: 14 | * - you are not allowed to use a 15 | * - you are not allowed to modify p 16 | * - only one statement 17 | * - you are not allowed to code anything else than this line of code 18 | */ 19 | p[5] = 98; 20 | /* ...so that this prints 98\n */ 21 | printf("a[2] = %d\n", a[2]); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/103-infinite_add.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * infinite_add - adds two numbers 5 | * @n1: number one. 6 | * @n2: number two. 7 | * @r: buffer that the function will use to store the result. 8 | * @size_r: buffer size: 9 | * Return: the pointer to dest. 10 | */ 11 | 12 | char *infinite_add(char *n1, char *n2, char *r, int size_r) 13 | { 14 | int c1 = 0, c2 = 0, op, bg, dr1, dr2, add = 0; 15 | 16 | while (*(n1 + c1) != '\0') 17 | c1++; 18 | while (*(n2 + c2) != '\0') 19 | c2++; 20 | if (c1 >= c2) 21 | bg = c1; 22 | else 23 | bg = c2; 24 | if (size_r <= bg + 1) 25 | return (0); 26 | r[bg + 1] = '\0'; 27 | c1--, c2--, size_r--; 28 | dr1 = *(n1 + c1) - 48, dr2 = *(n2 + c2) - 48; 29 | while (bg >= 0) 30 | { 31 | op = dr1 + dr2 + add; 32 | if (op >= 10) 33 | add = op / 10; 34 | else 35 | add = 0; 36 | if (op > 0) 37 | *(r + bg) = (op % 10) + 48; 38 | else 39 | *(r + bg) = '0'; 40 | if (c1 > 0) 41 | c1--, dr1 = *(n1 + c1) - 48; 42 | else 43 | dr1 = 0; 44 | if (c2 > 0) 45 | c2--, dr2 = *(n2 + c2) - 48; 46 | else 47 | dr2 = 0; 48 | bg--, size_r--; 49 | } 50 | if (*(r) == '0') 51 | return (r + 1); 52 | else 53 | return (r); 54 | } 55 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/104-print_buffer.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * print_buffer - prints a buffer 6 | * @b: buffer. 7 | * @size: size of buffer. 8 | * Return: no return. 9 | */ 10 | void print_buffer(char *b, int size) 11 | { 12 | int j, k, l; 13 | 14 | if (size <= 0) 15 | printf("\n"); 16 | else 17 | { 18 | for (j = 0; j < size; j += 10) 19 | { 20 | printf("%.8x:", j); 21 | for (k = j; k < j + 10; k++) 22 | { 23 | if (k % 2 == 0) 24 | printf(" "); 25 | if (k < size) 26 | printf("%.2x", *(b + k)); 27 | else 28 | printf(" "); 29 | } 30 | printf(" "); 31 | for (l = j; l < j + 10; l++) 32 | { 33 | if (l >= size) 34 | break; 35 | if (*(b + l) < 32 || *(b + l) > 126) 36 | printf("%c", '.'); 37 | else 38 | printf("%c", *(b + l)); 39 | } 40 | printf("\n"); 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/2-strncpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strncpy - copies a string 5 | * @dest: destination. 6 | * @src: source. 7 | * @n: amount of bytes from src. 8 | * Return: the pointer to destination. 9 | */ 10 | 11 | char *_strncpy(char *dest, char *src, int n) 12 | { 13 | int i; 14 | 15 | for (i = 0; i < n && src[i] != '\0'; i++) 16 | dest[i] = src[i]; 17 | for ( ; i < n; i++) 18 | dest[i] = '\0'; 19 | 20 | return (dest); 21 | } 22 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/3-strcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strcmp - compares two strings 4 | * @s1: first string. 5 | * @s2: second string. 6 | * Return: 0 if s1 and s2 are equal to eachother, 7 | * another number if not. 8 | */ 9 | 10 | int _strcmp(char *s1, char *s2) 11 | { 12 | int i = 0, parv = 0; 13 | 14 | while (parv == 0) 15 | { 16 | if ((*(s1 + i) == '\0') && (*(s2 + i) == '\0')) 17 | break; 18 | parv = *(s1 + i) - *(s2 + i); 19 | i++; 20 | } 21 | 22 | return (parv); 23 | } 24 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/4-rev_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * reverse_array - reverses the content of an array 4 | * of integers. 5 | * @a: array. 6 | * @n: number of elements of the array. 7 | * Return: nothing. 8 | */ 9 | 10 | void reverse_array(int *a, int n) 11 | { 12 | int i, b, parv; 13 | 14 | for (i = 0; i < n - 1; i++) 15 | { 16 | for (b = i + 1; b > 0; b--) 17 | { 18 | parv = *(a + b); 19 | *(a + b) = *(a + (b - 1)); 20 | *(a + (b - 1)) = parv; 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/5-string_toupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * string_toupper - changes all lowercase letters of a string 5 | * to uppercase 6 | * @s: input string. 7 | * Return: the pointer to destination. 8 | */ 9 | 10 | char *string_toupper(char *s) 11 | { 12 | int count = 0; 13 | 14 | while (*(s + count) != '\0') 15 | { 16 | if ((*(s + count) >= 97) && (*(s + count) <= 122)) 17 | *(s + count) = *(s + count) - 32; 18 | count++; 19 | } 20 | 21 | return (s); 22 | } 23 | -------------------------------------------------------------------------------- /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: input string. 6 | * Return: the pointer to dest. 7 | */ 8 | 9 | char *cap_string(char *s) 10 | { 11 | int count = 0, i; 12 | int sep_words[] = {32, 9, 10, 44, 59, 46, 33, 63, 34, 40, 41, 123, 125}; 13 | 14 | if (*(s + count) >= 97 && *(s + count) <= 122) 15 | *(s + count) = *(s + count) - 32; 16 | count++; 17 | while (*(s + count) != '\0') 18 | { 19 | for (i = 0; i < 13; i++) 20 | { 21 | if (*(s + count) == sep_words[i]) 22 | { 23 | if ((*(s + (count + 1)) >= 97) && (*(s + (count + 1)) <= 122)) 24 | *(s + (count + 1)) = *(s + (count + 1)) - 32; 25 | break; 26 | } 27 | } 28 | count++; 29 | } 30 | return (s); 31 | } 32 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/7-leet.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * leet - encodes a string into 1337 5 | * @s: input string. 6 | * Return: the pointer to dest. 7 | */ 8 | 9 | char *leet(char *s) 10 | { 11 | int count = 0, i; 12 | int low_letters[] = {97, 101, 111, 116, 108}; 13 | int upp_letters[] = {65, 69, 79, 84, 76}; 14 | int numbers[] = {52, 51, 48, 55, 49}; 15 | 16 | while (*(s + count) != '\0') 17 | { 18 | for (i = 0; i < 5; i++) 19 | { 20 | if (*(s + count) == low_letters[i] || *(s + count) == upp_letters[i]) 21 | { 22 | *(s + count) = numbers[i]; 23 | break; 24 | } 25 | } 26 | count++; 27 | } 28 | 29 | return (s); 30 | } 31 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | [![Typing SVG](https://readme-typing-svg.herokuapp.com?font=Fira+Code&weight=900&pause=1000&width=435&lines=C+more+pointers+arrays+and+strings)](https://git.io/typing-svg) 2 | 3 | 1. Write a function that concatenates two strings. 4 | 2. Write a function that concatenates two strings. (version 2) 5 | 3. Write a function that copies a string. 6 | 4. Write a function that compares two strings. 7 | 5. Write a function that reverses the content of an array of integers. 8 | 6. Write a function that changes all lowercase letters of a string to uppercase. 9 | 7. Write a function that capitalizes all words of a string. 10 | 8. Write a function that encodes a string into 1337. 11 | 9. Write a function that encodes a string using rot13. 12 | 10. Write a function that prints an integer. 13 | 11. the program prints a[2] = 98. 14 | 12. Write a function that adds two numbers. 15 | 13. Write a function that prints a buffer. 16 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _putchar - writes the character c to stdout 5 | * @c: The character to print 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | 11 | int _putchar(char c) 12 | { 13 | return (write(1, &c, 1)); 14 | } 15 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | #ifndef main_h 2 | #define main_h 3 | 4 | int _putchar(char c); 5 | char *_strcat(char *dest, char *src); 6 | char *_strncat(char *dest, char *src, int n); 7 | char *_strncpy(char *dest, char *src, int n); 8 | int _strcmp(char *s1, char *s2); 9 | void reverse_array(int *a, int n); 10 | char *string_toupper(char *); 11 | char *cap_string(char *); 12 | char *leet(char *); 13 | char *rot13(char *); 14 | void print_number(int n); 15 | char *infinite_add(char *n1, char *n2, char *r, int size_r); 16 | void print_buffer(char *b, int size); 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/0-memset.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _memset - fill a block of memory with a specific value 4 | * @s: starting address of memory to be filled 5 | * @b: the desired value 6 | * @n: number of bytes to be changed 7 | * 8 | * Return: changed array with new value for n bytes 9 | */ 10 | char *_memset(char *s, char b, unsigned int n) 11 | { 12 | int i = 0; 13 | 14 | for (; n > 0; i++) 15 | { 16 | s[i] = b; 17 | n--; 18 | } 19 | return (s); 20 | } 21 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/1-memcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | *_memcpy - a function that copies memory area 5 | *@dest: memory where is stored 6 | *@src: memory where is copied 7 | *@n: number of bytes 8 | * 9 | *Return: copied memory with n byted changed 10 | */ 11 | 12 | char *_memcpy(char *dest, char *src, unsigned int n) 13 | { 14 | int r = 0; 15 | int i = n; 16 | 17 | for (; r < i; r++) 18 | { 19 | dest[r] = src[r]; 20 | n--; 21 | } 22 | return (dest); 23 | } 24 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/100-set_string.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * set_string - sets the value of a pointer to a char 5 | * @s: pointer to pointer 6 | * @to: pointer char 7 | */ 8 | 9 | void set_string(char **s, char *to) 10 | { 11 | *s = to; 12 | } 13 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/101-crackme_password: -------------------------------------------------------------------------------- 1 | abc123 -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/2-strchr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strchr - Entry point 5 | * @s: input 6 | * @c: input 7 | * Return: Always 0 (Success) 8 | */ 9 | 10 | char *_strchr(char *s, char c) 11 | { 12 | int i = 0; 13 | 14 | for (; s[i] >= '\0'; i++) 15 | { 16 | if (s[i] == c) 17 | return (&s[i]); 18 | } 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/3-strspn.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strspn - Entry point 5 | * @s: input 6 | * @accept: input 7 | * Return: Always 0 (Success) 8 | */ 9 | 10 | unsigned int _strspn(char *s, char *accept) 11 | { 12 | unsigned int n = 0; 13 | int r; 14 | 15 | while (*s) 16 | { 17 | for (r = 0; accept[r]; r++) 18 | { 19 | if (*s == accept[r]) 20 | { 21 | n++; 22 | break; 23 | } 24 | else if (accept[r + 1] == '\0') 25 | return (n); 26 | } 27 | s++; 28 | } 29 | return (n); 30 | } 31 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/4-strpbrk.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strpbrk - Entry point 5 | * @s: input 6 | * @accept: input 7 | * Return: Always 0 (Success) 8 | */ 9 | 10 | char *_strpbrk(char *s, char *accept) 11 | { 12 | int k; 13 | 14 | while (*s) 15 | { 16 | for (k = 0; accept[k]; k++) 17 | { 18 | if (*s == accept[k]) 19 | return (s); 20 | } 21 | s++; 22 | } 23 | 24 | return ('\0'); 25 | } 26 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/5-strstr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strstr - Entry point 5 | * @haystack: input 6 | * @needle: input 7 | * Return: Always 0 (Success) 8 | */ 9 | 10 | char *_strstr(char *haystack, char *needle) 11 | { 12 | for (; *haystack != '\0'; haystack++) 13 | { 14 | char *l = haystack; 15 | char *p = needle; 16 | 17 | while (*l == *p && *p != '\0') 18 | { 19 | l++; 20 | p++; 21 | } 22 | 23 | if (*p == '\0') 24 | return (haystack); 25 | } 26 | 27 | return (0); 28 | } 29 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/7-print_chessboard.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_chessboard - Entry point 5 | * @a: array 6 | * Return: Always 0 (Success) 7 | */ 8 | 9 | void print_chessboard(char (*a)[8]) 10 | { 11 | int i; 12 | int j; 13 | 14 | for (i = 0; i < 8; i++) 15 | { 16 | for (j = 0; j < 8; j++) 17 | _putchar(a[i][j]); 18 | _putchar('\n'); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/8-print_diagsums.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * print_diagsums - Entry point 6 | * @a: input 7 | * @size: input 8 | * Return: Always 0 (Success) 9 | */ 10 | 11 | void print_diagsums(int *a, int size) 12 | { 13 | int sum1, sum2, y; 14 | 15 | sum1 = 0; 16 | sum2 = 0; 17 | 18 | for (y = 0; y < size; y++) 19 | { 20 | sum1 = sum1 + a[y * size + y]; 21 | } 22 | 23 | for (y = size - 1; y >= 0; y--) 24 | { 25 | sum2 += a[y * size + (size - y - 1)]; 26 | } 27 | 28 | printf("%d, %d\n", sum1, sum2); 29 | } 30 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | [![Typing SVG](https://readme-typing-svg.herokuapp.com?font=Fira+Code&weight=900&pause=1000&width=435&lines=Even+more+pointers+arrays+and+strings)](https://git.io/typing-svg) 2 | 3 | 1. Write a function that fills memory with a constant byte. 4 | 2. Write a function that copies memory area. 5 | 3. Write a function that locates a character in a string. 6 | 4. Write a function that gets the length of a prefix substring. 7 | 5. Write a function that searches a string for any of a set of bytes. 8 | 6. Write a function that locates a substring. 9 | 7. Write a function that prints the chessboard. 10 | 8. Write a function that prints the sum of the two diagonals of a square matrix of integers. 11 | 9. Write a function that sets the value of a pointer to a char. 12 | 10. Create a file that contains the password for the crackme2 executable. 13 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/_putchar.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _putchar - writes the character c to stdout 6 | * @c: The character to print 7 | * 8 | * Return: On success 1. 9 | * On error, -1 is returned, and errno is set appropriately. 10 | */ 11 | 12 | int _putchar(char c) 13 | { 14 | return (write(1, &c, 1)); 15 | } 16 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | #ifndef main_h 2 | #define main_h 3 | 4 | char *_memset(char *s, char b, unsigned int n); 5 | char *_memcpy(char *dest, char *src, unsigned int n); 6 | char *_strchr(char *s, char c); 7 | unsigned int _strspn(char *s, char *accept); 8 | char *_strpbrk(char *s, char *accept); 9 | char *_strstr(char *haystack, char *needle); 10 | void print_chessboard(char (*a)[8]); 11 | void print_diagsums(int *a, int size); 12 | void set_string(char **s, char *to); 13 | int _putchar(char c); 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /0x08-recursion/0-puts_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _puts_recursion - function like puts(); 5 | * @s: input 6 | * Return: Always 0 (Success) 7 | */ 8 | 9 | void _puts_recursion(char *s) 10 | { 11 | if (*s) 12 | { 13 | _putchar(*s); 14 | _puts_recursion(s + 1); 15 | } 16 | 17 | else 18 | _putchar('\n'); 19 | } 20 | -------------------------------------------------------------------------------- /0x08-recursion/1-print_rev_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _print_rev_recursion - Prints a string in reverse. 5 | * @s: The string to be printed. 6 | */ 7 | 8 | void _print_rev_recursion(char *s) 9 | { 10 | if (*s) 11 | { 12 | _print_rev_recursion(s + 1); 13 | _putchar(*s); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /0x08-recursion/100-is_palindrome.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | int check_pal(char *s, int i, int len); 4 | int _strlen_recursion(char *s); 5 | 6 | /** 7 | * is_palindrome - checks if a string is a palindrome 8 | * @s: string to reverse 9 | * 10 | * Return: 1 if it is, 0 it's not 11 | */ 12 | 13 | int is_palindrome(char *s) 14 | { 15 | if (*s == 0) 16 | return (1); 17 | return (check_pal(s, 0, _strlen_recursion(s))); 18 | } 19 | 20 | /** 21 | * _strlen_recursion - returns the length of a string 22 | * @s: string to calculate the length of 23 | * 24 | * Return: length of the string 25 | */ 26 | 27 | int _strlen_recursion(char *s) 28 | { 29 | if (*s == '\0') 30 | return (0); 31 | return (1 + _strlen_recursion(s + 1)); 32 | } 33 | 34 | /** 35 | * check_pal - checks the characters recursively for palindrome 36 | * @s: string to check 37 | * @i: iterator 38 | * @len: length of the string 39 | * 40 | * Return: 1 if palindrome, 0 if not 41 | */ 42 | 43 | int check_pal(char *s, int i, int len) 44 | { 45 | if (*(s + i) != *(s + len - 1)) 46 | return (0); 47 | if (i >= len) 48 | return (1); 49 | return (check_pal(s, i + 1, len - 1)); 50 | } 51 | -------------------------------------------------------------------------------- /0x08-recursion/101-wildcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * wildcmp - Compare strings 5 | * @s1: pointer to string params 6 | * @s2: pointer to string params 7 | * Return: 0 8 | */ 9 | 10 | int wildcmp(char *s1, char *s2) 11 | { 12 | if (*s1 == '\0') 13 | { 14 | if (*s2 != '\0' && *s2 == '*') 15 | { 16 | return (wildcmp(s1, s2 + 1)); 17 | } 18 | return (*s2 == '\0'); 19 | } 20 | 21 | if (*s2 == '*') 22 | { 23 | return (wildcmp(s1 + 1, s2) || wildcmp(s1, s2 + 1)); 24 | } 25 | else if (*s1 == *s2) 26 | { 27 | return (wildcmp(s1 + 1, s2 + 1)); 28 | } 29 | return (0); 30 | } 31 | -------------------------------------------------------------------------------- /0x08-recursion/2-strlen_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strlen_recursion - Returns the length of a string. 5 | * @s: The string to be measured. 6 | * 7 | * Return: The length of the string. 8 | */ 9 | 10 | int _strlen_recursion(char *s) 11 | { 12 | int longit = 0; 13 | 14 | if (*s) 15 | { 16 | longit++; 17 | longit += _strlen_recursion(s + 1); 18 | } 19 | 20 | return (longit); 21 | } 22 | -------------------------------------------------------------------------------- /0x08-recursion/3-factorial.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * factorial - returns the factorial of a number 5 | * @n: number to return the factorial from 6 | * 7 | * Return: factorial of n 8 | */ 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 - returns the value of x raised to the power of y 5 | * @x: value to raise 6 | * @y: power 7 | * 8 | * Return: result of the power 9 | */ 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 | int actual_sqrt_recursion(int n, int i); 4 | 5 | /** 6 | * _sqrt_recursion - returns the natural square root of a number 7 | * @n: number to calculate the square root of 8 | * 9 | * Return: the resulting square root 10 | */ 11 | 12 | int _sqrt_recursion(int n) 13 | { 14 | if (n < 0) 15 | return (-1); 16 | return (actual_sqrt_recursion(n, 0)); 17 | } 18 | 19 | /** 20 | * actual_sqrt_recursion - recurses to find the natural 21 | * square root of a number 22 | * @n: number to calculate the sqaure root of 23 | * @i: iterator 24 | * 25 | * Return: the resulting square root 26 | */ 27 | 28 | int actual_sqrt_recursion(int n, int i) 29 | { 30 | if (i * i > n) 31 | return (-1); 32 | if (i * i == n) 33 | return (i); 34 | return (actual_sqrt_recursion(n, i + 1)); 35 | } 36 | -------------------------------------------------------------------------------- /0x08-recursion/6-is_prime_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | int actual_prime(int n, int i); 4 | 5 | /** 6 | * is_prime_number - says if an integer is a prime number or not 7 | * @n: number to evaluate 8 | * 9 | * Return: 1 if n is a prime number, 0 if not 10 | */ 11 | 12 | int is_prime_number(int n) 13 | { 14 | if (n <= 1) 15 | return (0); 16 | return (actual_prime(n, n - 1)); 17 | } 18 | 19 | /** 20 | * actual_prime - calculates if a number is prime recursively 21 | * @n: number to evaluate 22 | * @i: iterator 23 | * 24 | * Return: 1 if n is prime, 0 if not 25 | */ 26 | 27 | int actual_prime(int n, int i) 28 | { 29 | if (i == 1) 30 | return (1); 31 | if (n % i == 0 && i > 0) 32 | return (0); 33 | return (actual_prime(n, i - 1)); 34 | } 35 | -------------------------------------------------------------------------------- /0x08-recursion/README.md: -------------------------------------------------------------------------------- 1 | [![Typing SVG](https://readme-typing-svg.herokuapp.com?font=Fira+Code&weight=900&size=29&pause=1000&width=435&lines=C+RECURSION+!!)](https://git.io/typing-svg) 2 | 3 | 1. Write a function that prints a string, followed by a new line. 4 | 2. Write a function that prints a string in reverse. 5 | 3. Write a function that returns the length of a string. 6 | 4. Write a function that returns the factorial of a given number. 7 | 5. Write a function that returns the value of x raised to the power of y. 8 | 6. Write a function that returns the natural square root of a number. 9 | 7. Write a function that returns 1 if the input integer is a prime number, otherwise return 0. 10 | 8. Write a function that returns 1 if a string is a palindrome and 0 if not. 11 | 9. Write a function that compares two strings and returns 1 if the strings can be considered identical, otherwise return 0. 12 | -------------------------------------------------------------------------------- /0x08-recursion/_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 | 11 | int _putchar(char c) 12 | { 13 | return (write(1, &c, 1)); 14 | } 15 | -------------------------------------------------------------------------------- /0x08-recursion/main.h: -------------------------------------------------------------------------------- 1 | #ifndef main_h 2 | #define main_h 3 | 4 | int _putchar(char c); 5 | void _puts_recursion(char *s); 6 | void _print_rev_recursion(char *s); 7 | int _strlen_recursion(char *s); 8 | int factorial(int n); 9 | int _pow_recursion(int x, int y); 10 | int _sqrt_recursion(int n); 11 | int is_prime_number(int n); 12 | int is_palindrome(char *s); 13 | int wildcmp(char *s1, char *s2); 14 | 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /0x09-static_libraries/0-isupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isupper - checks if parameter is an uppercase character 5 | * @c: input character 6 | * Return: 1 if is an uppercase character, 0 in other case. 7 | */ 8 | 9 | int _isupper(int c) 10 | { 11 | if (c >= 65 && c <= 90) 12 | { 13 | return (1); 14 | } 15 | else 16 | { 17 | return (0); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x09-static_libraries/0-isupper.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Chadkendu/alx-low_level_programming/2b12323b7b7b34d65714cd7410ac1365fde64a9a/0x09-static_libraries/0-isupper.o -------------------------------------------------------------------------------- /0x09-static_libraries/0-memset.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _memset - fills memory with a constant byte 5 | * @s: pointer to put the constant 6 | * @b: constatn 7 | * @n: max bytes to use 8 | * Return: s 9 | */ 10 | 11 | char *_memset(char *s, char b, unsigned int n) 12 | { 13 | unsigned int i; 14 | 15 | for (i = 0; n > 0; i++, n--) 16 | { 17 | s[i] = b; 18 | } 19 | 20 | return (s); 21 | } 22 | -------------------------------------------------------------------------------- /0x09-static_libraries/0-memset.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Chadkendu/alx-low_level_programming/2b12323b7b7b34d65714cd7410ac1365fde64a9a/0x09-static_libraries/0-memset.o -------------------------------------------------------------------------------- /0x09-static_libraries/0-strcat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strcat - concantenates @src to @dest 5 | * @src: the source string to append to @dest 6 | * @dest: the distination string to be concatenated upon 7 | * Return: pointer to the resulting string 8 | */ 9 | 10 | char *_strcat(char *dest, char *src) 11 | { 12 | int index = 0; 13 | int dest_len = 0; 14 | 15 | while (dest[index++]) 16 | dest_len++; 17 | for (index = 0; src[index]; index++) 18 | dest[dest_len++] = src[index]; 19 | return (dest); 20 | } 21 | -------------------------------------------------------------------------------- /0x09-static_libraries/0-strcat.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Chadkendu/alx-low_level_programming/2b12323b7b7b34d65714cd7410ac1365fde64a9a/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: digit 6 | * Return: 1 or 0 7 | */ 8 | 9 | int _isdigit(int c) 10 | { 11 | if (c >= 48 && c <= 57) 12 | { 13 | return (1); 14 | } 15 | else 16 | { 17 | return (0); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x09-static_libraries/1-isdigit.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Chadkendu/alx-low_level_programming/2b12323b7b7b34d65714cd7410ac1365fde64a9a/0x09-static_libraries/1-isdigit.o -------------------------------------------------------------------------------- /0x09-static_libraries/1-memcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _memcpy - copies memory area 5 | * @dest: memory are 6 | * @src: source 7 | * @n:length of src 8 | * 9 | * Return: pointer to dest 10 | */ 11 | 12 | char *_memcpy(char *dest, char *src, unsigned int n) 13 | { 14 | unsigned int i; 15 | 16 | for (i = 0; i < n; i++) 17 | { 18 | dest[i] = src[i]; 19 | } 20 | 21 | return (dest); 22 | } 23 | -------------------------------------------------------------------------------- /0x09-static_libraries/1-memcpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Chadkendu/alx-low_level_programming/2b12323b7b7b34d65714cd7410ac1365fde64a9a/0x09-static_libraries/1-memcpy.o -------------------------------------------------------------------------------- /0x09-static_libraries/1-strncat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strncat - Concatenates two strings using at most 5 | * an inputted number of bytes from src. 6 | * @dest: The string to be appended to dest. 7 | * @src: The string to be appended to dest. 8 | * @n: The number of bytes from src to be appended to dest 9 | * 10 | * Return: A pointer to the resulting string dest. 11 | */ 12 | char *_strncat(char *dest, char *src, int n) 13 | { 14 | int index = 0, dest_len = 0; 15 | 16 | while (dest[index++]) 17 | dest_len++; 18 | for (index = 0; src[index] && index < n; index++) 19 | dest[dest_len++] = src[index]; 20 | return (dest); 21 | } 22 | -------------------------------------------------------------------------------- /0x09-static_libraries/1-strncat.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Chadkendu/alx-low_level_programming/2b12323b7b7b34d65714cd7410ac1365fde64a9a/0x09-static_libraries/1-strncat.o -------------------------------------------------------------------------------- /0x09-static_libraries/100-atoi.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _atoi - convert a string to an integer. 5 | * @s: the string to be comverted. 6 | * 7 | *Return: The integer value of the comverted string. 8 | */ 9 | 10 | int _atoi(char *s) 11 | { 12 | int sign = 1; 13 | unsigned int num = 0; 14 | 15 | do { 16 | 17 | if (*s == '-') 18 | sign *= -1; 19 | 20 | else if (*s >= '0' && *s <= '9') 21 | num = (num * 10) + (*s - '0'); 22 | 23 | else if (num > 0) 24 | break; 25 | } while (*s++); 26 | 27 | return (num * sign); 28 | } 29 | -------------------------------------------------------------------------------- /0x09-static_libraries/100-atoi.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Chadkendu/alx-low_level_programming/2b12323b7b7b34d65714cd7410ac1365fde64a9a/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 to be located 6 | * 7 | * Return: pointer to s or null if not found 8 | */ 9 | char *_strchr(char *s, char c) 10 | { 11 | int index; 12 | 13 | for (index = 0; s[index] >= '\0'; index++) 14 | { 15 | if (s[index] == c) 16 | { 17 | return (s + index); 18 | } 19 | } 20 | 21 | return ('\0'); 22 | } 23 | -------------------------------------------------------------------------------- /0x09-static_libraries/2-strchr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Chadkendu/alx-low_level_programming/2b12323b7b7b34d65714cd7410ac1365fde64a9a/0x09-static_libraries/2-strchr.o -------------------------------------------------------------------------------- /0x09-static_libraries/2-strlen.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strlen - Length of a string 5 | * @s: string 6 | * 7 | * Return: int length of a string 8 | */ 9 | 10 | int _strlen(char *s) 11 | { 12 | int length = 0; 13 | 14 | while (s[length]) 15 | length++; 16 | return (length); 17 | } 18 | -------------------------------------------------------------------------------- /0x09-static_libraries/2-strlen.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Chadkendu/alx-low_level_programming/2b12323b7b7b34d65714cd7410ac1365fde64a9a/0x09-static_libraries/2-strlen.o -------------------------------------------------------------------------------- /0x09-static_libraries/2-strncpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strncpy - Copies at most an inputted number 5 | * of bytes from string src into dest. 6 | * @dest: The buffer storing the string copy. 7 | * @src: The source string. 8 | * @n: The maximum number of bytes to copies from src. 9 | * 10 | * Return: A pointer to the resulting string dest 11 | */ 12 | char *_strncpy(char *dest, char *src, int n) 13 | { 14 | int index = 0, src_len = 0; 15 | 16 | while (src[index++]) 17 | src_len++; 18 | 19 | for (index = 0; src[index] && index < n; index++) 20 | dest[index] = src[index]; 21 | 22 | for (index = src_len; index < n; index++) 23 | dest[index] = '\0'; 24 | 25 | return (dest); 26 | } 27 | -------------------------------------------------------------------------------- /0x09-static_libraries/2-strncpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Chadkendu/alx-low_level_programming/2b12323b7b7b34d65714cd7410ac1365fde64a9a/0x09-static_libraries/2-strncpy.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-islower.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _islower - Entry point 5 | * Description - Checks if a character is a lower case 6 | * @c: the integer value it receives 7 | * Return: 1 if true. 0 if false 8 | */ 9 | int _islower(int c) 10 | { 11 | int i = 'a'; 12 | 13 | for (i = 'a'; i <= 'z'; i++) 14 | { 15 | /*refer to int c*/ 16 | if (c == i) 17 | { 18 | return (1); 19 | } 20 | } 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-islower.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Chadkendu/alx-low_level_programming/2b12323b7b7b34d65714cd7410ac1365fde64a9a/0x09-static_libraries/3-islower.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-puts.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _puts - prints a string and a new line 5 | * @str: pointer to a string to print 6 | * 7 | * Return: 0 8 | */ 9 | 10 | void _puts(char *str) 11 | { 12 | int count = 0; 13 | 14 | while (count >= 0) 15 | { 16 | if (str[count] == '\0') 17 | { 18 | _putchar('\n'); 19 | break; 20 | } 21 | _putchar(str[count]); 22 | count++; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-puts.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Chadkendu/alx-low_level_programming/2b12323b7b7b34d65714cd7410ac1365fde64a9a/0x09-static_libraries/3-puts.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-strcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strcmp - compares pointers to two strings. 5 | * @s1: A poiter to the first string to be compared. 6 | * @s2: A pointer to the second string to be compared. 7 | * 8 | * Return: If str1 < str2, the negative difference of 9 | * the first unmatched characters. 10 | * if s1 == s2, 0. 11 | * if s1 > s2, the positive difference of 12 | * the first unmatched characters. 13 | */ 14 | int _strcmp(char *s1, char *s2) 15 | { 16 | while ((*s1 && *s2) && (*s1 == *s2)) 17 | { 18 | s1++; 19 | s2++; 20 | } 21 | 22 | return (*s1 - *s2); 23 | } 24 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-strcmp.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Chadkendu/alx-low_level_programming/2b12323b7b7b34d65714cd7410ac1365fde64a9a/0x09-static_libraries/3-strcmp.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-strspn.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strspn - gets the length of a prefix substring 4 | * @s: The string to be searched 5 | * @accept: The prefix to be measured. 6 | * 7 | * Return: The number of bytes in s which 8 | * consist only of bytes from accept 9 | */ 10 | 11 | unsigned int _strspn(char *s, char *accept) 12 | { 13 | unsigned int bytes = 0; 14 | int index; 15 | 16 | while (*s) 17 | { 18 | for (index = 0; accept[index]; index++) 19 | { 20 | if (*s == accept[index]) 21 | { 22 | bytes++; 23 | break; 24 | } 25 | 26 | else if (accept[index + 1] == '\0') 27 | return (bytes); 28 | } 29 | 30 | s++; 31 | } 32 | 33 | return (bytes); 34 | } 35 | 36 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-strspn.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Chadkendu/alx-low_level_programming/2b12323b7b7b34d65714cd7410ac1365fde64a9a/0x09-static_libraries/3-strspn.o -------------------------------------------------------------------------------- /0x09-static_libraries/4-isalpha.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isalpha - Entry Point 5 | * @c: the integer value it receives 6 | * Description: checks if alphabet is lowercase or uppercase 7 | * Return: 1 if true. 0 if false 8 | */ 9 | int _isalpha(int c) 10 | { 11 | if (c >= 'a' && c <= 'z') 12 | { 13 | return (1); 14 | } 15 | else if (c >= 'A' && c <= 'Z') 16 | { 17 | return (1); 18 | } 19 | else 20 | { 21 | return (0); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /0x09-static_libraries/4-isalpha.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Chadkendu/alx-low_level_programming/2b12323b7b7b34d65714cd7410ac1365fde64a9a/0x09-static_libraries/4-isalpha.o -------------------------------------------------------------------------------- /0x09-static_libraries/4-strpbrk.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strpbrk - searches a string for any of a set of bytes 4 | * @s: The string to be searched 5 | * @accept: The set of bytes to be searched for. 6 | * 7 | * Return: If a set is matched - a pointer to the matched byte. 8 | * If no set is matched - NULL 9 | */ 10 | 11 | char *_strpbrk(char *s, char *accept) 12 | { 13 | int index; 14 | 15 | while (*s) 16 | { 17 | for (index = 0; accept[index]; index++) 18 | { 19 | if (*s == accept[index]) 20 | return (s); 21 | } 22 | 23 | s++; 24 | } 25 | return ('\0'); 26 | } 27 | -------------------------------------------------------------------------------- /0x09-static_libraries/4-strpbrk.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Chadkendu/alx-low_level_programming/2b12323b7b7b34d65714cd7410ac1365fde64a9a/0x09-static_libraries/4-strpbrk.o -------------------------------------------------------------------------------- /0x09-static_libraries/5-strstr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strstr - locates a substring 4 | * @haystrack: The string to be searched 5 | * @needle: The substring to be located 6 | * 7 | * Return: If a substring is located - a pointer 8 | * to the beginning of the located substring 9 | * If the substring is not located - NULL 10 | */ 11 | 12 | char *_strstr(char *haystrack, char *needle) 13 | { 14 | int index; 15 | 16 | if (*needle == 0) 17 | { 18 | return (haystrack); 19 | } 20 | 21 | while (*haystrack) 22 | { 23 | index = 0; 24 | 25 | if (haystrack[index] == needle[index]) 26 | { 27 | do { 28 | if (needle[index + 1] == '\0') 29 | return (haystrack); 30 | 31 | index++; 32 | 33 | } while (haystrack[index] == needle[index]); 34 | } 35 | 36 | haystrack++; 37 | } 38 | 39 | return ('\0'); 40 | } 41 | -------------------------------------------------------------------------------- /0x09-static_libraries/5-strstr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Chadkendu/alx-low_level_programming/2b12323b7b7b34d65714cd7410ac1365fde64a9a/0x09-static_libraries/5-strstr.o -------------------------------------------------------------------------------- /0x09-static_libraries/6-abs.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _abs - Entry point 5 | * @n: n is an integer 6 | * Description: Prints the absolute value of an integer 7 | * Return: int 8 | */ 9 | 10 | int _abs(int n) 11 | { 12 | if (n > 0) 13 | { 14 | return (n); 15 | } 16 | else if (n < 0) 17 | { 18 | return (-n); 19 | } 20 | else 21 | { 22 | return (n); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /0x09-static_libraries/6-abs.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Chadkendu/alx-low_level_programming/2b12323b7b7b34d65714cd7410ac1365fde64a9a/0x09-static_libraries/6-abs.o -------------------------------------------------------------------------------- /0x09-static_libraries/9-strcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * *_strcpy - copies the string pointed to by src 5 | * @dest: char type string 6 | * @src: char type string 7 | * Return: Pointer to `dest 8 | */ 9 | 10 | char *_strcpy(char *dest, char *src) 11 | { 12 | int i = -1; 13 | 14 | do { 15 | i++; 16 | dest[i] = src[i]; 17 | } while (src[i] != '\0'); 18 | return (dest); 19 | } 20 | -------------------------------------------------------------------------------- /0x09-static_libraries/9-strcpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Chadkendu/alx-low_level_programming/2b12323b7b7b34d65714cd7410ac1365fde64a9a/0x09-static_libraries/9-strcpy.o -------------------------------------------------------------------------------- /0x09-static_libraries/README.md: -------------------------------------------------------------------------------- 1 | c staic libary 2 | -------------------------------------------------------------------------------- /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 | 11 | int _putchar(char c) 12 | { 13 | return (write(1, &c, 1)); 14 | } 15 | -------------------------------------------------------------------------------- /0x09-static_libraries/_putchar.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Chadkendu/alx-low_level_programming/2b12323b7b7b34d65714cd7410ac1365fde64a9a/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/Chadkendu/alx-low_level_programming/2b12323b7b7b34d65714cd7410ac1365fde64a9a/0x09-static_libraries/libmy.a -------------------------------------------------------------------------------- /0x09-static_libraries/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | int _putchar(char c); 5 | int _islower(int c); 6 | int _isalpha(int c); 7 | int _abs(int n); 8 | int _isupper(int c); 9 | int _isdigit(int c); 10 | int _strlen(char *s); 11 | void _puts(char *s); 12 | char *_strcpy(char *dest, char *src); 13 | int _atoi(char *s); 14 | char *_strcat(char *dest, char *src); 15 | char *_strncat(char *dest, char *src, int n); 16 | char *_strncpy(char *dest, char *src, int n); 17 | int _strcmp(char *s1, char *s2); 18 | char *_memset(char *s, char b, unsigned int n); 19 | char *_memcpy(char *dest, char *src, unsigned int n); 20 | char *_strchr(char *s, char c); 21 | unsigned int _strspn(char *s, char *accept); 22 | char *_strpbrk(char *s, char *accept); 23 | char *_strstr(char *haystack, char *needle); 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /0x0A-argc_argv/0-whatsmyname.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * main - Prints the name of the program 6 | * @argc: Number of command line arguments 7 | * @argv: Array name 8 | * Return: 0 9 | */ 10 | 11 | int main(int argc, char *argv[]) 12 | { 13 | printf("%s\n", argv[argc * 0]); 14 | 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x0A-argc_argv/1-args.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * main - Prints the number of arguments passed into it 6 | * @argc: Argument count 7 | * @argv: Argument vector 8 | * Return: Always zero 9 | */ 10 | 11 | int main(int argc, char *argv[]) 12 | { 13 | int i; 14 | 15 | 16 | if (argc == 1) 17 | printf("%d\n", argc - 1); 18 | 19 | else 20 | { 21 | for (i = 0; *argv; i++, argv++) 22 | ; 23 | 24 | printf("%d\n", i - 1); 25 | } 26 | 27 | return (0); 28 | } 29 | -------------------------------------------------------------------------------- /0x0A-argc_argv/100-change.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * main - prints the minimum number of coins to make change for an amount. 6 | * of money. 7 | * @argc: number of command line arguments. 8 | * @argv: array that contains the program command line arguments. 9 | * Return: 0 - success. 10 | */ 11 | 12 | int main(int argc, char *argv[]) 13 | { 14 | int cents, ncoins = 0; 15 | 16 | if (argc == 1 || argc > 2) 17 | { 18 | printf("Error\n"); 19 | return (1); 20 | } 21 | 22 | cents = atoi(argv[1]); 23 | 24 | while (cents > 0) 25 | { 26 | if (cents >= 25) 27 | cents -= 25; 28 | else if (cents >= 10) 29 | cents -= 10; 30 | else if (cents >= 5) 31 | cents -= 5; 32 | else if (cents >= 2) 33 | cents -= 2; 34 | else if (cents >= 1) 35 | cents -= 1; 36 | ncoins += 1; 37 | } 38 | printf("%d\n", ncoins); 39 | return (0); 40 | } 41 | -------------------------------------------------------------------------------- /0x0A-argc_argv/2-args.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * main - Prints all the arguments it receives 6 | * @argc: Argument count 7 | * @argv: Argument vector 8 | * Return: Always 0 9 | */ 10 | 11 | int main(int argc, char *argv[]) 12 | { 13 | int i; 14 | 15 | for (i = 0; i < argc; i++) 16 | printf("%s\n", argv[i]); 17 | 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /0x0A-argc_argv/3-mul.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "main.h" 4 | 5 | /** 6 | * main - Multiplies first integer arguments 7 | * @argc: Number of command line arguments 8 | * @argv: Array name 9 | * Return: 1 if not enough arguments passed in. 0 otherwise 10 | */ 11 | 12 | int main(int argc, char *argv[]) 13 | { 14 | int i, j; 15 | 16 | if (argc == 1 || argc == 2) 17 | { 18 | printf("Error\n"); 19 | return (1); 20 | } 21 | 22 | else 23 | { 24 | j = 1; 25 | 26 | for (i = 1; i < 3; i++) 27 | j *= atoi(argv[i]); 28 | 29 | printf("%d\n", j); 30 | } 31 | 32 | return (0); 33 | } 34 | -------------------------------------------------------------------------------- /0x0A-argc_argv/4-add.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "main.h" 6 | 7 | /** 8 | * main - Program that takes in all integr arguments and returns the sum 9 | * @argc: Number of command line arguments 10 | * @argv: Array name 11 | * Return: 1 if a non-integer is among the passed in arguments, 0 otherwise 12 | */ 13 | 14 | int main(int argc, char *argv[]) 15 | { 16 | int i, j, length, sum; 17 | char *ptr; 18 | 19 | if (argc < 2) 20 | printf("0\n"); 21 | 22 | else 23 | { 24 | sum = 0; 25 | for (i = 1; i < argc; i++) 26 | { 27 | ptr = argv[i]; 28 | length = strlen(ptr); 29 | 30 | for (j = 0; j < length; j++) 31 | { 32 | if (isdigit(*(ptr + j)) == 0) 33 | { 34 | printf("Error\n"); 35 | return (1); 36 | } 37 | } 38 | 39 | sum += atoi(argv[i]); 40 | } 41 | 42 | printf("%d\n", sum); 43 | } 44 | 45 | return (0); 46 | } 47 | -------------------------------------------------------------------------------- /0x0A-argc_argv/README.md: -------------------------------------------------------------------------------- 1 | C ARGC AND ARGV[] 2 | -------------------------------------------------------------------------------- /0x0A-argc_argv/main.h: -------------------------------------------------------------------------------- 1 | #ifndef HOLBERTON_H 2 | #define HOLBERTON_H 3 | 4 | int _putchar(char c); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /0x0B-malloc_free/0-create_array.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * create_array - Creates an array of chars and initializes it 6 | * with a specific char. 7 | * @size: The size of the array to be initialized. 8 | * @c: The specific char to initialize the array with. 9 | * 10 | * Return: If size == 0 or the function fails - NULL 11 | * otherwise - a pointer to the array 12 | */ 13 | 14 | char *create_array(unsigned int size, char c) 15 | { 16 | char *array; 17 | unsigned int index; 18 | 19 | if (size == 0) 20 | return (NULL); 21 | 22 | array = malloc(sizeof(char) * size); 23 | 24 | if (array == NULL) 25 | return (NULL); 26 | 27 | for (index = 0; index < size; index++) 28 | array[index] = c; 29 | 30 | return (array); 31 | } 32 | -------------------------------------------------------------------------------- /0x0B-malloc_free/1-strdup.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * _strdup - Returns a pointer to anewly -allocated space in memory 6 | * containing a copy of the string given as parameter. 7 | * @str: The string to be copied . 8 | * 9 | * Return: If str == NULL or insufficient memory is avalable - NULL. 10 | * Otherwise - a pointer to the duplicated string. 11 | */ 12 | 13 | char *_strdup(char *str) 14 | { 15 | char *duplicate; 16 | int index, len = 0; 17 | 18 | if (str == NULL) 19 | return (NULL); 20 | 21 | for (index = 0; str[index]; index++) 22 | len++; 23 | 24 | duplicate = malloc(sizeof(char) * (len + 1)); 25 | 26 | if (duplicate == NULL) 27 | return (NULL); 28 | 29 | for (index = 0; str[index]; index++) 30 | duplicate[index] = str[index]; 31 | 32 | duplicate[len] = '\0'; 33 | 34 | return (duplicate); 35 | } 36 | -------------------------------------------------------------------------------- /0x0B-malloc_free/100-argstostr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * argstostr - Concatenates all arguments of the program into a string; 6 | * arguments are seperated by a new line in the string. 7 | * @ac: The number of arguments passed to the program. 8 | * @av: The array of pointers to the arguments. 9 | * 10 | * Return: If ac == 0, av == NULL, or the function fails - NULL. 11 | * Otherwise - a pointer to the new string. 12 | * 13 | */ 14 | 15 | char *argstostr(int ac, char **av) 16 | { 17 | char *str; 18 | int arg, byte, index, size = ac; 19 | 20 | if (ac == 0 || av == NULL) 21 | return (NULL); 22 | 23 | for (arg = 0; arg < ac; arg++) 24 | { 25 | for (byte = 0; av[arg][byte]; byte++) 26 | size++; 27 | } 28 | 29 | str = malloc(sizeof(char) * size + 1); 30 | 31 | if (str == NULL) 32 | return (NULL); 33 | 34 | index = 0; 35 | 36 | for (arg = 0; arg < ac; arg++) 37 | { 38 | for (byte = 0; av[arg][byte]; byte++) 39 | str[index++] = av[arg][byte]; 40 | 41 | 42 | str[index++] = '\n'; 43 | } 44 | 45 | str[size] = '\0'; 46 | 47 | return (str); 48 | } 49 | -------------------------------------------------------------------------------- /0x0B-malloc_free/101-strtow.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | int word_len(char *str); 5 | int count_words(char *str); 6 | char **strtow(char *str); 7 | 8 | /** 9 | * word_len - Locates the index marking the end of the first 10 | * word contained withing a string. 11 | * @str: The string to be searched. 12 | * 13 | * Return: The index marking the end of the initial word pointed 14 | * to by str; 15 | */ 16 | 17 | int word_len(char *str) 18 | { 19 | int index = 0, len = 0; 20 | 21 | while (*(str + index) && *(str + index) != ' ') 22 | { 23 | len++; 24 | index++; 25 | } 26 | 27 | return (len); 28 | } 29 | 30 | /** 31 | * count_words - Counts the number of words contained withing a string 32 | * @str: The string to be searched. 33 | * 34 | * Return: The number of words contained withing str. 35 | */ 36 | 37 | int count_words(char *str) 38 | { 39 | int index = 0, words = 0, len = 0; 40 | 41 | for (index = 0; *(str + index); index++) 42 | len++; 43 | 44 | for (index = 0; index < len; index++) 45 | { 46 | if (*(str + index) != ' ') 47 | { 48 | words++; 49 | index += word_len(str + index); 50 | } 51 | } 52 | 53 | return (words); 54 | } 55 | 56 | /** 57 | * strtow - Splits a string into words. 58 | * @str: The string to be split. 59 | * 60 | * Return: If str = NULL, str = "", or the function fails -NULL. 61 | * Otherwise - a pointer to an array of strings (words). 62 | */ 63 | 64 | char **strtow(char *str) 65 | { 66 | char **strings; 67 | int index = 0, words, w, letters, l; 68 | 69 | if (str == NULL || str[0] == '\0') 70 | return (NULL); 71 | 72 | words = count_words(str); 73 | if (words == 0) 74 | return (NULL); 75 | 76 | strings = malloc(sizeof(char *) * (words + 1)); 77 | if (strings == NULL) 78 | return (NULL); 79 | 80 | for (w = 0; w < words; w++) 81 | { 82 | while (str[index] == ' ') 83 | index++; 84 | 85 | letters = word_len(str + index); 86 | 87 | strings[w] = malloc(sizeof(char) * (letters + 1)); 88 | 89 | if (strings[w] == NULL) 90 | { 91 | for (; w >= 0; w--) 92 | free(strings[w]); 93 | 94 | free(strings); 95 | return (NULL); 96 | } 97 | 98 | for (l = 0; l < letters; l++) 99 | strings[w][l] = str[index++]; 100 | 101 | strings[w][l] = '\0'; 102 | } 103 | strings[w] = NULL; 104 | 105 | return (strings); 106 | } 107 | -------------------------------------------------------------------------------- /0x0B-malloc_free/2-str_concat.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * str_concat - Concatenates two strings. 6 | * @s1: The string to be concatenated upon 7 | * @s2: The string to be concatenated to s1 8 | * 9 | * Return: If concatenation fails - NULL. 10 | * otherwise - a pointer the newly - allocted space in memory 11 | * containing the concatenated strings. 12 | * 13 | */ 14 | 15 | char *str_concat(char *s1, char *s2) 16 | { 17 | char *concat_str; 18 | int index, concat_index = 0, len = 0; 19 | 20 | if (s1 == NULL) 21 | s1 = ""; 22 | 23 | if (s2 == NULL) 24 | s2 = ""; 25 | 26 | for (index = 0; s1[index] || s2[index]; index++) 27 | len++; 28 | 29 | concat_str = malloc(sizeof(char) * len); 30 | 31 | if (concat_str == NULL) 32 | return (NULL); 33 | 34 | for (index = 0; s1[index]; index++) 35 | concat_str[concat_index++] = s1[index]; 36 | 37 | for (index = 0; s2[index]; index++) 38 | concat_str[concat_index++] = s2[index]; 39 | 40 | return (concat_str); 41 | } 42 | 43 | -------------------------------------------------------------------------------- /0x0B-malloc_free/3-alloc_grid.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * alloc_grid - Returns a pointer to a 2-dimensional array of integers 6 | * with each element initialized to 0. 7 | * @width: The width of the 2-dimensional array. 8 | * @height: The height of the 2-dimensional array. 9 | * 10 | * Return: If width <= 0, height <= 0, or the function fails - NULL. 11 | * Otherwise - a pointer to the 2-dimensional array of integers. 12 | */ 13 | 14 | int **alloc_grid(int width, int height) 15 | { 16 | int **twoD; 17 | int hgt_index, wid_index; 18 | 19 | if (width <= 0 || height <= 0) 20 | return (NULL); 21 | 22 | twoD = malloc(sizeof(int *) * height); 23 | 24 | if (twoD == NULL) 25 | return (NULL); 26 | 27 | for (hgt_index = 0; hgt_index < height; hgt_index++) 28 | { 29 | twoD[hgt_index] = malloc(sizeof(int) * width); 30 | 31 | if (twoD[hgt_index] == NULL) 32 | { 33 | for (; hgt_index >= 0; hgt_index--) 34 | free(twoD[hgt_index]); 35 | 36 | free(twoD); 37 | return (NULL); 38 | } 39 | } 40 | 41 | for (hgt_index = 0; hgt_index < height; hgt_index++) 42 | { 43 | for (wid_index = 0; wid_index < width; wid_index++) 44 | twoD[hgt_index][wid_index] = 0; 45 | } 46 | 47 | return (twoD); 48 | } 49 | -------------------------------------------------------------------------------- /0x0B-malloc_free/4-free_grid.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * *free_grid - Frees a 2-dimensional array of integers. 6 | * @grid: The 2-dimensional array of integers to be freed. 7 | * @height: The height of grid. 8 | */ 9 | 10 | void free_grid(int **grid, int height) 11 | { 12 | int index; 13 | 14 | for (index = 0; index < height; index++) 15 | free(grid[index]); 16 | 17 | free(grid); 18 | } 19 | 20 | -------------------------------------------------------------------------------- /0x0B-malloc_free/README.md: -------------------------------------------------------------------------------- 1 | MALLOC | FREE 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 **strtow(char *str); 8 | char *argstostr(int ac, char **av); 9 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/0-malloc_checked.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * malloc_checked - A function that allocates memory using malloc 6 | * @b: The number of bytes to be allocated 7 | * 8 | * Return: A pointer to the allocated memory 9 | */ 10 | 11 | void *malloc_checked(unsigned int b) 12 | { 13 | void *memry = malloc(b); 14 | 15 | if (memry == NULL) 16 | exit(98); 17 | 18 | return (memry); 19 | } 20 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/0x0C-more_malloc_free: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * malloc_checked - A function that allocates memory using malloc 6 | * @b: The number of bytes to be allocated 7 | * 8 | * Return: A pointer to the allocated memory 9 | */ 10 | 11 | void *malloc_checked(unsigned int b) 12 | { 13 | void *memry = malloc(b); 14 | 15 | if (memry == NULL) 16 | exit(98); 17 | 18 | return (memry); 19 | } 20 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/1-string_nconcat.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * string_nconcat - Concatenates two strings using at most 6 | * an inputted number of bytes. 7 | * @s1: The first string 8 | * @s2: The second string 9 | * @n: The maximum number of bytes of s2 to concatenates to s1 10 | * 11 | * Return: If the function fails - NULL 12 | * otherwiser a pointer to the concatenated space in memory. 13 | * 14 | */ 15 | 16 | char *string_nconcat(char *s1, char *s2, unsigned int n) 17 | { 18 | char *concat; 19 | unsigned int len = n, index; 20 | 21 | if (s1 == NULL) 22 | s1 = ""; 23 | 24 | if (s2 == NULL) 25 | s2 = ""; 26 | 27 | for (index = 0; s1[index]; index++) 28 | len++; 29 | 30 | concat = malloc(sizeof(char) * (len + 1)); 31 | 32 | if (concat == NULL) 33 | return (NULL); 34 | 35 | len = 0; 36 | 37 | for (index = 0; s1[index]; index++) 38 | concat[len++] = s1[index]; 39 | 40 | for (index = 0; s2[index] && index < n; index++) 41 | concat[len++] = s2[index]; 42 | 43 | concat[len] = '\0'; 44 | 45 | return (concat); 46 | } 47 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/100-realloc.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _realloc - Reallocates a memory block using malloc and free. 6 | * @ptr: A pointer to the memory previously allocated. 7 | * @old_size: The size in bytes of the allocated space for ptr. 8 | * @new_size: The size in bytes for the new memory block. 9 | * 10 | * Return: If new_size == old_size - ptr. 11 | * If new_size == 0 and ptr is not NULL - NULL. 12 | * Otherwise - a pointer to the reallocated memory block. 13 | */ 14 | void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size) 15 | { 16 | void *mem; 17 | char *ptr_copy, *filler; 18 | unsigned int index; 19 | 20 | if (new_size == old_size) 21 | return (ptr); 22 | 23 | if (ptr == NULL) 24 | { 25 | mem = malloc(new_size); 26 | 27 | if (mem == NULL) 28 | return (NULL); 29 | 30 | return (mem); 31 | } 32 | 33 | if (new_size == 0 && ptr != NULL) 34 | { 35 | free(ptr); 36 | return (NULL); 37 | } 38 | 39 | ptr_copy = ptr; 40 | mem = malloc(sizeof(*ptr_copy) * new_size); 41 | 42 | if (mem == NULL) 43 | { 44 | free(ptr); 45 | return (NULL); 46 | } 47 | 48 | filler = mem; 49 | 50 | for (index = 0; index < old_size && index < new_size; index++) 51 | filler[index] = *ptr_copy++; 52 | 53 | free(ptr); 54 | return (mem); 55 | } 56 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/101-mul.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | 5 | int find_len(char *str); 6 | char *create_xarray(int size); 7 | char *iterate_zeroes(char *str); 8 | void get_prod(char *prod, char *mult, int digit, int zeroes); 9 | void add_nums(char *final_prod, char *next_prod, int next_len); 10 | 11 | /** 12 | * find_len - Finds the length of a string. 13 | * @str: The string to be measured. 14 | * 15 | * Return: The length of the string. 16 | */ 17 | int find_len(char *str) 18 | { 19 | int len = 0; 20 | 21 | while (*str++) 22 | len++; 23 | 24 | return (len); 25 | } 26 | 27 | /** 28 | * create_xarray - Creates an array of chars and initializes it with 29 | * the character 'x'. Adds a terminating null byte. 30 | * @size: The size of the array to be initialized. 31 | * 32 | * Description: If there is insufficient space, the 33 | * function exits with a status of 98. 34 | * Return: A pointer to the array. 35 | */ 36 | char *create_xarray(int size) 37 | { 38 | char *array; 39 | int index; 40 | 41 | array = malloc(sizeof(char) * size); 42 | 43 | if (array == NULL) 44 | exit(98); 45 | 46 | for (index = 0; index < (size - 1); index++) 47 | array[index] = 'x'; 48 | 49 | array[index] = '\0'; 50 | 51 | return (array); 52 | } 53 | 54 | /** 55 | * iterate_zeroes - Iterates through a string of numbers containing 56 | * leading zeroes until it hits a non-zero number. 57 | * @str: The string of numbers to be iterate through. 58 | * 59 | * Return: A pointer to the next non-zero element. 60 | */ 61 | char *iterate_zeroes(char *str) 62 | { 63 | while (*str && *str == '0') 64 | str++; 65 | 66 | return (str); 67 | } 68 | 69 | /** 70 | * get_digit - Converts a digit character to a corresponding int. 71 | * @c: The character to be converted. 72 | * 73 | * Description: If c is a non-digit, the function 74 | * exits with a status of 98. 75 | * Return: The converted int. 76 | */ 77 | int get_digit(char c) 78 | { 79 | int digit = c - '0'; 80 | 81 | if (digit < 0 || digit > 9) 82 | { 83 | printf("Error\n"); 84 | exit(98); 85 | } 86 | 87 | return (digit); 88 | } 89 | 90 | /** 91 | * get_prod - Multiplies a string of numbers by a single digit. 92 | * @prod: The buffer to store the result. 93 | * @mult: The string of numbers. 94 | * @digit: The single digit. 95 | * @zeroes: The necessary number of leading zeroes. 96 | * 97 | * Description: If mult contains a non-digit, the function 98 | * exits with a status value of 98. 99 | */ 100 | void get_prod(char *prod, char *mult, int digit, int zeroes) 101 | { 102 | int mult_len, num, tens = 0; 103 | 104 | mult_len = find_len(mult) - 1; 105 | mult += mult_len; 106 | 107 | while (*prod) 108 | { 109 | *prod = 'x'; 110 | prod++; 111 | } 112 | 113 | prod--; 114 | 115 | while (zeroes--) 116 | { 117 | *prod = '0'; 118 | prod--; 119 | } 120 | 121 | for (; mult_len >= 0; mult_len--, mult--, prod--) 122 | { 123 | if (*mult < '0' || *mult > '9') 124 | { 125 | printf("Error\n"); 126 | exit(98); 127 | } 128 | 129 | num = (*mult - '0') * digit; 130 | num += tens; 131 | *prod = (num % 10) + '0'; 132 | tens = num / 10; 133 | } 134 | 135 | if (tens) 136 | *prod = (tens % 10) + '0'; 137 | } 138 | 139 | /** 140 | * add_nums - Adds the numbers stored in two strings. 141 | * @final_prod: The buffer storing the running final product. 142 | * @next_prod: The next product to be added. 143 | * @next_len: The length of next_prod. 144 | */ 145 | void add_nums(char *final_prod, char *next_prod, int next_len) 146 | { 147 | int num, tens = 0; 148 | 149 | while (*(final_prod + 1)) 150 | final_prod++; 151 | 152 | while (*(next_prod + 1)) 153 | next_prod++; 154 | 155 | for (; *final_prod != 'x'; final_prod--) 156 | { 157 | num = (*final_prod - '0') + (*next_prod - '0'); 158 | num += tens; 159 | *final_prod = (num % 10) + '0'; 160 | tens = num / 10; 161 | 162 | next_prod--; 163 | next_len--; 164 | } 165 | 166 | for (; next_len >= 0 && *next_prod != 'x'; next_len--) 167 | { 168 | num = (*next_prod - '0'); 169 | num += tens; 170 | *final_prod = (num % 10) + '0'; 171 | tens = num / 10; 172 | 173 | final_prod--; 174 | next_prod--; 175 | } 176 | 177 | if (tens) 178 | *final_prod = (tens % 10) + '0'; 179 | } 180 | 181 | /** 182 | * main - Multiplies two positive numbers. 183 | * @argv: The number of arguments passed to the program. 184 | * @argc: An array of pointers to the arguments. 185 | * 186 | * Description: If the number of arguments is incorrect or one number 187 | * contains non-digits, the function exits with a status of 98. 188 | * Return: Always 0. 189 | */ 190 | int main(int argc, char *argv[]) 191 | { 192 | char *final_prod, *next_prod; 193 | int size, index, digit, zeroes = 0; 194 | 195 | if (argc != 3) 196 | { 197 | printf("Error\n"); 198 | exit(98); 199 | } 200 | 201 | if (*(argv[1]) == '0') 202 | argv[1] = iterate_zeroes(argv[1]); 203 | if (*(argv[2]) == '0') 204 | argv[2] = iterate_zeroes(argv[2]); 205 | if (*(argv[1]) == '\0' || *(argv[2]) == '\0') 206 | { 207 | printf("0\n"); 208 | return (0); 209 | } 210 | 211 | size = find_len(argv[1]) + find_len(argv[2]); 212 | final_prod = create_xarray(size + 1); 213 | next_prod = create_xarray(size + 1); 214 | 215 | for (index = find_len(argv[2]) - 1; index >= 0; index--) 216 | { 217 | digit = get_digit(*(argv[2] + index)); 218 | get_prod(next_prod, argv[1], digit, zeroes++); 219 | add_nums(final_prod, next_prod, size - 1); 220 | } 221 | for (index = 0; final_prod[index]; index++) 222 | { 223 | if (final_prod[index] != 'x') 224 | putchar(final_prod[index]); 225 | } 226 | putchar('\n'); 227 | 228 | free(next_prod); 229 | free(final_prod); 230 | 231 | return (0); 232 | } 233 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/2-calloc.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * _calloc - Allocates memory for an array of a certain number 6 | * of elements each of an inputted byte size. 7 | * @nmemb: The number of elements 8 | * @size: The bytes size of each array element 9 | * 10 | * Return: If nmemb = 0, size = 0, or the function fails - NULL 11 | * Otherwise - a pointer to the allocated memory. 12 | * 13 | */ 14 | 15 | void *_calloc(unsigned int nmemb, unsigned int size) 16 | { 17 | void *mem; 18 | char *filler; 19 | unsigned int index; 20 | 21 | if (nmemb == 0 || size == 0) 22 | return (NULL); 23 | 24 | mem = malloc(size * nmemb); 25 | 26 | if (mem == NULL) 27 | return (NULL); 28 | 29 | filler = mem; 30 | 31 | for (index = 0; index < (size * nmemb); index++) 32 | filler[index] = '\0'; 33 | 34 | return (mem); 35 | } 36 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/3-array_range.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * array_range - Creates an array of integers ordered from 6 | * mim to max, inclusive 7 | * @min: The first value of the array 8 | * @max: The last value of the array 9 | * 10 | * Return: If min > max or the function fails - NULL 11 | * Otherwise - a pointer to the newly created array 12 | * 13 | */ 14 | 15 | int *array_range(int min, int max) 16 | { 17 | int *array, index, size; 18 | 19 | if (min > max) 20 | return (NULL); 21 | 22 | size = max - min + 1; 23 | 24 | array = malloc(sizeof(int) * size); 25 | 26 | if (array == NULL) 27 | return (NULL); 28 | 29 | for (index = 0; index < size; index++) 30 | array[index] = min++; 31 | 32 | return (array); 33 | } 34 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/README.md: -------------------------------------------------------------------------------- 1 | C MRE MALLOC AD FREE 2 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/_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 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/git.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | git add . 3 | echo "Enter your commit message: " 4 | read commit_message 5 | git commit -m "$commit_message" 6 | git push 7 | echo -e "\ngit push successful" 8 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/main.h: -------------------------------------------------------------------------------- 1 | int _putchar(char c); 2 | void *malloc_checked(unsigned int b); 3 | char *string_nconcat(char *s1, char *s2, unsigned int n); 4 | void *_calloc(unsigned int nmemb, unsigned int size); 5 | int *array_range(int min, int max); 6 | void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size); 7 | -------------------------------------------------------------------------------- /0x0D-preprocessor/0-object_like_macro.h: -------------------------------------------------------------------------------- 1 | #ifndef OBJECT_LIKE_MACRO_H 2 | #define OBJECT_LIKE_MACRO_H 3 | 4 | /** 5 | * File: 0-object_like_macro.h 6 | * Desc: Header file that defines a macro named 7 | * SIZE as an abbreviation for the token 1024 8 | */ 9 | 10 | #define SIZE 1024 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /0x0D-preprocessor/1-pi.h: -------------------------------------------------------------------------------- 1 | #ifndef PI_H 2 | #define PI_H 3 | 4 | /** 5 | * File - A macro named PI 6 | * Desc: As an abbreviation for the tokene 7 | */ 8 | 9 | #define PI 3.14159265359 10 | 11 | #endif 12 | -------------------------------------------------------------------------------- /0x0D-preprocessor/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Program that prints the file name of its source code 5 | * Return: 0 6 | */ 7 | 8 | int main(void) 9 | { 10 | printf("%s\n", __FILE__); 11 | 12 | return (0); 13 | } 14 | -------------------------------------------------------------------------------- /0x0D-preprocessor/3-function_like_macro.h: -------------------------------------------------------------------------------- 1 | #ifndef FUNCTION_LIKE_MACRO_H 2 | #define FUNCTION_LIKE_MACRO_H 3 | 4 | /** 5 | * File: 3-function_like_macro.h 6 | * Desc: Header file that defines a function-like macro ABS(x) 7 | * that computes the absolute number of x 8 | * @x: the number to find its absolute value 9 | */ 10 | 11 | #define ABS(x) ((x) < (0) ? ((x) * (-1)) : (x)) 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /0x0D-preprocessor/4-sum.h: -------------------------------------------------------------------------------- 1 | #ifndef SUM_H 2 | #define SUM_H 3 | 4 | /** 5 | * File - a macro that adds two integers 6 | * Desc: Add two integer 7 | * @x: first integer 8 | * @y: second integer 9 | */ 10 | 11 | #define SUM(x, y) ((x) + (y)) 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /0x0D-preprocessor/README.md: -------------------------------------------------------------------------------- 1 | C PREPROCESSOR 2 | -------------------------------------------------------------------------------- /0x0D-preprocessor/main.h: -------------------------------------------------------------------------------- 1 | int _putchar(char c); 2 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/1-init_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | 4 | /** 5 | * init_dog - Initializes a variable of type struct dog. 6 | * @d: The dog to be initialized. 7 | * @name: The name of the dog 8 | * @age: The age of the dog 9 | * @owner: The owner of the dog. 10 | */ 11 | 12 | void init_dog(struct dog *d, char *name, float age, char *owner) 13 | { 14 | if (d != NULL) 15 | { 16 | d->name = name; 17 | d->age = age; 18 | d->owner = owner; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/2-print_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | 4 | /** 5 | * print_dog - prints a struct dog. 6 | * @d: struct dog. 7 | * 8 | * Return: no return. 9 | */ 10 | 11 | void print_dog(struct dog *d) 12 | { 13 | if (d) 14 | { 15 | if (!(d->name)) 16 | printf("Name: (nil)\n"); 17 | else 18 | printf("Name: %s\n", d->name); 19 | 20 | printf("Age: %f\n", d->age); 21 | 22 | if (!(d->owner)) 23 | printf("Owner: (nil)\n"); 24 | else 25 | printf("Owner: %s\n", d->owner); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/4-new_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | 4 | /** 5 | * new_dog - creates a new dog. 6 | * @name: name of the dog. 7 | * @age: age of the dog. 8 | * @owner: owner of the dog. 9 | * 10 | * Return: struct dog. 11 | * if fails, returns NULL. 12 | */ 13 | dog_t *new_dog(char *name, float age, char *owner) 14 | { 15 | dog_t *p_dog; 16 | int i, lname, lowner; 17 | 18 | p_dog = malloc(sizeof(*p_dog)); 19 | if (p_dog == NULL || !(name) || !(owner)) 20 | { 21 | free(p_dog); 22 | return (NULL); 23 | } 24 | 25 | for (lname = 0; name[lname]; lname++) 26 | ; 27 | 28 | for (lowner = 0; owner[lowner]; lowner++) 29 | ; 30 | 31 | p_dog->name = malloc(lname + 1); 32 | p_dog->owner = malloc(lowner + 1); 33 | 34 | if (!(p_dog->name) || !(p_dog->owner)) 35 | { 36 | free(p_dog->owner); 37 | free(p_dog->name); 38 | free(p_dog); 39 | return (NULL); 40 | } 41 | 42 | for (i = 0; i < lname; i++) 43 | p_dog->name[i] = name[i]; 44 | p_dog->name[i] = '\0'; 45 | 46 | p_dog->age = age; 47 | 48 | for (i = 0; i < lowner; i++) 49 | p_dog->owner[i] = owner[i]; 50 | p_dog->owner[i] = '\0'; 51 | 52 | return (p_dog); 53 | } 54 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/5-free_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | 4 | /** 5 | * free_dog - frees dogs. 6 | * @d: struct dog. 7 | * 8 | * Return: no return. 9 | */ 10 | 11 | void free_dog(dog_t *d) 12 | { 13 | if (d) 14 | { 15 | free(d->name); 16 | free(d->owner); 17 | free(d); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/README.md: -------------------------------------------------------------------------------- 1 | C PROGRAMMING ( STRUCTURE AND TYPEDEF ) 2 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/dog.h: -------------------------------------------------------------------------------- 1 | #ifndef _DOG_H_ 2 | #define _DOG_H_ 3 | 4 | /** 5 | * dog_t - Typedef for struct dog 6 | */ 7 | typedef struct dog dog_t; 8 | 9 | /** 10 | * struct dog - struct that stores some information of a dog 11 | * @name: name of the dog 12 | * @age: age of the dog 13 | * @owner: owner of the dog 14 | * 15 | * Description: struct called "dog" that stores its name, its age 16 | * and the name of its owner. 17 | */ 18 | struct dog 19 | { 20 | char *name; 21 | float age; 22 | char *owner; 23 | }; 24 | 25 | void init_dog(struct dog *d, char *name, float age, char *owner); 26 | void print_dog(struct dog *d); 27 | dog_t *new_dog(char *name, float age, char *owner); 28 | void free_dog(dog_t *d); 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /0x0F-function_pointers/0-print_name.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "function_pointers.h" 3 | 4 | /** 5 | * print_name - prints a name 6 | * @name: name to print 7 | * @f: pointer to the printing function 8 | */ 9 | 10 | void print_name(char *name, void (*f)(char *)) 11 | { 12 | if (!name || !f) 13 | return; 14 | f(name); 15 | } 16 | -------------------------------------------------------------------------------- /0x0F-function_pointers/1-array_iterator.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "function_pointers.h" 3 | 4 | /** 5 | * array_iterator - executes a function given as a parameter 6 | * on each element of an array 7 | * @array: array to iterate over 8 | * @size: size of the array 9 | * @action: pointer to function used 10 | */ 11 | 12 | void array_iterator(int *array, size_t size, void (*action)(int)) 13 | { 14 | unsigned int i; 15 | 16 | if (!array || !action) 17 | return; 18 | 19 | for (i = 0; i < size; i++) 20 | action(array[i]); 21 | } 22 | -------------------------------------------------------------------------------- /0x0F-function_pointers/100-main_opcodes.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * main - Prints the opcodes of itself. 6 | * @argc: The number of arguments supplied to the program. 7 | * @argv: An array of pointers to the arguments. 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(int argc, char *argv[]) 12 | { 13 | int bytes, index; 14 | int (*address)(int, char **) = main; 15 | unsigned char opcode; 16 | 17 | if (argc != 2) 18 | { 19 | printf("Error\n"); 20 | exit(1); 21 | } 22 | 23 | bytes = atoi(argv[1]); 24 | 25 | if (bytes < 0) 26 | { 27 | printf("Error\n"); 28 | exit(2); 29 | } 30 | 31 | for (index = 0; index < bytes; index++) 32 | { 33 | opcode = *(unsigned char *)address; 34 | printf("%.2x", opcode); 35 | 36 | if (index == bytes - 1) 37 | continue; 38 | printf(" "); 39 | 40 | address++; 41 | } 42 | 43 | printf("\n"); 44 | 45 | return (0); 46 | } 47 | -------------------------------------------------------------------------------- /0x0F-function_pointers/2-int_index.c: -------------------------------------------------------------------------------- 1 | #include "function_pointers.h" 2 | 3 | /** 4 | * int_index - searches for an integer 5 | * @array: input integer array. 6 | * @size: size of the array. 7 | * @cmp: pointer to the function to be used 8 | * to compare values. 9 | * 10 | * Return: index of the first eement for which the cmp 11 | * function does not return 0. If no elements matches, 12 | * return -1. If size <= 0, return -1. 13 | */ 14 | 15 | int int_index(int *array, int size, int (*cmp)(int)) 16 | { 17 | int i; 18 | 19 | if (array && cmp) 20 | { 21 | if (size <= 0) 22 | return (-1); 23 | 24 | for (i = 0; i < size; i++) 25 | if (cmp(array[i])) 26 | return (i); 27 | } 28 | 29 | return (-1); 30 | } 31 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-calc.h: -------------------------------------------------------------------------------- 1 | #ifndef CALC_H 2 | #define CALC_H 3 | 4 | /* 5 | * Desc: Header file containing all structures and 6 | * prototypes used by the 3-main.c program 7 | */ 8 | 9 | /** 10 | * struct op - This is struct op. 11 | * @op: The operator 12 | * @f: The associated function 13 | */ 14 | 15 | typedef struct op 16 | { 17 | char *op; 18 | int (*f)(int a, int b); 19 | } op_t; 20 | 21 | int op_add(int a, int b); 22 | int op_sub(int a, int b); 23 | int op_mul(int a, int b); 24 | int op_div(int a, int b); 25 | int op_mod(int a, int b); 26 | int (*get_op_func(char *s))(int, int); 27 | 28 | #endif 29 | 30 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-get_op_func.c: -------------------------------------------------------------------------------- 1 | #include "3-calc.h" 2 | #include 3 | 4 | /** 5 | * get_op_func - Selects the correct function to perform 6 | * the operation asked by the user. 7 | * @s: The operator passed as argument. 8 | * 9 | * Return: A pointer to the function corresponding 10 | * to the operator given as a parameter. 11 | */ 12 | int (*get_op_func(char *s))(int, int) 13 | { 14 | op_t ops[] = { 15 | {"+", op_add}, 16 | {"-", op_sub}, 17 | {"*", op_mul}, 18 | {"/", op_div}, 19 | {"%", op_mod}, 20 | {NULL, NULL} 21 | }; 22 | int i = 0; 23 | 24 | while (ops[i].op != NULL && *(ops[i].op) != *s) 25 | i++; 26 | 27 | return (ops[i].f); 28 | } 29 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-main.c: -------------------------------------------------------------------------------- 1 | #include "3-calc.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * main - Prints the result of simple operations. 7 | * @argc: The number of arguments supplied to the program. 8 | * @argv: An array of pointers to the arguments. 9 | * 10 | * Return: Always 0. 11 | */ 12 | int main(int __attribute__((__unused__)) argc, char *argv[]) 13 | { 14 | int num1, num2; 15 | char *op; 16 | 17 | if (argc != 4) 18 | { 19 | printf("Error\n"); 20 | exit(98); 21 | } 22 | 23 | num1 = atoi(argv[1]); 24 | op = argv[2]; 25 | num2 = atoi(argv[3]); 26 | 27 | if (get_op_func(op) == NULL || op[1] != '\0') 28 | { 29 | printf("Error\n"); 30 | exit(99); 31 | } 32 | 33 | if ((*op == '/' && num2 == 0) || 34 | (*op == '%' && num2 == 0)) 35 | { 36 | printf("Error\n"); 37 | exit(100); 38 | } 39 | 40 | printf("%d\n", get_op_func(op)(num1, num2)); 41 | 42 | return (0); 43 | } 44 | 45 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-op_functions.c: -------------------------------------------------------------------------------- 1 | #include "3-calc.h" 2 | 3 | int op_add(int a, int b); 4 | int op_sub(int a, int b); 5 | int op_mul(int a, int b); 6 | int op_div(int a, int b); 7 | int op_mod(int a, int b); 8 | 9 | /** 10 | * op_add - Returns the sum of two numbers. 11 | * @a: The first number. 12 | * @b: The second number. 13 | * 14 | * Return: The sum of a and b. 15 | */ 16 | int op_add(int a, int b) 17 | { 18 | return (a + b); 19 | } 20 | 21 | /** 22 | * op_sub - Returns the difference of two numbers. 23 | * @a: The first number. 24 | * @b: The second number. 25 | * 26 | * Return: The difference of a and b. 27 | */ 28 | int op_sub(int a, int b) 29 | { 30 | return (a - b); 31 | } 32 | 33 | /** 34 | * op_mul - Returns the product of two numbers. 35 | * @a: The first number. 36 | * @b: The second number. 37 | * 38 | * Return: The product of a and b. 39 | */ 40 | int op_mul(int a, int b) 41 | { 42 | return (a * b); 43 | } 44 | 45 | /** 46 | * op_div - Returns the division of two numbers. 47 | * @a: The first number. 48 | * @b: The second number. 49 | * 50 | * Return: The quotient of a and b. 51 | */ 52 | int op_div(int a, int b) 53 | { 54 | return (a / b); 55 | } 56 | 57 | /** 58 | * op_mod - Returns the remainder of the division of two numbers. 59 | * @a: The first number. 60 | * @b: The second number. 61 | * 62 | * Return: The remainder of the division of a by b. 63 | */ 64 | int op_mod(int a, int b) 65 | { 66 | return (a % b); 67 | } 68 | -------------------------------------------------------------------------------- /0x0F-function_pointers/README.md: -------------------------------------------------------------------------------- 1 | C FUNCTIONS POINTERS 2 | -------------------------------------------------------------------------------- /0x0F-function_pointers/function_pointers.h: -------------------------------------------------------------------------------- 1 | #ifndef _FUNCTION_POINTERS_ 2 | #define _FUNCTION_POINTERS_ 3 | 4 | #include 5 | 6 | int _putchar(char c); 7 | void print_name(char *name, void (*f)(char *)); 8 | void array_iterator(int *array, size_t size, void (*action)(int)); 9 | int int_index(int *array, int size, int (*cmp)(int)); 10 | 11 | #endif 12 | -------------------------------------------------------------------------------- /0x10-variadic_functions/0-sum_them_all.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | 3 | /** 4 | * sum_them_all - Returns the sum of all its parameters. 5 | * @n: The number of parameters passed to the function. 6 | * @...: A variable number of parametes to calculate the sum of. 7 | * 8 | * Return: If n == 0 - 0 9 | * Otherwise - the sum of all parameters. 10 | */ 11 | 12 | int sum_them_all(const unsigned int n, ...) 13 | { 14 | va_list nums; 15 | unsigned int index, sum = 0; 16 | 17 | if (n == 0) 18 | return (0); 19 | 20 | va_start(nums, n); 21 | 22 | for (index = 0; index < n; index++) 23 | sum += va_arg(nums, int); 24 | 25 | va_end(nums); 26 | 27 | return (sum); 28 | } 29 | -------------------------------------------------------------------------------- /0x10-variadic_functions/1-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | 3 | /** 4 | * print_numbers - prints numbers followed by new line 5 | * @separator: string to be printed between numbers 6 | * @n: numbers of integers passed to the function 7 | * 8 | * Return: if seperator == NULL, dont print 9 | * otherwise print the numbers 10 | */ 11 | 12 | void print_numbers(const char *separator, const unsigned int n, ...) 13 | { 14 | va_list valist; 15 | unsigned int i; 16 | 17 | va_start(valist, n); 18 | 19 | for (i = 0; i < n; i++) 20 | { 21 | printf("%d", va_arg(valist, int)); 22 | if (separator && i < n - 1) 23 | printf("%s", separator); 24 | } 25 | 26 | printf("\n"); 27 | va_end(valist); 28 | } 29 | -------------------------------------------------------------------------------- /0x10-variadic_functions/2-print_strings.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | 3 | /** 4 | * print_strings - prints strings 5 | * @separator: string to be printed between the strings 6 | * @n: number of strings passed to the function. 7 | * 8 | * Return: no return 9 | */ 10 | 11 | void print_strings(const char *separator, const unsigned int n, ...) 12 | { 13 | va_list valist; 14 | unsigned int i; 15 | char *str; 16 | 17 | va_start(valist, n); 18 | 19 | for (i = 0; i < n; i++) 20 | { 21 | str = va_arg(valist, char *); 22 | 23 | if (str) 24 | printf("%s", str); 25 | 26 | else 27 | printf("(nil)"); 28 | 29 | if (i < n - 1) 30 | if (separator) 31 | printf("%s", separator); 32 | } 33 | 34 | printf("\n"); 35 | va_end(valist); 36 | } 37 | -------------------------------------------------------------------------------- /0x10-variadic_functions/3-print_all.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | 3 | /** 4 | * print_all - prints anything. 5 | * @format: a list of types of arguments passed to the function. 6 | * 7 | * Return: no return. 8 | */ 9 | 10 | void print_all(const char * const format, ...) 11 | { 12 | va_list valist; 13 | unsigned int i = 0, j, c = 0; 14 | char *str; 15 | const char t_arg[] = "cifs"; 16 | 17 | va_start(valist, format); 18 | while (format && format[i]) 19 | { 20 | j = 0; 21 | while (t_arg[j]) 22 | { 23 | if (format[i] == t_arg[j] && c) 24 | { 25 | printf(", "); 26 | break; 27 | } j++; 28 | } 29 | switch (format[i]) 30 | { 31 | case 'c': 32 | printf("%c", va_arg(valist, int)), c = 1; 33 | break; 34 | case 'i': 35 | printf("%d", va_arg(valist, int)), c = 1; 36 | break; 37 | case 'f': 38 | printf("%f", va_arg(valist, double)), c = 1; 39 | break; 40 | case 's': 41 | str = va_arg(valist, char *), c = 1; 42 | if (!str) 43 | { 44 | printf("(nil)"); 45 | break; 46 | } 47 | printf("%s", str); 48 | break; 49 | } i++; 50 | } 51 | printf("\n"), va_end(valist); 52 | } 53 | -------------------------------------------------------------------------------- /0x10-variadic_functions/README.md: -------------------------------------------------------------------------------- 1 | C VARIADIC FUNCTION 2 | -------------------------------------------------------------------------------- /0x10-variadic_functions/variadic_functions.h: -------------------------------------------------------------------------------- 1 | #ifndef _VARIADIC_FUNCTIONS_ 2 | #define _VARIADIC_FUNCTIONS_ 3 | 4 | #include 5 | #include 6 | 7 | int sum_them_all(const unsigned int n, ...); 8 | void print_numbers(const char *separator, const unsigned int n, ...); 9 | void print_strings(const char *separator, const unsigned int n, ...); 10 | void print_all(const char * const format, ...); 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/0-print_list.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /** 5 | * print_list - Prints all the elements of a list_t list. 6 | * @h: The list_t list. 7 | * 8 | * Return: The number of nodes in h 9 | */ 10 | 11 | size_t print_list(const list_t *h) 12 | { 13 | size_t nodes = 0; 14 | 15 | while (h) 16 | { 17 | if (h->str == NULL) 18 | printf("[0] (nil)\n"); 19 | 20 | else 21 | printf("[%d] %s\n", h->len, h->str); 22 | 23 | nodes++; 24 | h = h->next; 25 | } 26 | 27 | return (nodes); 28 | } 29 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/1-list_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * list_len - Finds the number of elements in 5 | * a linked list_t list. 6 | * @h: The linked list_t list. 7 | * @elements: a counter var for size_t (nodes) 8 | * 9 | * Return: The number of elements in h. 10 | */ 11 | 12 | size_t list_len(const list_t *h) 13 | { 14 | size_t elements = 0; 15 | 16 | while (h) 17 | { 18 | elements++; 19 | h = h->next; 20 | } 21 | return (elements); 22 | } 23 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/100-first.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void __attribute__((constructor)) tortoise_hare(void); 4 | 5 | /** 6 | * tortoise_hare - prints a statement before main is executed 7 | * 8 | */ 9 | 10 | void tortoise_hare(void) 11 | { 12 | printf("You're beat! and yet, you must allow,\n"); 13 | printf("I bore my house upon my back!\n"); 14 | } 15 | 16 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/101-hello_holberton.asm: -------------------------------------------------------------------------------- 1 | ; My comments: It is so fun to write in assembly language 2 | ; File: 101-hello_holberton.asm 3 | ; Auth: Stephen Oloo 4 | ; Desc: 64-bit assembly program that prints 5 | ; Hello, Holberton followed by a new line. 6 | 7 | extern printf 8 | 9 | section .text 10 | global main 11 | 12 | main: 13 | push rbp 14 | 15 | mov rdi,fmt 16 | mov rsi,msg 17 | mov rax,0 18 | call printf 19 | 20 | pop rbp 21 | 22 | mov rax,0 23 | ret 24 | 25 | section .data 26 | msg: db "Hello, Holberton", 0 27 | fmt: db "%s", 10, 0 28 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/2-add_node.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /** 5 | * add_node - Adds a new node at the beginning 6 | * of a list_t list 7 | * @head: A pointer to the head of the list_t list 8 | * @str: The string to be added to the list_t list. 9 | * 10 | * Return: If the function fails - NULL 11 | * Otherwise - the addrress of the new element 12 | */ 13 | list_t *add_node(list_t **head, const char *str) 14 | { 15 | list_t *new; 16 | char *dup_str; 17 | int len; 18 | 19 | new = malloc(sizeof(list_t)); 20 | if (new == NULL) 21 | return (NULL); 22 | 23 | dup_str = strdup(str); 24 | if (dup_str == NULL) 25 | { 26 | free(new); 27 | return (NULL); 28 | } 29 | 30 | for (len = 0; str[len];) 31 | len++; 32 | 33 | new->str = dup_str; 34 | new->len = len; 35 | new->next = *head; 36 | 37 | *head = new; 38 | 39 | return (new); 40 | } 41 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/3-add_node_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /** 5 | * add_node_end - Adds a new node at the end 6 | * of a list_t list. 7 | * @head: A pointer the head of the list_t list. 8 | * @str: The string to be added to the list_t list. 9 | * 10 | * Return: If the function fails - NULL. 11 | * Otherwise - the address of the new element. 12 | */ 13 | list_t *add_node_end(list_t **head, const char *str) 14 | { 15 | char *dup; 16 | int len; 17 | list_t *new, *last; 18 | 19 | new = malloc(sizeof(list_t)); 20 | if (new == NULL) 21 | return (NULL); 22 | 23 | dup = strdup(str); 24 | if (str == NULL) 25 | { 26 | free(new); 27 | return (NULL); 28 | } 29 | 30 | for (len = 0; str[len];) 31 | len++; 32 | 33 | new->str = dup; 34 | new->len = len; 35 | new->next = NULL; 36 | 37 | if (*head == NULL) 38 | *head = new; 39 | 40 | else 41 | { 42 | last = *head; 43 | while (last->next != NULL) 44 | last = last->next; 45 | last->next = new; 46 | } 47 | 48 | return (*head); 49 | } 50 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/4-free_list.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /** 5 | * free_list - Frees a list_t list. 6 | * @head: A pointer to the list_t list. 7 | * 8 | */ 9 | void free_list(list_t *head) 10 | { 11 | list_t *tmp; 12 | 13 | while (head) 14 | { 15 | tmp = head->next; 16 | free(head->str); 17 | free(head); 18 | head = tmp; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/README.md: -------------------------------------------------------------------------------- 1 | C PROGRAMMING Singly linked lists 2 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef LISTS_H 2 | #define LISTS_H 3 | 4 | #include 5 | 6 | /** 7 | * struct list_s - singly linked list. 8 | * @str: string - (malloc'ed string) 9 | * @len: length of the string 10 | * @next: points to the next node 11 | * 12 | * Description: singly linked list node structure 13 | */ 14 | typedef struct list_s 15 | { 16 | char *str; 17 | unsigned int len; 18 | struct list_s *next; 19 | } list_t; 20 | 21 | int _putchar(char c); 22 | size_t print_list(const list_t *h); 23 | size_t list_len(const list_t *h); 24 | list_t *add_node(list_t **head, const char *str); 25 | list_t *add_node_end(list_t **head, const char *str); 26 | void free_list(list_t *head); 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/0-print_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * print_listint - prints all elements of a list 5 | * 6 | * @h: list to print elements of 7 | * Return: size_t of the list 8 | */ 9 | size_t print_listint(const listint_t *h) 10 | { 11 | size_t n = 0; 12 | 13 | while (h) 14 | { 15 | printf("%d\n", h->n); 16 | h = h->next; 17 | n++; 18 | } 19 | return (n); 20 | } 21 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/1-listint_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * listint_len - returns number of elements in linked list 5 | * 6 | * @h: list to traverse 7 | * Return: size_t number of elements 8 | */ 9 | size_t listint_len(const listint_t *h) 10 | { 11 | size_t n = 0; 12 | 13 | while (h) 14 | { 15 | n++; 16 | h = h->next; 17 | } 18 | return (n); 19 | } 20 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/10-delete_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * delete_nodeint_at_index - deletes node at index 5 | * 6 | * @head: head of list 7 | * @index: index at which to delete 8 | * Return: 1 if success, -1 if fail 9 | */ 10 | int delete_nodeint_at_index(listint_t **head, unsigned int index) 11 | { 12 | listint_t *prev; 13 | listint_t *next; 14 | unsigned int i; 15 | 16 | if (!head || !(*head)) 17 | return (-1); 18 | next = (*head)->next; 19 | prev = *head; 20 | if (index == 0) 21 | { 22 | free(prev); 23 | *head = next; 24 | return (1); 25 | } 26 | if (next) 27 | next = next->next; 28 | for (i = 1; i < index && next; i++) 29 | { 30 | prev = prev->next; 31 | next = next->next; 32 | } 33 | 34 | if (i == index) 35 | { 36 | free(prev->next); 37 | prev->next = next; 38 | return (1); 39 | } 40 | return (-1); 41 | } 42 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/100-reverse_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * reverse_listint - reverses a linked list 5 | * 6 | * @head: head of the list 7 | * Return: new first node of list 8 | */ 9 | listint_t *reverse_listint(listint_t **head) 10 | { 11 | listint_t *prev; 12 | listint_t *next; 13 | 14 | if (!head || !(*head)) 15 | return (NULL); 16 | 17 | prev = NULL; 18 | next = *head; 19 | 20 | while (next) 21 | { 22 | next = (*head)->next; 23 | (*head)->next = prev; 24 | prev = *head; 25 | *head = next; 26 | } 27 | *head = prev; 28 | return (prev); 29 | } 30 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/101-print_listint_safe.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * print_listint_safe - prints a linked list 5 | * 6 | * @head: head of list 7 | * Return: number of nodes 8 | */ 9 | size_t print_listint_safe(const listint_t *head) 10 | { 11 | size_t n = 0, i = 0; 12 | const listint_t *loop; 13 | const listint_t *h; 14 | 15 | h = head; 16 | loop = head; 17 | while (head) 18 | { 19 | printf("[%p] %d\n", (void *) head, head->n); 20 | if (head == head->next) 21 | { 22 | printf("-> [%p] %d\n", (void *) head, head->n); 23 | n++; 24 | break; 25 | } 26 | head = head->next; 27 | while (i < n && loop) 28 | { 29 | if (head == loop) 30 | { 31 | printf("-> [%p] %d\n", (void *) head, head->n); 32 | if (head != head->next) 33 | n++; 34 | return (n); 35 | } 36 | loop = loop->next; 37 | i++; 38 | } 39 | loop = h; 40 | i = 0; 41 | n++; 42 | } 43 | return (n); 44 | } 45 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/2-add_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_nodeint - adds a node to beginning of list 5 | * 6 | * @head: the first element of list 7 | * @n: int to make content of new node 8 | * Return: address of new element of NULL if failed 9 | */ 10 | listint_t *add_nodeint(listint_t **head, const int n) 11 | { 12 | listint_t *new_node; 13 | 14 | new_node = malloc(sizeof(listint_t)); 15 | if (!new_node) 16 | return (NULL); 17 | new_node->n = n; 18 | new_node->next = NULL; 19 | 20 | if (!head) 21 | return (NULL); 22 | 23 | if (head) 24 | new_node->next = *head; 25 | *head = new_node; 26 | return (new_node); 27 | } 28 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/3-add_nodeint_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_nodeint_end - adds a node at end of list 5 | * 6 | * @head: first node of list 7 | * @n: int to make content of new node 8 | * Return: address of new element or NULL if failed 9 | */ 10 | listint_t *add_nodeint_end(listint_t **head, const int n) 11 | { 12 | listint_t *new_node; 13 | listint_t *copy; 14 | 15 | new_node = malloc(sizeof(listint_t)); 16 | if (!new_node) 17 | return (NULL); 18 | new_node->n = n; 19 | new_node->next = NULL; 20 | 21 | if (!head) 22 | return (NULL); 23 | 24 | if (*head) 25 | { 26 | copy = *head; 27 | while (copy->next) 28 | { 29 | copy = copy->next; 30 | } 31 | copy->next = new_node; 32 | } 33 | else 34 | { 35 | *head = new_node; 36 | } 37 | 38 | return (new_node); 39 | } 40 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/4-free_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_listint - frees a list 5 | * 6 | * @head: head of list to free 7 | * Return: void 8 | */ 9 | void free_listint(listint_t *head) 10 | { 11 | listint_t *temp; 12 | 13 | if (!head) 14 | return; 15 | while (head->next) 16 | { 17 | temp = head->next; 18 | free(head); 19 | head = temp; 20 | } 21 | free(head); 22 | } 23 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/5-free_listint2.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_listint2 - frees a list and sets head to NULL 5 | * 6 | * @head: head of list 7 | * Return: void 8 | */ 9 | void free_listint2(listint_t **head) 10 | { 11 | listint_t *temp; 12 | 13 | if (!head) 14 | return; 15 | if (!(*head)) 16 | return; 17 | 18 | while ((*head)->next) 19 | { 20 | temp = (*head)->next; 21 | free(*head); 22 | *head = temp; 23 | } 24 | free(*head); 25 | *head = NULL; 26 | } 27 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/6-pop_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * pop_listint - deletes head of list and returns head's data 5 | * 6 | * @head: head of linked list 7 | * Return: int value of head 8 | */ 9 | int pop_listint(listint_t **head) 10 | { 11 | listint_t *temp; 12 | int n; 13 | 14 | if (!head || !(*head)) 15 | return (0); 16 | temp = *head; 17 | n = (*head)->n; 18 | if (temp->next) 19 | *head = temp->next; 20 | else 21 | *head = NULL; 22 | free(temp); 23 | return (n); 24 | } 25 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/7-get_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * get_nodeint_at_index - returns nth node of list 5 | * 6 | * @head: head of list 7 | * @index: the index to return node for 8 | * Return: node, if it does not exist, NULL 9 | */ 10 | listint_t *get_nodeint_at_index(listint_t *head, unsigned int index) 11 | { 12 | unsigned int i; 13 | 14 | if (!head) 15 | return (NULL); 16 | 17 | for (i = 0; i < index && head; i++) 18 | head = head->next; 19 | if (i == index) 20 | return (head); 21 | return (NULL); 22 | } 23 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/8-sum_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * sum_listint - returns sum of all data in list 5 | * 6 | * @head: head of list 7 | * Return: sum of all data in list, 0 if empty list 8 | */ 9 | int sum_listint(listint_t *head) 10 | { 11 | int sum = 0; 12 | 13 | while (head) 14 | { 15 | sum += head->n; 16 | head = head->next; 17 | } 18 | return (sum); 19 | } 20 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/9-insert_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * insert_nodeint_at_index - inserts a node at nth position 5 | * 6 | * @head: head of list 7 | * @idx: index to add node at 8 | * @n: data for node 9 | * Return: address of new node or NULL if fail 10 | */ 11 | listint_t *insert_nodeint_at_index(listint_t **head, unsigned int idx, int n) 12 | { 13 | listint_t *new_node; 14 | listint_t *next; 15 | listint_t *prev; 16 | unsigned int i; 17 | 18 | if (!head) 19 | return (NULL); 20 | 21 | new_node = malloc(sizeof(listint_t)); 22 | if (!new_node) 23 | return (NULL); 24 | 25 | next = *head; 26 | 27 | for (i = 0; i < idx && next; i++) 28 | { 29 | prev = next; 30 | next = next->next; 31 | } 32 | 33 | if (i == idx) 34 | { 35 | new_node->n = n; 36 | new_node->next = next; 37 | if (i != 0) 38 | prev->next = new_node; 39 | else 40 | *head = new_node; 41 | } 42 | else 43 | { 44 | return (NULL); 45 | } 46 | return (new_node); 47 | } 48 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/README.md: -------------------------------------------------------------------------------- 1 | 2 | C MORE SINGLY LINKED LISTS PROJECT 3 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef _LISTS_H_ 2 | #define _LISTS_H_ 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | /** 9 | * struct listint_s - singly linked list 10 | * @n: integer 11 | * @next: points to the next node 12 | * 13 | * Description: singly linked list node structure 14 | * for Holberton project 15 | */ 16 | typedef struct listint_s 17 | { 18 | int n; 19 | struct listint_s *next; 20 | } listint_t; 21 | 22 | size_t print_listint(const listint_t *h); 23 | size_t listint_len(const listint_t *h); 24 | listint_t *add_nodeint(listint_t **head, const int n); 25 | listint_t *add_nodeint_end(listint_t **head, const int n); 26 | void free_listint(listint_t *head); 27 | void free_listint2(listint_t **head); 28 | int pop_listint(listint_t **head); 29 | listint_t *get_nodeint_at_index(listint_t *head, unsigned int index); 30 | int sum_listint(listint_t *head); 31 | listint_t *insert_nodeint_at_index(listint_t **head, unsigned int idx, int n); 32 | int delete_nodeint_at_index(listint_t **head, unsigned int index); 33 | listint_t *reverse_listint(listint_t **head); 34 | size_t print_listint_safe(const listint_t *head); 35 | size_t free_listint_safe(listint_t **h); 36 | listint_t *find_listint_loop(listint_t *head); 37 | 38 | #endif /* _LISTS_H_ */ 39 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/0-binary_to_uint.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * binary_to_uint - converts binary number-unsigned integer 5 | * 6 | * Description: 7 | * @b: string with the binary number 8 | * 9 | * Return: converted number 10 | */ 11 | 12 | unsigned int binary_to_uint(const char *b) 13 | { 14 | int a; 15 | unsigned int decimalVal = 0; 16 | 17 | if (!b) 18 | return (0); 19 | 20 | for (a = 0; b[a]; a++) 21 | { 22 | if (b[a] < '0' || b[a] > '1') 23 | return (0); 24 | decimalVal = 2 * decimalVal + (b[a] - '0'); 25 | } 26 | return (decimalVal); 27 | } 28 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/1-print_binary.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_binary - function to print binary repr of a number 5 | * 6 | * Description: 7 | * @n: parameter 8 | * 9 | * Return: void 10 | */ 11 | 12 | void print_binary(unsigned long int n) 13 | { 14 | int b, count = 0; 15 | unsigned long int present; 16 | 17 | for (b = 63; b >= 0; b--) 18 | { 19 | present = n >> b; 20 | 21 | if (present & 1) 22 | { 23 | _putchar('1'); 24 | count++; 25 | } 26 | else if (count) 27 | _putchar('0'); 28 | } 29 | if (!count) 30 | _putchar('0'); 31 | } 32 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/100-get_endianness.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * get_endianness - checks the end 5 | * 6 | * Return: 0 if large endian, and 1 if small endian 7 | */ 8 | 9 | int get_endianness(void) 10 | { 11 | unsigned int b = 1; 12 | char *d = (char *) &b; 13 | 14 | return (*d); 15 | } 16 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/101-password: -------------------------------------------------------------------------------- 1 | Hol 2 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/2-get_bit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * get_bit - function to get value of a bit at a given index 5 | * 6 | * Description: 7 | * @n: decimal parameter 8 | * @index: index 9 | * 10 | * Return: value 11 | */ 12 | 13 | int get_bit(unsigned long int n, unsigned int index) 14 | { 15 | int bit; 16 | 17 | bit = (n >> index); 18 | if (index > 32) 19 | return (-1); 20 | return (bit & 1); 21 | } 22 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/3-set_bit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * set_bit - funtion to sets value of bit to 1 at a given index 6 | * 7 | * Description: 8 | * @n: parameter 9 | * @index: index 10 | * 11 | * Return: value 12 | */ 13 | 14 | int set_bit(unsigned long int *n, unsigned int index) 15 | { 16 | if (index > sizeof(n) * 8) 17 | return (-1); 18 | *n |= (1 << index); 19 | return (1); 20 | } 21 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/4-clear_bit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * clear_bit - function to sets the value of a bit to 0 at a given index 6 | * 7 | * Description: 8 | * @n: parameter 9 | * @index: index 10 | * 11 | * Return: 1 if success, -1 if error 12 | */ 13 | 14 | int clear_bit(unsigned long int *n, unsigned int index) 15 | { 16 | if (index > sizeof(n) * 8) 17 | return (-1); 18 | *n &= ~(1 << index); 19 | return (1); 20 | } 21 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/5-flip_bits.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * flip_bits - function that get the number of bits 5 | * 6 | * Description: 7 | * @n: parameter 8 | * @m: destination 9 | * 10 | * Return: number of flips 11 | */ 12 | 13 | unsigned int flip_bits(unsigned long int n, unsigned long int m) 14 | { 15 | unsigned int b, no_flips = 0; 16 | unsigned long int d = sizeof(unsigned long int) * 8; 17 | 18 | for (b = 0; b < d; b++) 19 | { 20 | if ((m & 1) != (n & 1)) 21 | no_flips += 1; 22 | n = n >> 1; 23 | m = m >> 1; 24 | } 25 | return (no_flips); 26 | } 27 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/README.md: -------------------------------------------------------------------------------- 1 | [![Typing SVG](https://readme-typing-svg.herokuapp.com?font=Fira+Code&weight=900&size=30&pause=1000&width=435&lines=BIT+MANIPULATION)](https://git.io/typing-svg) 2 | 3 | C PROGRAMMING 4 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/_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 | 11 | int _putchar(char c) 12 | { 13 | return (write(1, &c, 1)); 14 | } 15 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | #define BYTE_LENGHT 8 5 | 6 | int _putchar(char c); 7 | unsigned int binary_to_uint(const char *b); 8 | void print_binary(unsigned long int n); 9 | int get_bit(unsigned long int n, unsigned int index); 10 | int set_bit(unsigned long int *n, unsigned int index); 11 | int clear_bit(unsigned long int *n, unsigned int index); 12 | unsigned int flip_bits(unsigned long int n, unsigned long int m); 13 | int get_endianness(void); 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /0x15-file_io/0-read_textfile.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | #include 5 | 6 | /** 7 | * read_textfile - A text file and prints it to POSIX stdout. 8 | * 9 | * Description: 10 | * @filename: file name pointer 11 | * @letters: letter number needed to be read 12 | * 13 | * Return: letter numbers read and printed, or 0 (failure) 14 | * 15 | */ 16 | 17 | ssize_t read_textfile(const char *filename, size_t letters) 18 | { 19 | ssize_t file, rdCount, wrCount; 20 | char *buffer; 21 | 22 | if (filename == NULL) 23 | return (0); 24 | 25 | buffer = malloc(sizeof(char) * letters); 26 | if (buffer == NULL) 27 | return (0); 28 | 29 | file = open(filename, O_RDONLY); 30 | rdCount = read(file, buffer, letters); 31 | wrCount = write(STDOUT_FILENO, buffer, rdCount); 32 | 33 | if (file == -1 || rdCount == -1 || wrCount == -1 || wrCount != rdCount) 34 | { 35 | free(buffer); 36 | return (0); 37 | } 38 | 39 | free(buffer); 40 | close(file); 41 | 42 | return (wrCount); 43 | } 44 | -------------------------------------------------------------------------------- /0x15-file_io/1-create_file.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * create_file - Creats a text file and prints it to STDOUT 6 | * 7 | * Description: 8 | * @filename:file name to create 9 | * @text_content: Text content to write in the file 10 | * 11 | * Return: 1 on success, -1 on failure 12 | */ 13 | 14 | int create_file(const char *filename, char *text_content) 15 | { 16 | int file, bytestat, length = 0; 17 | 18 | if (filename == NULL) 19 | return (-1); 20 | 21 | file = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0600); 22 | if (file == -1) 23 | return (-1); 24 | 25 | if (text_content != NULL) 26 | { 27 | while (*(text_content + length) != '\0') 28 | length++; 29 | } 30 | 31 | bytestat = write(file, text_content, length); 32 | if (bytestat == -1) 33 | { 34 | close(file); 35 | return (-1); 36 | } 37 | 38 | close(file); 39 | return (1); 40 | } 41 | -------------------------------------------------------------------------------- /0x15-file_io/2-append_text_to_file.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * append_text_to_file - Appends text at the file end 6 | * 7 | * Description: 8 | * @filename: Name of the file 9 | * @text_content: content to append into the file 10 | * 11 | * Return: 1 on success and -1 on failure 12 | */ 13 | 14 | int append_text_to_file(const char *filename, char *text_content) 15 | { 16 | int file, wLength = 0, tLength = 0; 17 | 18 | if (filename == NULL) 19 | return (-1); 20 | 21 | file = open(filename, O_WRONLY | O_APPEND); 22 | if (file < -1) 23 | return (-1); 24 | 25 | if (text_content) 26 | { 27 | while (*(text_content + tLength) != '\0') 28 | tLength++; 29 | 30 | wLength = write(file, text_content, tLength); 31 | if (wLength == -1 || wLength != tLength) 32 | return (-1); 33 | } 34 | 35 | close(file); 36 | 37 | return (1); 38 | } 39 | -------------------------------------------------------------------------------- /0x15-file_io/3-cp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | #define MAXSIZE 1204 5 | #define SE STDERR_FILENO 6 | 7 | /** 8 | * main - create the copy bash script 9 | * 10 | * Description: 11 | * @ac: argument count 12 | * @av: arguments as strings 13 | * 14 | * Return: 0 15 | */ 16 | int main(int ac, char *av[]) 17 | { 18 | int input_fd, output_fd, istatus, ostatus; 19 | char buf[MAXSIZE]; 20 | mode_t mode; 21 | 22 | mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH; 23 | if (ac != 3) 24 | dprintf(SE, "Usage: cp file_from file_to\n"), exit(97); 25 | input_fd = open(av[1], O_RDONLY); 26 | if (input_fd == -1) 27 | dprintf(SE, "Error: Can't read from file %s\n", av[1]), exit(98); 28 | output_fd = open(av[2], O_CREAT | O_WRONLY | O_TRUNC, mode); 29 | if (output_fd == -1) 30 | dprintf(SE, "Error: Can't write to %s\n", av[2]), exit(99); 31 | 32 | do { 33 | istatus = read(input_fd, buf, MAXSIZE); 34 | if (istatus == -1) 35 | { 36 | dprintf(SE, "Error: Can't read from file %s\n", av[1]); 37 | exit(98); 38 | } 39 | if (istatus > 0) 40 | { 41 | ostatus = write(output_fd, buf, (ssize_t) istatus); 42 | if (ostatus == -1) 43 | dprintf(SE, "Error: Can't write to %s\n", av[2]), exit(99); 44 | } 45 | } while (istatus > 0); 46 | 47 | istatus = close(input_fd); 48 | if (istatus == -1) 49 | dprintf(SE, "Error: Can't close fd %d\n", input_fd), exit(100); 50 | ostatus = close(output_fd); 51 | if (ostatus == -1) 52 | dprintf(SE, "Error: Can't close fd %d\n", output_fd), exit(100); 53 | 54 | return (0); 55 | } 56 | -------------------------------------------------------------------------------- /0x15-file_io/README.md: -------------------------------------------------------------------------------- 1 | C PROGRAMMING FILE / IO PROJECT 2 | -------------------------------------------------------------------------------- /0x15-file_io/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | ssize_t read_textfile(const char *filename, size_t letters); 13 | int create_file(const char *filename, char *text_content); 14 | int append_text_to_file(const char *filename, char *text_content); 15 | int _putchar(char c); 16 | int _strlen(char *str); 17 | 18 | /** 19 | * ELF header function 20 | * 21 | * Joshua ajayi 22 | */ 23 | 24 | void check_elf(unsigned char *e_ident); 25 | void print_magic(unsigned char *e_ident); 26 | void print_class(unsigned char *e_ident); 27 | void print_data(unsigned char *e_ident); 28 | void print_version(unsigned char *e_ident); 29 | void print_abi(unsigned char *e_ident); 30 | void print_osabi(unsigned char *e_ident); 31 | void print_type(unsigned int e_type, unsigned char *e_ident); 32 | void print_entry(unsigned long int e_entry, unsigned char *e_ident); 33 | void close_elf(int elf); 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/0-print_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * print_dlistint - prints all the elements of a dlistint_t list 5 | * 6 | * Description: 7 | * @h: pointe head of list 8 | * Return: number nodes in the list 9 | * 10 | */ 11 | 12 | size_t print_dlistint(const dlistint_t *h) 13 | { 14 | size_t count = 0; 15 | const dlistint_t *temp; 16 | 17 | if (!h) 18 | return (count); 19 | 20 | temp = h; 21 | while (temp) 22 | { 23 | printf("%d\n", temp->n); 24 | count++; 25 | temp = temp->next; 26 | } 27 | return (count); 28 | } 29 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/1-dlistint_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * dlistint_len - computes the number of nodes in a list 5 | * 6 | * Description: 7 | * @h: pointer head node of the list 8 | * Return: number of nodes 9 | */ 10 | size_t dlistint_len(const dlistint_t *h) 11 | { 12 | size_t count = 0; 13 | const dlistint_t *temp; 14 | 15 | if (!h) 16 | return (count); 17 | 18 | temp = h; 19 | while (temp) 20 | { 21 | count++; 22 | temp = temp->next; 23 | } 24 | return (count); 25 | } 26 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/2-add_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_dnodeint - adds node at the beginning of a list 5 | * 6 | * Description: 7 | * @head: pointer pointer to head node 8 | * @n: value of new node to be inserted 9 | * 10 | * Return: address of new node 11 | */ 12 | 13 | dlistint_t *add_dnodeint(dlistint_t **head, const int n) 14 | { 15 | dlistint_t *new_node; 16 | 17 | new_node = malloc(sizeof(dlistint_t)); 18 | if (!new_node) 19 | return (NULL); 20 | 21 | new_node->n = n; 22 | new_node->prev = NULL; 23 | new_node->next = NULL; 24 | 25 | if (!*head) 26 | *head = new_node; 27 | else 28 | { 29 | new_node->next = *head; 30 | (*head)->prev = new_node; 31 | *head = new_node; 32 | } 33 | 34 | return (new_node); 35 | } 36 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/3-add_dnodeint_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_dnodeint_end - adds new node at the end of a list 5 | * 6 | * Description: 7 | * @head: pointer to pointer to head node 8 | * @n: value of the new node to be inserted 9 | * 10 | * Return: address of new node 11 | */ 12 | 13 | dlistint_t *add_dnodeint_end(dlistint_t **head, const int n) 14 | { 15 | dlistint_t *new_node, *temp; 16 | 17 | new_node = malloc(sizeof(dlistint_t)); 18 | if (!new_node) 19 | return (NULL); 20 | 21 | new_node->n = n; 22 | new_node->next = NULL; 23 | 24 | if (!*head) 25 | *head = new_node; 26 | else 27 | { 28 | temp = *head; 29 | while (temp->next) 30 | temp = temp->next; 31 | 32 | temp->next = new_node; 33 | new_node->prev = temp; 34 | } 35 | 36 | return (new_node); 37 | } 38 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/4-free_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_dlistint - frees a list 5 | * 6 | * Description: 7 | * @head: pointer to head node 8 | */ 9 | 10 | void free_dlistint(dlistint_t *head) 11 | { 12 | dlistint_t *old, *new; 13 | 14 | /* to keep of the track of node(s) when traversing */ 15 | old = head; 16 | new = head; 17 | 18 | while (new) 19 | { 20 | new = new->next; 21 | free(old); 22 | old = new; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/5-get_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * get_dnodeint_at_index - gets the nth node of a list 5 | * 6 | * Description: 7 | * @head: pointer to head node of list 8 | * @index: position of node in the list 9 | * 10 | * Return: address of nth nth or NULL if it is not found 11 | */ 12 | dlistint_t *get_dnodeint_at_index(dlistint_t *head, unsigned int index) 13 | { 14 | dlistint_t *temp; 15 | unsigned int count = 0; 16 | 17 | if (!head) 18 | return (NULL); 19 | 20 | temp = head; 21 | while (temp) 22 | { 23 | if (count == index) 24 | return (temp); 25 | count++; 26 | temp = temp->next; 27 | } 28 | return (NULL); 29 | } 30 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/6-sum_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * sum_dlistint - computes the sum of data in a list 5 | * 6 | * Description: 7 | * @head: pointer to head node of list 8 | * 9 | * Return: sum of data in the list or 0 if list is empty 10 | */ 11 | int sum_dlistint(dlistint_t *head) 12 | { 13 | dlistint_t *temp; 14 | int sum = 0; 15 | 16 | if (!head) 17 | return (0); 18 | 19 | temp = head; 20 | while (temp) 21 | { 22 | sum += temp->n; 23 | temp = temp->next; 24 | } 25 | return (sum); 26 | } 27 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/7-insert_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * insert_dnodeint_at_index - inserts node at a specific position 5 | * 6 | * Description; 7 | * @h: double pointer to head node of list 8 | * @idx: index position of new node, starting at 0 9 | * @n: new node data 10 | * 11 | * Return: address of new node after insertion or NULL is insertion failed 12 | */ 13 | 14 | dlistint_t *insert_dnodeint_at_index(dlistint_t **h, unsigned int idx, int n) 15 | { 16 | dlistint_t *new_node, *lead; 17 | unsigned int count = 0; 18 | 19 | if (!*h && idx != 0) 20 | return (NULL); 21 | 22 | new_node = malloc(sizeof(dlistint_t)); 23 | if (!new_node) 24 | return (NULL); 25 | new_node->n = n; 26 | new_node->next = NULL; 27 | new_node->prev = NULL; 28 | 29 | if (!*h && idx == 0) 30 | { 31 | new_node->prev = *h; 32 | *h = new_node; 33 | return (new_node); 34 | } 35 | if (idx == 0) /* insert at the beginning of list */ 36 | { 37 | new_node->next = *h; 38 | (*h)->prev = new_node; 39 | *h = new_node; 40 | return (new_node); 41 | } 42 | lead = *h; 43 | while (count < idx - 1) 44 | { 45 | count++; 46 | lead = lead->next; 47 | if (!lead) /* when index is out range */ 48 | return (NULL); 49 | } 50 | if (lead->next) /* when operation is still within list */ 51 | lead->next->prev = new_node; 52 | new_node->next = lead->next; 53 | new_node->prev = lead; 54 | lead->next = new_node; 55 | return (new_node); 56 | } 57 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/8-delete_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * delete_dnodeint_at_index - deletes node at specific position 5 | * 6 | * Description: 7 | * @head: double pointer to head node 8 | * @index: position of node to be deleted 9 | * 10 | * Return: 1 if successful, otherwise fail 11 | */ 12 | 13 | int delete_dnodeint_at_index(dlistint_t **head, unsigned int index) 14 | { 15 | dlistint_t *temp; 16 | unsigned int count = 0; 17 | 18 | if (!*head) 19 | return (-1); 20 | 21 | temp = *head; 22 | if (index == 0) 23 | { 24 | *head = (*head)->next; 25 | free(temp); 26 | if (*head) 27 | (*head)->prev = NULL; 28 | return (1); 29 | } 30 | while (count < index) 31 | { 32 | count++; 33 | temp = temp->next; 34 | 35 | if (!temp) /* when idx is out of range */ 36 | return (-1); 37 | } 38 | temp->prev->next = temp->next; 39 | temp->next->prev = temp->prev; 40 | free(temp); 41 | return (1); 42 | } 43 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/README.md: -------------------------------------------------------------------------------- 1 | [![Typing SVG](https://readme-typing-svg.herokuapp.com?font=Fira+Code&size=28&pause=1000&width=435&lines=C+-+Doubly+linked+lists)](https://git.io/typing-svg) 2 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef LISTS_H 2 | #define LISTS_H 3 | 4 | 5 | #include 6 | #include 7 | 8 | /** 9 | * struct dlistint_s - doubly linked list 10 | * @n: integer 11 | * @prev: points to the previous node 12 | * @next: points to the next node 13 | * 14 | * Description: doubly linked list node structure 15 | * 16 | */ 17 | typedef struct dlistint_s 18 | { 19 | int n; 20 | struct dlistint_s *prev; 21 | struct dlistint_s *next; 22 | } dlistint_t; 23 | 24 | size_t print_dlistint(const dlistint_t *h); 25 | size_t dlistint_len(const dlistint_t *h); 26 | dlistint_t *add_dnodeint(dlistint_t **head, const int n); 27 | dlistint_t *add_dnodeint_end(dlistint_t **head, const int n); 28 | void free_dlistint(dlistint_t *head); 29 | dlistint_t *get_dnodeint_at_index(dlistint_t *head, unsigned int index); 30 | int sum_dlistint(dlistint_t *head); 31 | dlistint_t *insert_dnodeint_at_index(dlistint_t **h, unsigned int idx, int n); 32 | int delete_dnodeint_at_index(dlistint_t **head, unsigned int index); 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/0-isupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isupper - check uppercase 5 | * 6 | * Description: 7 | * @charc: character to be checked 8 | * Return: 1 if upper, 0 otherwise 9 | */ 10 | 11 | int _isupper(int charc) 12 | { 13 | if (charc >= 'A' && charc <= 'Z') 14 | { 15 | return (1); 16 | } 17 | else 18 | { 19 | return (0); 20 | } 21 | _putchar('\n'); 22 | } 23 | 24 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/0-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - entry point 6 | * 7 | * Description: 8 | * checks the code 9 | * 10 | * Return: Always EXIT_SUCCESS. 11 | */ 12 | 13 | int main(void) 14 | { 15 | printf("%d\n", _strlen("My Dyn Lib")); 16 | return (EXIT_SUCCESS); 17 | } 18 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/0-memset.c: -------------------------------------------------------------------------------- 1 | #include"main.h" 2 | 3 | /** 4 | * _memset - fills memory with constant byte 5 | * 6 | * Description: 7 | * @sr: pointer to memory area to fill 8 | * @nb: the number of bytes to fill 9 | * @by: constant byte 10 | * 11 | * Return: a pointer to the memory area (@sr) 12 | */ 13 | 14 | char *_memset(char *sr, char by, unsigned int nb) 15 | { 16 | unsigned int parv; 17 | 18 | for (parv = 0; parv < nb; parv++) 19 | { 20 | sr[parv] = by; 21 | } 22 | return (sr); 23 | } 24 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/0-strcat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strcat - function that concatenates two strings 5 | * 6 | * Description: 7 | * @dest: output dest 8 | * @src: input source 9 | * 10 | * Return: return to dest, the concatenated two strings 11 | */ 12 | 13 | char *_strcat(char *dest, char *src) 14 | { 15 | int i, j; 16 | 17 | for (i = 0; dest[i] != '\0'; i++) 18 | ; 19 | for (j = 0; src[j] != '\0'; j++) 20 | { 21 | dest[i] = src[j]; 22 | i++; 23 | } 24 | return (dest); 25 | } 26 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/1-create_dynamic_lib.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -Wall -fPIC -c *.c 3 | gcc -shared -o liball.so *.o 4 | export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH 5 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/1-isdigit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isdigit - function that checks if a character is a digit 5 | * 6 | * Description 7 | * a function that checks for a digit (0 through 9). 8 | * @c: character to check 9 | * 10 | * Return: 0 if not a digit and 1 if it a digit 11 | * 12 | */ 13 | 14 | int _isdigit(int c) 15 | { 16 | if (c >= '0' && c <= '9') 17 | { 18 | return (1); 19 | } 20 | else 21 | { 22 | return (0); 23 | } 24 | _putchar('\n'); 25 | } 26 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/1-strncat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strncat - Write a function that concatenates two string 5 | * 6 | * Description: 7 | * @dest: output dest 8 | * @src: input src 9 | * @n: number bytes fro src 10 | * 11 | * Return: void return 12 | */ 13 | 14 | char *_strncat(char *dest, char *src, int n) 15 | { 16 | int index, count; 17 | 18 | for (index = 0; dest[index] != '\0'; index++) 19 | ; 20 | for (count = 0; count < n && src[count] != '\0'; count++) 21 | { 22 | dest[index] = src[count]; 23 | index++; 24 | } 25 | if (count < n) 26 | { 27 | dest[index] = '\0'; 28 | } 29 | return (dest); 30 | } 31 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/100-atoi.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _atoi - converts a string to an integer 5 | * 6 | * Description: 7 | * @s: string to be converted 8 | * 9 | * Return: the integer converted from the string 10 | */ 11 | 12 | int _atoi(char *s) 13 | { 14 | int i, len, d, f, n, digit; 15 | 16 | i = 0; 17 | len = 0; 18 | n = 0; 19 | d = 0; 20 | digit = 0; 21 | f = 0; 22 | 23 | while (s[len] != '\0') 24 | len++; 25 | 26 | while (i < len && f == 0) 27 | { 28 | if (s[i] == '-') 29 | ++d; 30 | 31 | if (s[i] >= '0' && s[i] <= '9') 32 | { 33 | digit = s[i] - '0'; 34 | if (d % 2) 35 | digit = -digit; 36 | n = n * 10 + digit; 37 | f = 1; 38 | if (s[i + 1] < '0' || s[i + 1] > '9') 39 | break; 40 | f = 0; 41 | } 42 | i++; 43 | } 44 | 45 | if (f == 0) 46 | return (0); 47 | 48 | return (n); 49 | } 50 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/100-operations.so: -------------------------------------------------------------------------------- 1 | ELF 2 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/100-tests.py: -------------------------------------------------------------------------------- 1 | import random 2 | import ctypes 3 | 4 | cops = ctypes.CDLL('./100-operations.so') 5 | a = random.randint(-111, 111) 6 | b = random.randint(-111, 111) 7 | print("{} + {} = {}".format(a, b, cops.add(a, b))) 8 | print("{} - {} = {}".format(a, b, cops.sub(a, b))) 9 | print("{} x {} = {}".format(a, b, cops.mul(a, b))) 10 | print("{} / {} = {}".format(a, b, cops.div(a, b))) 11 | print("{} % {} = {}".format(a, b, cops.mod(a, b))) 12 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/101-make_me_win.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | wget -P .. https://raw.githubusercontent.com/monoprosito/holbertonschool-low_level_programming/master/0x18-dynamic_libraries/libgiga.so 3 | export LD_PRELOAD="$PWD/../libgiga.so" 4 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/2-strchr.c: -------------------------------------------------------------------------------- 1 | #include"main.h" 2 | #include 3 | 4 | /** 5 | * _strchr - locates a character in a string 6 | * 7 | * Description: 8 | * @s: string to check 9 | * @c: character we're looking for 10 | * 11 | * Return: 12 | * pointer to the first occurance of the character 13 | * else NULL 14 | */ 15 | 16 | char *_strchr(char *s, char c) 17 | { 18 | while (*s) 19 | { 20 | if (*s != c) 21 | s++; 22 | else 23 | return (s); 24 | } 25 | if (c == '\0') 26 | return (s); 27 | 28 | return (NULL); 29 | } 30 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/2-strlen.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strlen - function tha returns the length of a string 5 | * 6 | * Description: 7 | * @s: string to beevaluate 8 | * 9 | * Return: the length of the string 10 | */ 11 | 12 | int _strlen(char *s) 13 | { 14 | int i; 15 | 16 | i = 0; 17 | 18 | while (s[i] != '\0') 19 | { 20 | i++; 21 | } 22 | 23 | return (i); 24 | } 25 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/2-strncpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strncpy - function that copies a string, with number of bytes 4 | * 5 | * Description: 6 | * @dest: output dest 7 | * @src: input source 8 | * @n: number of bytes to copy 9 | * 10 | *Return: string copied with the number of bytes 11 | */ 12 | 13 | char *_strncpy(char *dest, char *src, int n) 14 | { 15 | int index; 16 | 17 | for (index = 0; index < n && src[index] != '\0' ; index++) 18 | { 19 | dest[index] = src[index]; 20 | } 21 | for (; index < n; index++) 22 | { 23 | dest[index] = '\0'; 24 | } 25 | return (dest); 26 | } 27 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/README.md: -------------------------------------------------------------------------------- 1 | C DYNAMIC LIBARY 2 | -------------------------------------------------------------------------------- /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 5 | * 6 | * Description: 7 | * @size: size of the array 8 | * 9 | * Return: pointer to the new created hash table or NULL 10 | */ 11 | 12 | hash_table_t *hash_table_create(unsigned long int size) 13 | { 14 | hash_table_t *hash_table; 15 | unsigned long int i; 16 | 17 | hash_table = malloc(sizeof(hash_table_t)); 18 | if (hash_table == NULL) 19 | return (NULL); 20 | 21 | hash_table->array = malloc(sizeof(hash_node_t *) * size); 22 | if (hash_table->array == NULL) 23 | { 24 | free(hash_table); 25 | return (NULL); 26 | } 27 | 28 | for (i = 0; i < size; i++) 29 | hash_table->array[i] = NULL; 30 | 31 | hash_table->size = size; 32 | 33 | return (hash_table); 34 | } 35 | -------------------------------------------------------------------------------- /0x1A-hash_tables/1-djb2.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_djb2 - the implementation of djbw algorithm 5 | * 6 | * Description: 7 | * @str: the string used to generate hash value 8 | * 9 | * Return: the hash value 10 | */ 11 | 12 | unsigned long int hash_djb2(const unsigned char *str) 13 | { 14 | unsigned long int hash; 15 | int c; 16 | 17 | hash = 5381; 18 | while ((c = *str++)) 19 | { 20 | hash = ((hash << 5) + hash) + c; /* hash * 33 + c */ 21 | } 22 | 23 | return (hash); 24 | } 25 | -------------------------------------------------------------------------------- /0x1A-hash_tables/2-key_index.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * key_index - function that calculates index of key in hash table array 5 | * 6 | * Description: 7 | * @key: key 8 | * @size: size for the array 9 | * 10 | * Return: index which the key should store 11 | */ 12 | 13 | unsigned long int key_index(const unsigned char *key, unsigned long int size) 14 | { 15 | unsigned long int hash_value; 16 | unsigned long int index; 17 | 18 | hash_value = hash_djb2(key); 19 | index = hash_value % size; 20 | 21 | return (index); 22 | } 23 | -------------------------------------------------------------------------------- /0x1A-hash_tables/3-hash_table_set.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_set - Adds an element to the hash table 5 | * 6 | * Description: 7 | * @ht: hash table to update the key/value 8 | * @key: key 9 | * @value: value linked with the key 10 | * 11 | * Return: 1 if valid or 0 invalid 12 | */ 13 | 14 | int hash_table_set(hash_table_t *ht, const char *key, const char *value) 15 | { 16 | unsigned long int index; 17 | hash_node_t *new_node, *head; 18 | 19 | if (ht == NULL || key == NULL || *key == '\0') 20 | return (0); 21 | 22 | index = key_index((unsigned char *)key, ht->size); 23 | 24 | head = ht->array[index]; 25 | while (head != NULL) 26 | { 27 | if (strcmp(head->key, key) == 0) 28 | { 29 | free(head->value); 30 | head->value = strdup(value); 31 | return (1); 32 | } 33 | head = head->next; 34 | } 35 | 36 | new_node = malloc(sizeof(hash_node_t)); 37 | if (new_node == NULL) 38 | return (0); 39 | 40 | new_node->key = strdup(key); 41 | new_node->value = strdup(value); 42 | 43 | new_node->next = ht->array[index]; 44 | ht->array[index] = new_node; 45 | 46 | return (1); 47 | } 48 | -------------------------------------------------------------------------------- /0x1A-hash_tables/4-hash_table_get.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_get - Function that retrieves the value associated with key 5 | * 6 | * Description: 7 | * @ht: hash table to look into 8 | * @key: key to look for 9 | * 10 | * Return: value associated with the key, or NULL if key can't be found 11 | */ 12 | 13 | char *hash_table_get(const hash_table_t *ht, const char *key) 14 | { 15 | unsigned long int index; 16 | hash_node_t *head; 17 | 18 | if (ht == NULL || key == NULL || *key == '\0') 19 | return (NULL); 20 | 21 | index = key_index((unsigned char *)key, ht->size); 22 | 23 | head = ht->array[index]; 24 | while (head != NULL) 25 | { 26 | if (strcmp(head->key, key) == 0) 27 | return (head->value); 28 | 29 | head = head->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 - function that prints a hash table 5 | * 6 | * Description: 7 | * @ht: hash table to be printed 8 | */ 9 | 10 | void hash_table_print(const hash_table_t *ht) 11 | { 12 | unsigned long int i; 13 | hash_node_t *node; 14 | int flag = 0; 15 | 16 | if (ht == NULL) 17 | return; 18 | 19 | printf("{"); 20 | for (i = 0; i < ht->size; i++) 21 | { 22 | node = ht->array[i]; 23 | while (node != NULL) 24 | { 25 | if (flag == 1) 26 | printf(", "); 27 | printf("'%s': '%s'", node->key, node->value); 28 | flag = 1; 29 | node = node->next; 30 | } 31 | } 32 | printf("}\n"); 33 | } 34 | -------------------------------------------------------------------------------- /0x1A-hash_tables/6-hash_table_delete.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_delete - function that deletes a hash table 5 | * 6 | * Description: 7 | * @ht: hash table to be deleted 8 | */ 9 | 10 | void hash_table_delete(hash_table_t *ht) 11 | { 12 | unsigned long int i; 13 | hash_node_t *node, *temp; 14 | 15 | if (ht == NULL) 16 | return; 17 | 18 | for (i = 0; i < ht->size; i++) 19 | { 20 | node = ht->array[i]; 21 | while (node != NULL) 22 | { 23 | temp = node; 24 | node = node->next; 25 | free(temp->key); 26 | free(temp->value); 27 | free(temp); 28 | } 29 | } 30 | 31 | free(ht->array); 32 | 33 | free(ht); 34 | } 35 | -------------------------------------------------------------------------------- /0x1A-hash_tables/README.md: -------------------------------------------------------------------------------- 1 | C - HASH TABLE 2 | -------------------------------------------------------------------------------- /0x1A-hash_tables/hash_tables.h: -------------------------------------------------------------------------------- 1 | #ifndef HASH_TABLES_H 2 | #define HASH_TABLES_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | /** 9 | * struct hash_node_s - the node of hash table 10 | * 11 | * Description: 12 | * @key: unique key, string in hash table 13 | * @value: value corresponding to a key 14 | * @next: pointer to next node of the List 15 | */ 16 | 17 | typedef struct hash_node_s 18 | { 19 | char *value; 20 | char *key; 21 | struct hash_node_s *next; 22 | } hash_node_t; 23 | 24 | /** 25 | * struct hash_table_s - the hash table data structure 26 | * 27 | * Description: 28 | * @size: size of the array 29 | * @array: array of size @size 30 | * Each cell of the array is a pointer to first node of linked list 31 | */ 32 | 33 | typedef struct hash_table_s 34 | { 35 | unsigned long int size; 36 | hash_node_t **array; 37 | } hash_table_t; 38 | 39 | unsigned long int hash_djb2(const unsigned char *str); 40 | unsigned long int key_index(const unsigned char *key, unsigned long int size); 41 | char *hash_table_get(const hash_table_t *ht, const char *key); 42 | hash_table_t *hash_table_create(unsigned long int size); 43 | void hash_table_print(const hash_table_t *ht); 44 | int hash_table_set(hash_table_t *ht, const char *key, const char *value); 45 | void hash_table_delete(hash_table_t *ht); 46 | 47 | #endif 48 | 49 | -------------------------------------------------------------------------------- /0x1C-makefiles/0-Makefile: -------------------------------------------------------------------------------- 1 | all : school 2 | 3 | school: 4 | gcc -o school main.c school.c 5 | -------------------------------------------------------------------------------- /0x1C-makefiles/1-Makefile: -------------------------------------------------------------------------------- 1 | all: school 2 | 3 | CC=gcc 4 | SRC=school main.c school.c 5 | 6 | school: 7 | $(CC) -o $(SRC) 8 | -------------------------------------------------------------------------------- /0x1C-makefiles/100-Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | NAME=school 3 | SRC=main.c school.c 4 | OBJ=$(SRC:.c=.o) 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 | NAME=school 3 | SRC=main.c school.c 4 | OBJ=$(SRC:.c=.o) # defines the object that correspond file for each source file 5 | 6 | all:$(SRC) $(NAME) 7 | 8 | # create executable file 9 | # $@ represents the name of target (executable name) 10 | # $^ represents the object files of the target 11 | $(NAME): $(OBJ) 12 | $(CC) -o $@ $^ 13 | -------------------------------------------------------------------------------- /0x1C-makefiles/3-Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | NAME=school 3 | SRC=main.c school.c 4 | OBJ=$(SRC:.c=.o) # defines the object that correspond file for each source file 5 | RM=rm -f 6 | 7 | all:$(SRC) $(NAME) 8 | 9 | # create executable file 10 | # $@ represents the name of target (executable name) 11 | # $^ represents the object files of the target 12 | $(NAME): $(OBJ) 13 | $(CC) -o $@ $^ 14 | 15 | # deletes all the emacs and Vim temporary files along with executable 16 | clean: 17 | $(RM) *~ $(NAME) 18 | 19 | # deletes object files 20 | oclean: 21 | $(RM) $(OBJ) 22 | 23 | fclean: 24 | $(RM) *~ $(NAME) $(OBJ) 25 | 26 | re: oclean all 27 | -------------------------------------------------------------------------------- /0x1C-makefiles/4-Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | NAME=school 3 | SRC=main.c school.c 4 | OBJ=$(SRC:.c=.o) # defines the object that correspond file for each source file 5 | RM=rm -f 6 | CFLAGS=-Wall -Werror -Wextra -pedantic 7 | 8 | all:$(SRC) $(NAME) 9 | 10 | # create executable file 11 | # $@ represents the name of target (executable name) 12 | # $^ represents the object files of the target 13 | $(NAME): $(OBJ) 14 | $(CC) $(CFLAGS) -o $@ $^ 15 | 16 | # deletes all the emacs and Vim temporary files along with executable 17 | clean: 18 | $(RM) *~ $(NAME) 19 | 20 | # deletes object files 21 | oclean: 22 | $(RM) $(OBJ) 23 | 24 | fclean: 25 | $(RM) *~ $(NAME) $(OBJ) 26 | 27 | re: oclean all 28 | -------------------------------------------------------------------------------- /0x1C-makefiles/README.md: -------------------------------------------------------------------------------- 1 | MAKE FILE PROJECT IN C 2 | -------------------------------------------------------------------------------- /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: Success 7 | */ 8 | 9 | int main(void) 10 | { 11 | print_school(); 12 | return (EXIT_SUCCESS); 13 | } 14 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/0-linear.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * linear_search - function that searches for a value in array of 5 | * integers using Linear search algorithm 6 | * 7 | * Description: 8 | * @array: the input array 9 | * @size: the size of the array 10 | * @value: the value to search in 11 | * 12 | * Return: Always EXIT_SUCCESS 13 | */ 14 | 15 | int linear_search(int *array, size_t size, int value) 16 | { 17 | int i; 18 | 19 | if (array == NULL) 20 | return (-1); 21 | 22 | for (i = 0; i < (int)size; i++) 23 | { 24 | printf("Value checked array[%u] = [%d]\n", i, array[i]); 25 | if (value == array[i]) 26 | return (i); 27 | } 28 | return (-1); 29 | } 30 | 31 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/1-binary.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * binary_search - function that searches for a value in sorted array 5 | * of integers with binary search. 6 | * 7 | * Description: Prints [sub]array being searched after each change. 8 | * @array: Pointer to first element of array to search. 9 | * @size: Number of elements in the array. 10 | * @value: Value to search 11 | * 12 | * Return: If value is not present or array is NULL, -1. 13 | * Otherwise, index where value is located. 14 | * 15 | */ 16 | 17 | int binary_search(int *array, size_t size, int value) 18 | { 19 | size_t i, left, right; 20 | 21 | if (array == NULL) 22 | return (-1); 23 | 24 | for (left = 0, right = size - 1; right >= left;) 25 | { 26 | printf("Searching in array: "); 27 | for (i = left; i < right; i++) 28 | printf("%d, ", array[i]); 29 | printf("%d\n", array[i]); 30 | 31 | i = left + (right - left) / 2; 32 | if (array[i] == value) 33 | return (i); 34 | if (array[i] > value) 35 | right = i - 1; 36 | else 37 | left = i + 1; 38 | } 39 | 40 | return (-1); 41 | } 42 | 43 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/100-jump.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | #include 3 | 4 | /** 5 | * jump_search - function that searches for value in an array of 6 | * integers using Jump search algorithm 7 | * 8 | * Description: 9 | * @array: the input array 10 | * @size: size of array 11 | * @value: the value to search in 12 | * Return: index of number 13 | * 14 | */ 15 | 16 | int jump_search(int *array, size_t size, int value) 17 | { 18 | int index, m, k, prev; 19 | 20 | if (array == NULL || size == 0) 21 | return (-1); 22 | 23 | m = (int)sqrt((double)size); 24 | k = 0; 25 | prev = index = 0; 26 | 27 | do { 28 | printf("Value checked array[%d] = [%d]\n", index, array[index]); 29 | 30 | if (array[index] == value) 31 | return (index); 32 | k++; 33 | prev = index; 34 | index = k * m; 35 | } while (index < (int)size && array[index] < value); 36 | 37 | printf("Value found between indexes [%d] and [%d]\n", prev, index); 38 | 39 | for (; prev <= index && prev < (int)size; prev++) 40 | { 41 | printf("Value checked array[%d] = [%d]\n", prev, array[prev]); 42 | if (array[prev] == value) 43 | return (prev); 44 | } 45 | 46 | return (-1); 47 | } 48 | 49 | -------------------------------------------------------------------------------- /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 - function that searches for value in array of 5 | * integers using Interpolation search algorithm 6 | * 7 | * Description: 8 | * @array: the input array 9 | * @size: size of array 10 | * @value: the value to search in 11 | * Return: index of number 12 | * 13 | */ 14 | 15 | int interpolation_search(int *array, size_t size, int value) 16 | { 17 | size_t pos, low, high; 18 | double f; 19 | 20 | if (array == NULL) 21 | return (-1); 22 | 23 | low = 0; 24 | high = size - 1; 25 | 26 | while (size) 27 | { 28 | f = (double)(high - low) / (array[high] - array[low]) * (value - array[low]); 29 | pos = (size_t)(low + f); 30 | printf("Value checked array[%d]", (int)pos); 31 | 32 | if (pos >= size) 33 | { 34 | printf(" is out of range\n"); 35 | break; 36 | } 37 | else 38 | { 39 | printf(" = [%d]\n", array[pos]); 40 | } 41 | 42 | if (array[pos] == value) 43 | return ((int)pos); 44 | 45 | if (array[pos] < value) 46 | low = pos + 1; 47 | else 48 | high = pos - 1; 49 | 50 | if (low == high) 51 | break; 52 | } 53 | 54 | return (-1); 55 | } 56 | 57 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/103-exponential.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * _binary_search - function that searches for value in sorted array 5 | * of integers using binary search. 6 | * 7 | * Description: Prints the [sub]array being searched after each change. 8 | * @array: Pointer to first element of array to search. 9 | * @left: Starting index of [sub]array to search. 10 | * @right: Ending index of [sub]array to search. 11 | * @value: Value to search for. 12 | * 13 | * Return: If value is not present or the array is NULL, -1. 14 | * Otherwise, index where value is located. 15 | * 16 | */ 17 | int _binary_search(int *array, size_t left, size_t right, int value) 18 | { 19 | size_t i; 20 | 21 | if (array == NULL) 22 | return (-1); 23 | 24 | while (right >= left) 25 | { 26 | printf("Searching in array: "); 27 | for (i = left; i < right; i++) 28 | printf("%d, ", array[i]); 29 | printf("%d\n", array[i]); 30 | 31 | i = left + (right - left) / 2; 32 | if (array[i] == value) 33 | return (i); 34 | if (array[i] > value) 35 | right = i - 1; 36 | else 37 | left = i + 1; 38 | } 39 | 40 | return (-1); 41 | } 42 | 43 | /** 44 | * exponential_search - function that searches for value in a sorted array 45 | * of integers using exponential search. 46 | * 47 | * Description: Prints a value every time it is compared in the array. 48 | * @array: Pointer to first element of array to search. 49 | * @size: Number of elements in the array. 50 | * @value: Value to search for. 51 | * 52 | * Return: If value is not present or the array is NULL, -1. 53 | * Otherwise, index where the value is located. 54 | * 55 | */ 56 | 57 | int exponential_search(int *array, size_t size, int value) 58 | { 59 | size_t i = 0, right; 60 | 61 | if (array == NULL) 62 | return (-1); 63 | 64 | if (array[0] != value) 65 | { 66 | for (i = 1; i < size && array[i] <= value; i = i * 2) 67 | printf("Value checked array[%ld] = [%d]\n", i, array[i]); 68 | } 69 | 70 | right = i < size ? i : size - 1; 71 | printf("Value found between indexes [%ld] and [%ld]\n", i / 2, right); 72 | return (_binary_search(array, i / 2, right, value)); 73 | } 74 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/104-advanced_binary.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * rec_search - function that searches for a value in an array of 5 | * integers using Binary search algorithm 6 | * 7 | * Description: 8 | * @array: the input array 9 | * @size: size of array 10 | * @value: the value to search in 11 | * Return: index of number 12 | */ 13 | 14 | int rec_search(int *array, size_t size, int value) 15 | { 16 | size_t half = size / 2; 17 | size_t i; 18 | 19 | if (array == NULL || size == 0) 20 | return (-1); 21 | 22 | printf("Searching in array"); 23 | 24 | for (i = 0; i < size; i++) 25 | printf("%s %d", (i == 0) ? ":" : ",", array[i]); 26 | 27 | printf("\n"); 28 | 29 | if (half && size % 2 == 0) 30 | half--; 31 | 32 | if (value == array[half]) 33 | { 34 | if (half > 0) 35 | return (rec_search(array, half + 1, value)); 36 | return ((int)half); 37 | } 38 | 39 | if (value < array[half]) 40 | return (rec_search(array, half + 1, value)); 41 | 42 | half++; 43 | return (rec_search(array + half, size - half, value) + half); 44 | } 45 | 46 | /** 47 | * advanced_binary - function that calls to rec_search to return 48 | * index of number 49 | * 50 | * Description: 51 | * @array: the input array 52 | * @size: size of array 53 | * @value: the value to search in 54 | * Return: index of number 55 | */ 56 | 57 | int advanced_binary(int *array, size_t size, int value) 58 | { 59 | int index; 60 | 61 | index = rec_search(array, size, value); 62 | 63 | if (index >= 0 && array[index] != value) 64 | return (-1); 65 | 66 | return (index); 67 | } 68 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/105-jump_list.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | #include 3 | 4 | /** 5 | * jump_list - function that searches for value in an array of 6 | * integers using Jump search algorithm 7 | * 8 | * Description: 9 | * @list: the input list 10 | * @size: size of array 11 | * @value: the value to search in 12 | * Return: index of number 13 | */ 14 | 15 | listint_t *jump_list(listint_t *list, size_t size, int value) 16 | { 17 | size_t index, k, m; 18 | listint_t *prev; 19 | 20 | if (list == NULL || size == 0) 21 | return (NULL); 22 | 23 | m = (size_t)sqrt((double)size); 24 | index = 0; 25 | k = 0; 26 | 27 | do { 28 | prev = list; 29 | k++; 30 | index = k * m; 31 | 32 | while (list->next && list->index < index) 33 | list = list->next; 34 | 35 | if (list->next == NULL && index != list->index) 36 | index = list->index; 37 | 38 | printf("Value checked at index [%d] = [%d]\n", (int)index, list->n); 39 | 40 | } while (index < size && list->next && list->n < value); 41 | 42 | printf("Value found between indexes "); 43 | printf("[%d] and [%d]\n", (int)prev->index, (int)list->index); 44 | 45 | for (; prev && prev->index <= list->index; prev = prev->next) 46 | { 47 | printf("Value checked at index [%d] = [%d]\n", (int)prev->index, prev->n); 48 | if (prev->n == value) 49 | return (prev); 50 | } 51 | 52 | return (NULL); 53 | } 54 | 55 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/106-linear_skip.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * linear_skip - function that searches for a value in a skip list 5 | * 6 | * Description: 7 | * @list: the input list 8 | * @value: the value to search in 9 | * Return: index of number 10 | */ 11 | 12 | skiplist_t *linear_skip(skiplist_t *list, int value) 13 | { 14 | skiplist_t *go; 15 | 16 | if (list == NULL) 17 | return (NULL); 18 | 19 | go = list; 20 | 21 | do { 22 | list = go; 23 | go = go->express; 24 | printf("Value checked at index "); 25 | printf("[%d] = [%d]\n", (int)go->index, go->n); 26 | } while (go->express && go->n < value); 27 | 28 | if (go->express == NULL) 29 | { 30 | list = go; 31 | while (go->next) 32 | go = go->next; 33 | } 34 | 35 | printf("Value found between indexes "); 36 | printf("[%d] and [%d]\n", (int)list->index, (int)go->index); 37 | 38 | while (list != go->next) 39 | { 40 | printf("Value checked at index [%d] = [%d]\n", (int)list->index, list->n); 41 | if (list->n == value) 42 | return (list); 43 | list = list->next; 44 | } 45 | 46 | return (NULL); 47 | } 48 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/107-O: -------------------------------------------------------------------------------- 1 | O(n) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/108-O: -------------------------------------------------------------------------------- 1 | O(sqrt(n)) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/2-O: -------------------------------------------------------------------------------- 1 | O(n) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/3-O: -------------------------------------------------------------------------------- 1 | O(1) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/4-O: -------------------------------------------------------------------------------- 1 | O(log(n)) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/5-O: -------------------------------------------------------------------------------- 1 | O(1) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/6-O: -------------------------------------------------------------------------------- 1 | O(nm) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/README.md: -------------------------------------------------------------------------------- 1 | Search Algorithms in C 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/search_algos.h: -------------------------------------------------------------------------------- 1 | #ifndef _SORTING_ALGOS_H_ 2 | #define _SORTING_ALGOS_H_ 3 | 4 | #include 5 | #include 6 | 7 | /** 8 | * struct listint_s - singly linked list struct 9 | * 10 | * @n: Integer 11 | * @index: Index of node in the list 12 | * @next: Pointer to next node 13 | * 14 | * Description: singly linked list node structure 15 | * 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 express lane 26 | * 27 | * @n: Integer 28 | * @index: Index of node in the list 29 | * @next: Pointer to next node 30 | * @express: Pointer to next node in express lane 31 | * 32 | * Description: singly linked list node structure with an express lane 33 | * 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 binary_search(int *array, size_t size, int value); 44 | int exponential_search(int *array, size_t size, int value); 45 | int advanced_binary(int *array, size_t size, int value); 46 | int jump_search(int *array, size_t size, int value); 47 | int interpolation_search(int *array, size_t size, int value); 48 | listint_t *jump_list(listint_t *list, size_t size, int value); 49 | skiplist_t *linear_skip(skiplist_t *list, int value); 50 | int linear_search(int *array, size_t size, int value); 51 | 52 | #endif 53 | 54 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Typing SVG](https://readme-typing-svg.herokuapp.com?font=Fira+Code&weight=900&size=30&pause=1000&width=435&lines=ALX+LOW_LEVEL_PROGRAMMING)](https://git.io/typing-svg) 2 | 3 | [![Typing SVG](https://readme-typing-svg.herokuapp.com?font=Fira+Code&weight=900&pause=1000&color=11F700&width=435&lines=%23include+%3Cstdio.h%3E)](https://git.io/typing-svg) 4 | 5 | C programming repository C preprocessing, compile, assemble, linking - Project 6 | 7 | c variables, if, else, while - Project 2 8 | 9 | c functions and nested loops - Project 3 10 | 11 | c debugging - Project 4 12 | 13 | c more functions and nested loops - Project 5 14 | 15 | c pointers, arrays and strings - Project 6 16 | 17 | c more pointers, arrays and strings - Project 7 18 | 19 | c Even more pointers, arrays and strings - Project 8 20 | 21 | C - Recursion - Project 9 22 | 23 | c static libaries - Project 10 24 | 25 | c malloc | free - Project 11 26 | 27 | c more malloc - Project 12 28 | 29 | c structure and typedef - Project 13 30 | 31 | c functions pointers - Project 14 32 | 33 | c Variadic functions - Project 15 34 | 35 | c Singly linked lists - Project 16 36 | 37 | c more singly linked list - Project 17 38 | 39 | c bit manipulation - Project 18 40 | 41 | c file/io - Project 19 42 | --------------------------------------------------------------------------------