├── 0x00-hello_world ├── .4-puts.c.swp ├── 0-preprocessor ├── 1-compiler ├── 100-intel ├── 101-quote.c ├── 2-assembler ├── 3-name ├── 4-puts.c ├── 5-printf.c ├── 6-size.c ├── README.md ├── size32 └── size64 ├── 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 ├── .11-print_to_98.c.swp ├── 0-putchar.c ├── 1-alphabet.c ├── 10-add.c ├── 100-times_table.c ├── 101-natural.c ├── 102-fibonacci.c ├── 103-fibonacci.c ├── 104-fibonacci.c ├── 11-print_to_98.c ├── 2-print_alphabet_x10.c ├── 3-islower.c ├── 4-isalpha.c ├── 5-sign.c ├── 6-abs.c ├── 7-print_last_digit.c ├── 8-24_hours.c ├── 9-times_table.c ├── README.md └── main.h ├── 0x03-debugging ├── .3-print_remaining_days.c.swo ├── .3-print_remaining_days.c.swp ├── 0-main.c ├── 1-main.c ├── 2-largest_number.c ├── 2-main.c ├── 3-convert_day.c ├── 3-print_remaining_days.c ├── README.md ├── main.h └── positive_or_negative.c ├── 0x04-more_functions_nested_loops ├── .101-print_number.c.swp ├── 0-isupper.c ├── 1-isdigit.c ├── 10-print_triangle.c ├── 100-prime_factor.c ├── 101-print_number.c ├── 2-mul.c ├── 3-print_numbers.c ├── 4-print_most_numbers.c ├── 5-more_numbers.c ├── 6-print_line.c ├── 7-print_diagonal.c ├── 8-print_square.c ├── 9-fizz_buzz.c ├── README.md └── main.h ├── 0x05-pointers_arrays_strings ├── 0-reset_to_98.c ├── 1-swap.c ├── 100-atoi.c ├── 101-keygen.c ├── 2-strlen.c ├── 3-puts.c ├── 4-print_rev.c ├── 5-rev_string.c ├── 6-puts2.c ├── 7-puts_half.c ├── 8-print_array.c ├── 9-strcpy.c ├── README.md └── main.h ├── 0x06-pointers_arrays_strings ├── 0-strcat.c ├── 1-strncat.c ├── 100-rot13.c ├── 101-print_number.c ├── 102-magic.c ├── 103-infinite_add.c ├── 104-print_buffer.c ├── 2-strncpy.c ├── 3-strcmp.c ├── 4-rev_array.c ├── 5-string_toupper.c ├── 6-cap_string.c ├── 7-leet.c ├── 8-rot13.c ├── README.md └── main.h ├── 0x07-pointers_arrays_strings ├── 0-memset.c ├── 1-memcpy.c ├── 100-set_string.c ├── 101-crackme_password ├── 2-strchr.c ├── 3-strspn.c ├── 4-strpbrk.c ├── 5-strstr.c ├── 7-print_chessboard.c ├── 8-print_diagsums.c ├── README.md └── main.h ├── 0x08-recursion ├── .0-puts_recursion.c.swp ├── 0-main.c ├── 0-puts_recursion.c ├── 1-main.c ├── 1-print_rev_recursion.c ├── 100-is_palindrome.c ├── 100-main.c ├── 101-main.c ├── 101-wildcmp.c ├── 2-main.c ├── 2-strlen_recursion.c ├── 3-factorial.c ├── 3-main.c ├── 4-main.c ├── 4-pow_recursion.c ├── 5-main.c ├── 5-sqrt_recursion.c ├── 6-is_prime_number.c ├── 6-main.c ├── README.md └── main.h ├── 0x09-static_libraries ├── 0-isupper.c ├── 0-isupper.o ├── 0-memset.c ├── 0-memset.o ├── 0-strcat.c ├── 0-strcat.o ├── 1-isdigit.c ├── 1-isdigit.o ├── 1-memcpy.c ├── 1-memcpy.o ├── 1-strncat.c ├── 1-strncat.o ├── 100-atoi.c ├── 100-atoi.o ├── 2-strchr.c ├── 2-strchr.o ├── 2-strlen.c ├── 2-strlen.o ├── 2-strncpy.c ├── 2-strncpy.o ├── 3-islower.c ├── 3-islower.o ├── 3-puts.c ├── 3-puts.o ├── 3-strcmp.c ├── 3-strcmp.o ├── 3-strspn.c ├── 3-strspn.o ├── 4-isalpha.c ├── 4-isalpha.o ├── 4-strpbrk.c ├── 4-strpbrk.o ├── 5-strstr.c ├── 5-strstr.o ├── 6-abs.c ├── 6-abs.o ├── 9-strcpy.c ├── 9-strcpy.o ├── README.md ├── _putchar.c ├── _putchar.o ├── create_static_lib.sh ├── liball.a ├── libmy.a ├── main.c ├── main.h └── main.o ├── 0x0A-argc_argv ├── .100-change.c.swp ├── 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 ├── 0-main.c ├── 1-main.c ├── 1-strdup.c ├── 100-argstostr.c ├── 100-main.c ├── 101-main.c ├── 101-strtow.c ├── 2-main.c ├── 2-str_concat.c ├── 3-alloc_grid.c ├── 3-main.c ├── 4-free_grid.c ├── 4-main.c ├── README.md ├── _putchar.c └── main.h ├── 0x0C-more_malloc_free ├── 0-main.c ├── 0-malloc_checked.c ├── 1-main.c ├── 1-string_nconcat.c ├── 100-main.c ├── 100-realloc.c ├── 101-mul.c ├── 2-calloc.c ├── 2-main.c ├── 3-array_range.c ├── 3-main.c ├── README.md ├── _putchar.c └── main.h ├── 0x0D-preprocessor ├── 0-main.c ├── 0-object_like_macro.h ├── 1-main.c ├── 1-pi.h ├── 2-main.c ├── 3-function_like_macro.h ├── 3-main.c ├── 4-main.c ├── 4-sum.h ├── README.md └── _putchar.c ├── 0x0E-structures_typedef ├── 0-main.c ├── 1-init_dog.c ├── 1-main.c ├── 2-main.c ├── 2-print_dog.c ├── 3-main.c ├── 4-main.c ├── 4-new_dog.c ├── 5-free_dog.c ├── 5-main.c ├── README.md └── dog.h ├── 0x0F-function_pointers ├── 0-main.c ├── 0-print_name.c ├── 1-array_iterator.c ├── 1-main.c ├── 100-main_opcodes.c ├── 2-int_index.c ├── 2-main.c ├── 3-calc.h ├── 3-get_op_func.c ├── 3-main.c ├── 3-op_functions.c ├── README.md ├── _putchar.c └── function_pointers.h ├── 0x10-variadic_functions ├── 0-main.c ├── 0-sum_them_all.c ├── 1-main.c ├── 1-print_numbers.c ├── 2-main.c ├── 2-print_strings.c ├── 3-main.c ├── 3-print_all.c ├── README.md ├── _putchar.c └── variadic_functions.h ├── 0x12-singly_linked_lists ├── 0-main.c ├── 0-print_list.c ├── 1-list_len.c ├── 1-main.c ├── 100-first.c ├── 101-hello_holberton.asm ├── 101-hello_holberton.o ├── 2-add_node.c ├── 2-main.c ├── 3-add_node_end.c ├── 3-main.c ├── 4-free_list.c ├── 4-main.c ├── README.md ├── _putchar.c └── lists.h ├── 0x13-more_singly_linked_lists ├── 0-main.c ├── 0-print_listint.c ├── 1-listint_len.c ├── 1-main.c ├── 10-delete_nodeint.c ├── 10-main.c ├── 100-reverse_listint.c ├── 101-main.c ├── 101-print_listint_safe.c ├── 102-free_listint_safe.c ├── 102-main.c ├── 103-find_loop.c ├── 103-main.c ├── 2-add_nodeint.c ├── 2-main.c ├── 3-add_nodeint_end.c ├── 3-main.c ├── 4-free_listint.c ├── 4-main.c ├── 5-free_listint2.c ├── 5-main.c ├── 6-main.c ├── 6-pop_listint.c ├── 7-get_nodeint.c ├── 7-main.c ├── 8-main.c ├── 8-sum_listint.c ├── 9-insert_nodeint.c ├── 9-main.c ├── README.md ├── _putchar.c └── lists.h ├── 0x14-bit_manipulation ├── 0-binary_to_uint.c ├── 0-main.c ├── 1-main.c ├── 1-print_binary.c ├── 100-get_endianness.c ├── 100-main.c ├── 101-password ├── 2-get_bit.c ├── 2-main.c ├── 3-main.c ├── 3-set_bit.c ├── 4-clear_bit.c ├── 4-main.c ├── 5-flip_bits.c ├── 5-main.c ├── README.md ├── _putchar.c └── main.h ├── 0x15-file_io ├── 0-main.c ├── 0-read_textfile.c ├── 1-create_file.c ├── 1-main.c ├── 100-elf_header.c ├── 2-append_text_to_file.c ├── 2-main.c ├── 3-cp.c ├── README.md ├── _putchar.c └── main.h ├── 0x17-doubly_linked_lists ├── 0-print_dlistint.c ├── 1-dlistint_len.c ├── 100-password ├── 102-result ├── 103-keygen.c ├── 2-add_dnodeint.c ├── 3-add_dnodeint_end.c ├── 4-free_dlistint.c ├── 5-get_dnodeint.c ├── 6-sum_dlistint.c ├── 7-insert_dnodeint.c ├── 8-delete_dnodeint.c ├── README.md ├── lists.h └── tests │ ├── 0-main.c │ ├── 1-main.c │ ├── 2-main.c │ ├── 3-main.c │ ├── 4-main.c │ ├── 5-main.c │ ├── 6-main.c │ ├── 7-main.c │ └── 8-main.c ├── 0x18-dynamic_libraries ├── .main.h.swp ├── 0-isupper.c ├── 0-isupper.o ├── 0-memset.c ├── 0-memset.o ├── 0-strcat.c ├── 0-strcat.o ├── 1-create_dynamic_lib.sh ├── 1-isdigit.c ├── 1-isdigit.o ├── 1-memcpy.c ├── 1-memcpy.o ├── 1-strncat.c ├── 1-strncat.o ├── 100-atoi.c ├── 100-atoi.o ├── 100-operations.so ├── 100-test.py ├── 100-tests.py ├── 101-make_me_win.sh ├── 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 ├── TEST │ ├── _putchar.c │ ├── _putchar.o │ ├── abs.c │ ├── abs.o │ ├── atoi.c │ ├── atoi.o │ ├── isalpha.c │ ├── isalpha.o │ ├── isdigit.c │ ├── isdigit.o │ ├── islower.c │ ├── islower.o │ ├── isupper.c │ ├── isupper.o │ ├── libfilename.so │ ├── main.h │ ├── memcpy.c │ ├── memcpy.o │ ├── memset.c │ ├── memset.o │ ├── puts.c │ ├── puts.o │ ├── strcat.c │ ├── strcat.o │ ├── strchr.c │ ├── strchr.o │ ├── strcmp.c │ ├── strcmp.o │ ├── strcpy.c │ ├── strcpy.o │ ├── strlen.c │ ├── strlen.o │ ├── strncat.c │ ├── strncat.o │ ├── strncpy.c │ ├── strncpy.o │ ├── strpbrk.c │ ├── strpbrk.o │ ├── strspn.c │ ├── strspn.o │ ├── strstr.c │ └── strstr.o ├── _putchar.c ├── _putchar.o ├── iwin.c ├── iwin.so ├── libdynamic.so ├── main.h ├── nrandom.so └── test.c ├── 0x1A-hash_tables ├── 0-hash_table_create.c ├── 0-main.c ├── 1-djb2.c ├── 1-main.c ├── 100-main.c ├── 100-sorted_hash_table.c ├── 2-key_index.c ├── 2-main.c ├── 3-hash_table_set.c ├── 3-main.c ├── 4-hash_table_get.c ├── 4-main.c ├── 5-hash_table_print.c ├── 5-main.c ├── 6-hash_table_delete.c ├── 6-main.c ├── README.md ├── hash_tables.h └── tests │ ├── 0-main.c │ ├── 100-main.c │ ├── 2-main.c │ ├── 3-main.c │ ├── 4-main.c │ ├── 5-main.c │ └── 6-main.c ├── 0x1C-makefiles ├── 0-Makefile ├── 1-Makefile ├── 100-Makefile ├── 2-Makefile ├── 3-Makefile ├── 4-Makefile ├── 5-island_perimeter.py ├── README.md ├── m.h ├── main.c ├── school.c └── tests │ └── 5-main.py ├── 0x1E-search_algorithms ├── 0-linear.c ├── 1-binary.c ├── 100-jump.c ├── 101-O ├── 102-interpolation.c ├── 103-exponential.c ├── 104-advanced_binary.c ├── 105-jump_list.c ├── 106-linear_skip.c ├── 107-O ├── 108-O ├── 2-O ├── 3-O ├── 4-O ├── 5-O ├── 6-O ├── README.md └── search_algos.h └── README.md /0x00-hello_world/.4-puts.c.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x00-hello_world/.4-puts.c.swp -------------------------------------------------------------------------------- /0x00-hello_world/0-preprocessor: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc $CFILE -E -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 | /* 2 | * File: 101-quote.c 3 | * Auth: Prince Solomon 4 | */ 5 | 6 | #include 7 | 8 | /** 9 | * main - prints "and that piece of art is useful" - Dora Korpar, 2015-10-19" 10 | * Return: 1. 11 | */ 12 | int main(void) 13 | { 14 | write(2, 15 | "and that piece of art is useful\" - Dora kopar, 2015-10-19\n", 16 | 59); 17 | return (1); 18 | } 19 | -------------------------------------------------------------------------------- /0x00-hello_world/2-assembler: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -S $CFILE 3 | -------------------------------------------------------------------------------- /0x00-hello_world/3-name: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc $CFILE -o cisfun 3 | -------------------------------------------------------------------------------- /0x00-hello_world/4-puts.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - my first c programming 4 | * 5 | * Return: 0 6 | */ 7 | 8 | int main(void) 9 | { 10 | puts("\"Programming is like building a multilingual puzzle"); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x00-hello_world/5-printf.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - print a C program using printf 4 | * 5 | * Return: 0 6 | */ 7 | 8 | int main(void) 9 | { 10 | printf("with proper grammar, but the outcome is a piece of art,\n"); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x00-hello_world/6-size.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - C program that prints the size of various types 4 | * Code by Prince Solomon 5 | * Return: 0 6 | */ 7 | int main(void) 8 | { 9 | char a; 10 | int b; 11 | long int c; 12 | long long int d; 13 | float f; 14 | 15 | printf("size of a char: %d byte(s)\n", (int) sizeof(a)); 16 | printf("size of a int: %d byte(s)\n", (int) sizeof(b)); 17 | printf("size of a long int: %d byte(s)\n", (int) sizeof(c)); 18 | printf("size of a lolng lolng int: %d byte(s)\n", (int) sizeof(d)); 19 | printf("size of a float: %d byte(s)\n", (int) sizeof(f)); 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /0x00-hello_world/size32: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x00-hello_world/size32 -------------------------------------------------------------------------------- /0x00-hello_world/size64: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x00-hello_world/size64 -------------------------------------------------------------------------------- /0x01-variables_if_else_while/0-positive_or_negative.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | /** 5 | * main - Print a random numbers 6 | * Code by: Prince Solomon 7 | * Return: 0 8 | */ 9 | 10 | int main(void) 11 | { 12 | int n; 13 | 14 | srand(time(0)); 15 | n = rand() - RAND_MAX / 2; 16 | if (n > 0) 17 | printf("%d is positive\n", n); 18 | else if (n < 0) 19 | printf("%d is negative\n", n); 20 | else 21 | printf("%d is zero\n", n); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/1-last_digit.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | /** 5 | * main - print last digit 6 | * code by: Prince Solomon 7 | * Return: 0 8 | */ 9 | int main(void) 10 | { 11 | int n; 12 | int x; 13 | 14 | srand(time(0)); 15 | n = rand() - RAND_MAX / 2; 16 | x = n % 10; 17 | printf("Last digit of %d is %d ", n, x); 18 | if (x > 5) 19 | { 20 | printf("and is greater than 5"); 21 | } 22 | if (x == 0) 23 | { 24 | printf("and is 0"); 25 | } 26 | if (x < 6 && x != 0) 27 | { 28 | printf("and is less than 6 and not 0"); 29 | } 30 | printf("\n"); 31 | return (0); 32 | } 33 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/100-print_comb3.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - Prints all possible combinations of two different digits, 4 | * 5 | * Return: 0 6 | */ 7 | int main(void) 8 | { 9 | int digit1, digit2; 10 | 11 | for (digit1 = 0; digit1 < 9; digit1++) 12 | { 13 | for (digit2 = digit1 + 1; digit2 < 10; digit2++) 14 | { 15 | putchar((digit1 % 10) + '0'); 16 | putchar((digit2 % 10) + '0'); 17 | if (digit1 == 8 && digit2 == 9) 18 | continue; 19 | putchar(','); 20 | putchar(' '); 21 | } 22 | } 23 | putchar('\n'); 24 | 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/101-print_comb4.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - Prints all possible combinations of three different digits, 4 | * 5 | * Return: 0 6 | */ 7 | int main(void) 8 | { 9 | int digit1, digit2, digit3; 10 | 11 | for (digit1 = 0; digit1 < 8; digit1++) 12 | { 13 | for (digit2 = digit1 + 1; digit2 < 9; digit2++) 14 | { 15 | for (digit3 = digit2 + 1; digit3 < 10; digit3++) 16 | { 17 | putchar((digit1 % 10) + '0'); 18 | putchar((digit2 % 10) + '0'); 19 | putchar((digit3 % 10) + '0'); 20 | if (digit1 == 7 && digit2 == 8 && digit3 == 9) 21 | continue; 22 | putchar(','); 23 | putchar(' '); 24 | } 25 | } 26 | } 27 | 28 | putchar('\n'); 29 | 30 | return (0); 31 | } 32 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/102-print_comb5.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - prints all posible combination of two-digit nummbers. 4 | * 5 | * Return: 0 6 | */ 7 | int main(void) 8 | { 9 | int i, j; 10 | 11 | for (i = 0; i < 100; i++) 12 | { 13 | for (j = 0; j < 100; j++) 14 | { 15 | if (i < j) 16 | { 17 | putchar((i / 10) + 48); 18 | putchar((i % 10) + 48); 19 | putchar(' '); 20 | putchar((j / 10) + 48); 21 | putchar((j % 10) + 48); 22 | if (i != 98 || j != 99) 23 | { 24 | putchar(','); 25 | putchar(' '); 26 | } 27 | } 28 | } 29 | } 30 | putchar('\n'); 31 | 32 | return (0); 33 | } 34 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/2-print_alphabet.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - prints tha alphabet in lowercase 4 | * Code By: Prince Solomon 5 | * Return: 0 6 | */ 7 | int main(void) 8 | { 9 | char letter; 10 | 11 | for (letter = 'a'; letter <= 'z'; letter++) 12 | putchar(letter); 13 | 14 | putchar('\n'); 15 | 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/3-print_alphabets.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Prints the alphabet in lowercase, and then in uppercase 5 | * Code By: Prince Solomon 6 | * Return: 0 7 | */ 8 | int main(void) 9 | { 10 | char letter; 11 | 12 | for (letter = 'a'; letter <= 'z'; letter++) 13 | putchar(letter); 14 | 15 | for (letter = 'A'; letter <= 'Z'; letter++) 16 | putchar(letter); 17 | 18 | putchar('\n'); 19 | 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/4-print_alphabt.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - print the alphabet in lowercase, except q and e. 4 | * Code By: Prince Solomon 5 | * Return: 0 6 | */ 7 | int main(void) 8 | { 9 | char letter; 10 | 11 | for (letter = 'a'; letter <= 'z'; letter++) 12 | { 13 | if (letter != 'e' && letter != 'q') 14 | putchar(letter); 15 | } 16 | putchar('\n'); 17 | 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/5-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - prints all single digit numbers of base 10 starting from 0, 4 | * 5 | * Return: 0 6 | */ 7 | int main(void) 8 | { 9 | int n; 10 | 11 | for (n = 0; n < 10; n++) 12 | { 13 | printf("%d", n); 14 | } 15 | printf("\n"); 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/6-print_numberz.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - prints all single digit numbers 4 | * 5 | * Return: 0 6 | */ 7 | int main(void) 8 | { 9 | int num; 10 | 11 | for (num = 0; num < 10; num++) 12 | putchar((num % 10) + '0'); 13 | 14 | putchar('\n'); 15 | 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/7-print_tebahpla.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - print the lowercase alphabet in reverse. 4 | * 5 | * Return: 0 6 | */ 7 | int main(void) 8 | { 9 | int letter; 10 | 11 | for (letter = 'z'; letter >= 'a'; letter--) 12 | putchar(letter); 13 | 14 | putchar('\n'); 15 | 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/8-print_base16.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - prints all numbers of base 16 in lowercase. 4 | * 5 | * Return: 0 6 | */ 7 | int main(void) 8 | { 9 | int num; 10 | char letter; 11 | 12 | for (num = 0; num < 10; num++) 13 | putchar((num % 10) + '0'); 14 | 15 | for (letter = 'a'; letter <= 'f'; letter++) 16 | putchar(letter); 17 | putchar('\n'); 18 | 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/9-print_comb.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - prints all possible combination of single-digit numbers. 4 | * 5 | * Return: 0 6 | */ 7 | int main(void) 8 | { 9 | int num; 10 | 11 | for (num = 0; num <= 9; num++) 12 | { 13 | putchar((num % 10) + '0'); 14 | if (num == 9) 15 | continue; 16 | putchar(','); 17 | putchar(' '); 18 | } 19 | 20 | putchar('\n'); 21 | 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/\: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - print the lowercase alphabet in reverse. 4 | * 5 | * Return: 0 6 | */ 7 | int main(void) 8 | { 9 | int letter; 10 | 11 | for (letter = 'z'; letter >= 'a'; letter--) 12 | putchar(letter); 13 | 14 | putchar('\n'); 15 | 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/.11-print_to_98.c.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x02-functions_nested_loops/.11-print_to_98.c.swp -------------------------------------------------------------------------------- /0x02-functions_nested_loops/0-putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | /** 4 | * main - print _putchar 5 | * Code by: Prince Solomon 6 | * Return: 0 7 | */ 8 | int main(void) 9 | { 10 | putchar('_'); 11 | putchar('p'); 12 | putchar('u'); 13 | putchar('t'); 14 | putchar('c'); 15 | putchar('h'); 16 | putchar('a'); 17 | putchar('r'); 18 | putchar('\n'); 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/1-alphabet.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_alphabet - print the alphabet in lowercase, followed by a new line. 5 | * Code by Prince Solomon 6 | */ 7 | 8 | void print_alphabet(void) 9 | { 10 | char letter; 11 | 12 | for (letter = 'a'; letter <= 'z'; letter++) 13 | _putchar(letter); 14 | 15 | putchar('\n'); 16 | } 17 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/10-add.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * add - adds two integers. 5 | * @num1: the first integer to be added 6 | * @num2: the second integer to be added 7 | * Code by: Princexz 8 | * Return: the result of the addition. 9 | */ 10 | int add(int num1, int num2) 11 | { 12 | return (num1 + num2); 13 | } 14 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/101-natural.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Lists all the natural numbers below 1024 (excluded) 5 | * that are multiples of 3 or 5. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int i, sum = 0; 12 | 13 | for (i = 0; i < 1024; i++) 14 | { 15 | if ((i % 3) == 0 || (i % 5) == 0) 16 | sum += i; 17 | } 18 | 19 | printf("%d\n", sum); 20 | 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/102-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Prints first 50 Fibonacci numbers, starting with 1 and 2, 5 | * separated by a comma followed by a space. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int count; 12 | unsigned long fib1 = 0, fib2 = 1, sum; 13 | 14 | for (count = 0; count < 50; count++) 15 | { 16 | sum = fib1 + fib2; 17 | printf("%lu", sum); 18 | 19 | fib1 = fib2; 20 | fib2 = sum; 21 | 22 | if (count == 49) 23 | printf("\n"); 24 | else 25 | printf(", "); 26 | } 27 | 28 | return (0); 29 | } 30 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/103-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - entry point 4 | * Description: computes nd prints even number < 4,000,000 5 | * 5 below 1024 (excluded), followed by a new line 6 | * Return: 0 7 | */ 8 | int main(void) 9 | { 10 | int a = 0, b = 1, next = 0; 11 | int sum = 0; 12 | 13 | while (next < 4000000) 14 | { 15 | next = a + b; 16 | a = b; 17 | b = next; 18 | if (next % 2 == 0) 19 | sum += next; 20 | } 21 | printf("%i\n", sum); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/11-print_to_98.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_to_98 - prints all natural numbers from input to 98, 5 | * in order seperated by comma followed by a space. 6 | * @n: the number to begin counting at. 7 | */ 8 | void print_to_98(int n) 9 | { 10 | if (n >= 98) 11 | { 12 | while (n > 98) 13 | printf("%d, ", n--); 14 | printf("%d\n", n); 15 | } 16 | else 17 | { 18 | while (n < 98) 19 | printf("%d, ", n++); 20 | printf("%d\n", n); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/2-print_alphabet_x10.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_alphabet_x10 - Prints 10 times the alphabet, in lowercases 5 | * Code by Prince Solomon 6 | */ 7 | 8 | void print_alphabet_x10(void) 9 | { 10 | int count = 0; 11 | char letter; 12 | 13 | while (count++ <= 9) 14 | { 15 | for (letter = 'a'; letter <= 'z'; letter++) 16 | _putchar(letter); 17 | _putchar('\n'); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/3-islower.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _islower - Checks if a character is lowercase. 5 | * @c: The character to be checked. 6 | * 7 | * Return: 1 if character is lower, 0 otherwise 8 | */ 9 | 10 | int _islower(int c) 11 | { 12 | if (c >= 'a' && c <= 'z') 13 | return (1); 14 | else 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/4-isalpha.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isalpha - Check if a character is alphabet 5 | * @c: the character to be check 6 | * Prince Solomon 7 | * Return: 1 if charater is letter, lowercase or upper, o upper 8 | */ 9 | int _isalpha(int c) 10 | { 11 | if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) 12 | return (1); 13 | else 14 | return (0); 15 | } 16 | 17 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/5-sign.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_sign - prints the sign of anumber. 5 | * @n: The of which the sign will be printed. 6 | * 7 | * Return: 1 if the number is greater than zero, 8 | * 0 if the number is zero, 9 | * -1 if the number is less than zero. 10 | */ 11 | int print_sign(int n) 12 | { 13 | if (n > 0) 14 | { 15 | _putchar('+'); 16 | return (1); 17 | } 18 | else if (n == 0) 19 | { 20 | _putchar('0'); 21 | return (0); 22 | } 23 | else 24 | { 25 | _putchar('-'); 26 | return (-1); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/6-abs.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _abs - computes the value of an integer. 5 | * @n: the integer to be computed. 6 | * 7 | * Return: the absolute value of the integer. 8 | */ 9 | int _abs(int n) 10 | { 11 | if (n >= 0) 12 | return (n); 13 | else 14 | return (-n); 15 | } 16 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/7-print_last_digit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_last_digit - prints the last digit of a number. 5 | * @n: the number in question. 6 | * 7 | * Return: value of the last digit. 8 | */ 9 | int print_last_digit(int n) 10 | { 11 | int last_digit = n % 10; 12 | 13 | if (last_digit < 0) 14 | last_digit *= -1; 15 | 16 | _putchar(last_digit + '0'); 17 | 18 | return (last_digit); 19 | } 20 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/8-24_hours.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * jack_bauer - print every minute of the day of 5 | * jack bauer, starting from 00:00 to 23:59. 6 | */ 7 | void jack_bauer(void) 8 | { 9 | int hour, minute; 10 | 11 | for (hour = 0; hour <= 23; hour++) 12 | { 13 | for (minute = 0; minute <= 59; minute++) 14 | { 15 | _putchar((hour / 10) + '0'); 16 | _putchar((hour % 10) + '0'); 17 | _putchar(':'); 18 | _putchar((minute / 10) + '0'); 19 | _putchar((minute % 10) + '0'); 20 | _putchar('\n'); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/9-times_table.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * times_table - prints the 9 times table starting with 0 5 | */ 6 | void times_table(void) 7 | { 8 | int num, mult, prod; 9 | 10 | for (num = 0; num <= 9; num++) 11 | { 12 | _putchar('0'); 13 | 14 | for (mult = 1; mult <= 9; mult++) 15 | { 16 | _putchar(','); 17 | _putchar(' '); 18 | 19 | prod = num * mult; 20 | 21 | if (prod <= 9) 22 | _putchar(' '); 23 | else 24 | _putchar((prod / 10) + '0'); 25 | 26 | _putchar((prod % 10) + '0'); 27 | } 28 | _putchar('\n'); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | #include 4 | 5 | int _putchar(char c); 6 | void print_alphabet(void); 7 | void print_alphabet_x10(void); 8 | int _islower(int c); 9 | int _isalpha(int c); 10 | int print_sign(int n); 11 | int _abs(int); 12 | int print_last_digit(int); 13 | void jack_bauer(void); 14 | void times_table(void); 15 | int add(int, int); 16 | void print_to_98(int n); 17 | void print_times_table(int n); 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /0x03-debugging/.3-print_remaining_days.c.swo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x03-debugging/.3-print_remaining_days.c.swo -------------------------------------------------------------------------------- /0x03-debugging/.3-print_remaining_days.c.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x03-debugging/.3-print_remaining_days.c.swp -------------------------------------------------------------------------------- /0x03-debugging/0-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - test 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 | * largest_number - returns the largest of 3 numbers 4 | * @a: first integer 5 | * @b: second integer 6 | * @c: third integer 7 | * Return: Largest number 8 | */ 9 | 10 | int largest_number(int a, int b, int c) 11 | { 12 | int largest; 13 | 14 | if (a > b && a > c) 15 | { 16 | largest = a; 17 | } 18 | else if (b > c) 19 | { 20 | largest = b; 21 | } 22 | else 23 | { 24 | largest = c; 25 | } 26 | return (largest); 27 | } 28 | -------------------------------------------------------------------------------- /0x03-debugging/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | /** 4 | * main - prints the largest of 3 integers 5 | * Return: 0 6 | */ 7 | 8 | int main(void) 9 | { 10 | int a, b, c; 11 | int largest; 12 | 13 | a = 972; 14 | b = -98; 15 | c = 0; 16 | 17 | largest = largest_number(a, b, c); 18 | 19 | printf("%d is the largest number\n", largest); 20 | 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /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 | 12 | #endif 13 | -------------------------------------------------------------------------------- /0x03-debugging/positive_or_negative.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | #include 5 | 6 | /** 7 | * main - evaluates whether a value n is positive or negative 8 | * 9 | * Description: program allocates a random number to the 10 | * value n and evaluates whether it is positive or negative 11 | * 12 | * Return: Always 0 (success) 13 | */ 14 | int main(void) 15 | { 16 | int n; 17 | 18 | srand(time(0)); 19 | n = rand() - RAND_MAX / 2; 20 | 21 | if (n > 2) 22 | { 23 | printf("%d is positive\n", n); 24 | } 25 | else if (n == 0) 26 | { 27 | printf("%d is zero\n", n); 28 | } 29 | else if (n < 0) 30 | { 31 | printf("%d is negative\n", n); 32 | } 33 | return (0); 34 | } 35 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/.101-print_number.c.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x04-more_functions_nested_loops/.101-print_number.c.swp -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/0-isupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isupper - uppercase letters 5 | * @c: char to check 6 | * 7 | * Prince Solomon 8 | * Return: 0 or 1 9 | */ 10 | 11 | int _isupper(int c) 12 | 13 | { 14 | if (c >= 'A' && c <= 'Z') 15 | return (1); 16 | else 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/1-isdigit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isdigit - write a function that check for a digit (0 throuh 9) 5 | * @c: characters to check 6 | * Prince Solomon 7 | * Return: 0 or 1 8 | */ 9 | 10 | int _isdigit(int c) 11 | { 12 | if (c >= '0' && c <= '9') 13 | return (1); 14 | else 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/10-print_triangle.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_triangle - print a triangle 5 | * code by Prince Solomon 6 | * @size: size of the triangle 7 | * Return: void 8 | */ 9 | 10 | void print_triangle(int size) 11 | { 12 | int row, hashes, spaces; 13 | 14 | if (size <= 0) 15 | { 16 | _putchar('\n'); 17 | } 18 | else 19 | { 20 | for (row = 1; row <= size; row++) 21 | { 22 | for (spaces = size - row; spaces >= 1; spaces--) 23 | { 24 | _putchar(' '); 25 | } 26 | for (hashes = 1; hashes <= row; hashes++) 27 | { 28 | _putchar('#'); 29 | } 30 | _putchar('\n'); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/100-prime_factor.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * main - prints the largest prime factor of 612852475143 6 | * Prince Solomon 7 | * Return: always 0 8 | */ 9 | 10 | int main(void) 11 | { 12 | unsigned long int i, n = 612852485143; 13 | 14 | for (i = 3; i < 782849; i = i + 2) 15 | { 16 | while ((n % i == 0) && (n != i)) 17 | n = n / i; 18 | } 19 | printf("%lu\n", n); 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: .input integer parameter 6 | * 7 | * code by prince solomon 8 | */ 9 | 10 | void print_number(int n) 11 | { 12 | unsigned int i = n; 13 | 14 | if (n < 0) 15 | { 16 | _putchar(45); 17 | i = -1; 18 | } 19 | if (i / 10) 20 | { 21 | print_number(i / 10); 22 | } 23 | _putchar(i % 10 + '0'); 24 | } 25 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/2-mul.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * mul - multiple two integers 6 | * @a: first integer 7 | * @b: second integer 8 | * Code By Prince Solomon 9 | * Return: addition of a and b 10 | */ 11 | 12 | int mul(int a, int b) 13 | { 14 | return (a * b); 15 | } 16 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/3-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_numbers - print 0123456789 5 | * code by prince Solomon 6 | * Return: void 7 | */ 8 | 9 | void print_numbers(void) 10 | { 11 | char c; 12 | 13 | for (c = '0'; c <= '9'; c++) 14 | { 15 | _putchar(c); 16 | } 17 | _putchar('\n'); 18 | } 19 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/4-print_most_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_most_numbers - prints most numbers 5 | * Prince Solomon 6 | * Return: void 7 | */ 8 | 9 | void print_most_numbers(void) 10 | { 11 | char c; 12 | 13 | for (c = '0'; c <= '9'; c++) 14 | { 15 | if (!(c == '2' || c == '4')) 16 | _putchar(c); 17 | } 18 | _putchar('\n'); 19 | } 20 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/5-more_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * more_numbers - print more numbers.. 5 | * CyberTech (file) 6 | * Return: no return 7 | */ 8 | 9 | void more_numbers(void) 10 | { 11 | int i, j; 12 | 13 | for (i = 1; i <= 10; i++) 14 | { 15 | for (j = 0; j <= 14; j++) 16 | { 17 | if (j >= 10) 18 | _putchar('1'); 19 | _putchar(j % 10 + '0'); 20 | } 21 | _putchar('\n'); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/6-print_line.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_line - printing lines 5 | * @n: numbers of times 6 | * Program by prince and code by CyberTech 7 | * Return: void 8 | */ 9 | 10 | void print_line(int n) 11 | { 12 | int i; 13 | 14 | for (i = 0; i < n; i++) 15 | { 16 | _putchar('_'); 17 | } 18 | _putchar('\n'); 19 | } 20 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/7-print_diagonal.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_diagonal - printing lines in diagonal 5 | * @n: input variable 6 | * CyberTech 7 | * Return: void 8 | */ 9 | 10 | void print_diagonal(int n) 11 | { 12 | int i, j; 13 | 14 | if (n <= 0) 15 | _putchar('\n'); 16 | for (i = 0; i < n; i++) 17 | { 18 | for (j = 0; j < i; j++) 19 | { 20 | _putchar(' '); 21 | } 22 | _putchar('\\'); 23 | _putchar('\n'); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/8-print_square.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_square - prints a square of size size 5 | * @size: size of the shape 6 | * Prince Solomon 7 | * Return: void 8 | */ 9 | 10 | 11 | void print_square(int size) 12 | { 13 | int i, j; 14 | 15 | if (size <= 0) 16 | _putchar('\n'); 17 | 18 | for (i = 0; i < size; i++) 19 | { 20 | for (j = 0; j < (size); j++) 21 | { 22 | _putchar('#'); 23 | } 24 | _putchar('\n'); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/9-fizz_buzz.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * main - Enrty 6 | * 7 | * Description: prints the numbers 1 - 100 w/ 8 | * Fizz for multiples of 3, Bizz for multiple of 5, 9 | * and FizzBizz for multiple of both 10 | * 11 | * Return: always 0 12 | */ 13 | int main(void) 14 | { 15 | int i; 16 | 17 | for (i = 0; i <= 100; i++) 18 | { 19 | if (i % 15 == 0) 20 | printf("FizzBuzz"); 21 | else if (i % 3 == 0) 22 | printf("Fizz"); 23 | else if (i % 5 == 0) 24 | printf("Bizz"); 25 | else 26 | printf("%i", i); 27 | if (i < 100) 28 | printf(" "); 29 | } 30 | printf("\n"); 31 | return (0); 32 | } 33 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | 5 | int _putchar(char c); 6 | int _isupper(int c); 7 | int _isdigit(int c); 8 | int mul(int a, int b); 9 | void print_numbers(void); 10 | void print_most_numbers(void); 11 | void more_numbers(void); 12 | void print_line(int n); 13 | void print_diagonal(int n); 14 | void print_square(int size); 15 | void print_triangle(int size); 16 | void print_number(int n); 17 | 18 | 19 | #endif 20 | /* 21 | * Code by: Prince Solomon(CyberTech) 22 | * */ 23 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/0-reset_to_98.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * reset_to_98 - takes the pointer and updates the value 5 | * @n: int to check 6 | * #prince Solomon 7 | * Return: 0 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 | * swap_int - swaps the value of int a and int b 4 | * [Prince Solomon] 5 | * @a: first int 6 | * @b: second int 7 | * Return 0 8 | */ 9 | 10 | void swap_int(int *a, int *b) 11 | { 12 | int s; 13 | 14 | s = *a; 15 | *a = *b; 16 | *b = s; 17 | } 18 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/2-strlen.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strlen - return the length of a string 5 | * [Prince Solomon] 6 | * @s: char to check 7 | * Description: this will return the length of a string 8 | * Return: 0 is (success) 9 | */ 10 | 11 | int _strlen(char *s) 12 | { 13 | int a = 0; 14 | 15 | for (; *s++;) 16 | a++; 17 | return (a); 18 | } 19 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/3-puts.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _puts - prints a string 5 | * @str: the string 6 | * 7 | * Return: the length of the string 8 | */ 9 | void _puts(char *str) 10 | { 11 | int i = 0; 12 | 13 | while (str[i] != '\0') 14 | { 15 | _putchar(str[i]); 16 | i++; 17 | } 18 | _putchar('\n'); 19 | } 20 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/4-print_rev.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_rev -> printing a string in reverse 5 | * @s: the string to be printed in rev 6 | */ 7 | 8 | void print_rev(char *s) 9 | { 10 | int i, n; 11 | 12 | n = 0; 13 | while (s[n] != '\0') 14 | n++; 15 | 16 | for (i = n - 1; i >= 0; i--) 17 | { 18 | _putchar(s[i]); 19 | } 20 | _putchar('\n'); 21 | } 22 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/5-rev_string.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * rev_string - reverses a string 4 | * _putchar - print each character 5 | * @s: char to check 6 | * 7 | * Description: This will reverse a string 8 | * Return: 0 is (success) 9 | */ 10 | 11 | void rev_string(char *s) 12 | { 13 | int a = 0, b, c; 14 | char d; 15 | 16 | while (s[a] != '\0') 17 | { 18 | a++; 19 | } 20 | c = a - 1; 21 | for (b = 0; c >= 0 && b < c; c--, b++) 22 | { 23 | d = s[b]; 24 | s[b] = s[c]; 25 | s[c] = d; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/6-puts2.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * puts2 - prints every other character of a string 4 | * 5 | * @str: char to check 6 | * 7 | * Return: 0 is (success) 8 | */ 9 | 10 | void puts2(char *str) 11 | { 12 | int string; 13 | 14 | for (string = 0; str[string] != '\0'; string++) 15 | if (string % 2 == 0) 16 | _putchar(str[string]); 17 | _putchar('\n'); 18 | } 19 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/7-puts_half.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * puts_half - prints half of a string 5 | * @str: char to check 6 | * 7 | * Return: 0 is [success] 8 | */ 9 | void puts_half(char *str) 10 | { 11 | int string = 0, n; 12 | 13 | while (str[string] != '\0') 14 | string++; 15 | if (string + 1 % 2 != '0') 16 | n = (string - 1) / 2; 17 | else 18 | n = (string / 2); 19 | n++; 20 | 21 | for (string = n; str[string] != '\0'; string++) 22 | _putchar(str[string]); 23 | _putchar('\n'); 24 | } 25 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/8-print_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * print_array - prints n element of an array of integers 6 | * [Prince Solomon] 7 | * @a: int to check 8 | * @n: int to check 9 | * Return: 0 is success 10 | */ 11 | void print_array(int *a, int n) 12 | { 13 | int x; 14 | 15 | for (x = 0; x < n; x++) 16 | if (x != n - 1) 17 | printf("%d, ", a[x]); 18 | else 19 | printf("%d", a[x]); 20 | printf("\n"); 21 | } 22 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/9-strcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strcpy - copy the string pointed to by src to dest 4 | * @dest: char to check 5 | * @src: char to check 6 | * Return: 0 is success 7 | */ 8 | 9 | char *_strcpy(char *dest, char *src) 10 | { 11 | int a; 12 | 13 | for (a = 0; src[a] != '0'; a++) 14 | dest[a] = src[a]; 15 | dest[a] = '\0'; 16 | return (dest); 17 | } 18 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H /*DataSoft*/ 3 | 4 | void reset_to_98(int *n); 5 | void swap_int(int *a, int *b); 6 | int _strlen(char *s); 7 | void _puts(char *str); 8 | void print_rev(char *s); 9 | void rev_string(char *s); 10 | void puts2(char *str); 11 | void puts_half(char *str); 12 | void print_array(int *a, int n); 13 | char *_strcpy(char *dest, char *src); 14 | int _atoi(char *s); 15 | int _putchar(char c); 16 | 17 | 18 | #endif /*Prince Solomon*/ 19 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/0-strcat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strcat - concat 2 string 5 | * @dest:char 6 | * @src:char 7 | * Return:char 8 | */ 9 | 10 | char *_strcat(char *dest, char *src) 11 | { 12 | char *s = dest; 13 | 14 | while (*dest != '\0') 15 | { 16 | dest++; 17 | } 18 | 19 | while (*src != '\0') 20 | { 21 | *dest = *src; 22 | dest++; 23 | src++; 24 | } 25 | *dest = '\0'; 26 | return (s); 27 | } /*Prince Solomon*/ 28 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/1-strncat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strncat - concatenates two strings 5 | * 6 | * @src: The source of strings 7 | * @dest: The destination of the string 8 | * @n: The length of int 9 | * 10 | * Return: pointer to the resulting string dest 11 | */ 12 | char *_strncat(char *dest, char *src, int n) 13 | { 14 | int i, j; 15 | 16 | for (i = 0; dest[i] != '\0'; i++) 17 | { 18 | continue; 19 | } 20 | for (j = 0; src[j] != '\0' && j < n; j++) 21 | { 22 | dest[i + j] = src[j]; 23 | } 24 | dest[i + j] = '\0'; 25 | return (dest); 26 | } 27 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/100-rot13.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * rot13 - encodes a string into rot13 5 | * @s: string to encode 6 | * 7 | * Return: address of s 8 | */ 9 | char *rot13(char *s) 10 | { 11 | int i, j; 12 | char a[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; 13 | char b[] = "nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM"; 14 | 15 | for (i = 0; *(s + i); i++) 16 | { 17 | for (j = 0; j < 52; j++) 18 | { 19 | if (a[j] == *(s + i)) 20 | { 21 | *(s + i) = b[j]; 22 | break; 23 | } 24 | } 25 | } 26 | return (s); 27 | } /*I'm a Software Engineer*/ 28 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/101-print_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_number - Prints any integer with putchar 5 | * @n: Number to prints 6 | * 7 | * Return: Nothing 8 | */ 9 | void print_number(int n) 10 | { 11 | unsigned int x; 12 | 13 | if (n < 0) 14 | { 15 | _putchar('-'); 16 | n *= -1; 17 | } 18 | 19 | x = n; 20 | 21 | if (x / 10) 22 | print_number(x / 10); 23 | 24 | _putchar(x % 10 + '0'); 25 | } 26 | -------------------------------------------------------------------------------- /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/2-strncpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | *_strncpy - function that copies a string 5 | * 6 | * @src: The source of strings 7 | * @dest: The destination of the string 8 | * @n: The length of int 9 | * 10 | * Return: pointer to the resulting string dest 11 | */ 12 | char *_strncpy(char *dest, char *src, int n) 13 | { 14 | int i; 15 | 16 | for (i = 0; i < n && *(src + i); i++) 17 | { 18 | *(dest + i) = *(src + i); 19 | } 20 | for (; i < n; i++) 21 | { 22 | *(dest + i) = '\0'; 23 | } 24 | return (dest); 25 | 26 | } 27 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/3-strcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strcmp - compare 2 string 4 | * @s1:string 5 | * @s2:strmp 6 | * Return:int 7 | */ 8 | int _strcmp(char *s1, char *s2) 9 | { 10 | while (((*s1 != '\0') && (*s2 != '\0')) && (*s1 == *s2)) 11 | { 12 | s1++; 13 | s2++; 14 | } 15 | 16 | if (*s1 == *s2) 17 | { 18 | return (0); 19 | } 20 | 21 | else 22 | { 23 | return (*s1 - *s2); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/4-rev_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * reverse_array - reverse array 4 | * @a:array 5 | * @n:integer 6 | * Return:void 7 | */ 8 | void reverse_array(int *a, int n) 9 | { 10 | int i, c; 11 | 12 | for (i = 0; (i < (n - 1) / 2); i++) 13 | { 14 | c = a[i]; 15 | a[i] = a[n - 1 - i]; 16 | a[n - 1 - i] = c; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/5-string_toupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * string_toupper - change lowercase to uppercase 4 | * @s:string 5 | * Return:char 6 | */ 7 | char *string_toupper(char *s) 8 | { 9 | 10 | int i; 11 | 12 | i = 0; 13 | while (*(s + i)) 14 | { 15 | if (*(s + i) >= 'a' && *(s + i) <= 'z') 16 | *(s + i) -= 'a' - 'A'; 17 | i++; 18 | } 19 | return (s); 20 | } 21 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/6-cap_string.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * cap_string - capitalizes all words in a string 5 | * @s: string 6 | * Return: address of s 7 | */ 8 | char *cap_string(char *s) 9 | { 10 | int i = 0, j; 11 | char a[] = " \t\n,;.!?\"(){}"; 12 | 13 | while (*(s + i)) 14 | { 15 | if (*(s + i) >= 'a' && *(s + i) <= 'z') 16 | { 17 | if (i == 0) 18 | *(s + i) -= 'a' - 'A'; 19 | else 20 | { 21 | for (j = 0; j <= 12; j++) 22 | { 23 | if (a[j] == *(s + i - 1)) 24 | *(s + i) -= 'a' - 'A'; 25 | } 26 | } 27 | } 28 | i++; 29 | } 30 | return (s); 31 | } 32 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/7-leet.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * leet - encodes a string into 1337 5 | * @s: string to encode 6 | * 7 | * Return: address of s 8 | */ 9 | char *leet(char *s) 10 | { 11 | int i, j; 12 | char a[] = "aAeEoOtTlL"; 13 | char b[] = "4433007711"; 14 | 15 | for (i = 0; *(s + i); i++) 16 | { 17 | for (j = 0; j <= 9; j++) 18 | { 19 | if (a[j] == s[i]) 20 | s[i] = b[j]; 21 | } 22 | } 23 | return (s); 24 | } 25 | /* Doing hard things is a must (I'm Prince) */ 26 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/8-rot13.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | /** 4 | * rot13 - encodes string to rot13 5 | * @s: string to encode 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | char *rot13(char *s) 11 | { 12 | int i, j; 13 | char rot13a[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; 14 | char rot13b[] = "NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm"; 15 | 16 | j = 0; 17 | while (s[j] != 0) 18 | { 19 | for (i = 0; i < 52; i++) 20 | { 21 | if (s[j] == rot13a[i]) 22 | { 23 | s[j] = rot13b[i]; 24 | break; 25 | } 26 | } 27 | j++; 28 | } 29 | return (s); 30 | } 31 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | /*Prince*/ 5 | 6 | int _putchar(char c); 7 | char *_strcat(char *dest, char *src); 8 | char *_strncat(char *dest, char *src, int n); 9 | char *_strncpy(char *dest, char *src, int n); 10 | int _strcmp(char *s1, char *s2); 11 | void reverse_array(int *a, int n); 12 | char *string_toupper(char *); 13 | char *cap_string(char *); 14 | char *leet(char *); 15 | char *rot13(char *); 16 | void print_number(int n); 17 | char *infinite_add(char *n1, char *n2, char *r, int size_r); 18 | void print_buffer(char *b, int size); 19 | 20 | #endif /*Solomon*/ 21 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/0-memset.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _memset - function fill the first @n bytes of the memory area pointed 5 | * to by @s with the constant byte @b 6 | * 7 | * @n: bytes of the memory area pointed to by @s 8 | * 9 | * @s: with the constant byte @b 10 | * 11 | * @b: memory area pointer 12 | * 13 | * Return: a pointer to the memory area @s 14 | */ 15 | char *_memset(char *s, char b, unsigned int n) 16 | { 17 | unsigned int i = 0; 18 | 19 | while (i < n) 20 | { 21 | s[i] = b; 22 | i++; 23 | } 24 | 25 | return (s); 26 | } 27 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/1-memcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _memcpy - function copies @n bytes from memory area @src 5 | * to memory area @dest 6 | * 7 | * @n: function copies 8 | * 9 | * @src: bytes from memory area 10 | * 11 | * @dest: to memory area 12 | * 13 | * Return: a pointer to @dest 14 | */ 15 | char *_memcpy(char *dest, char *src, unsigned int n) 16 | { 17 | unsigned int i = 0; 18 | 19 | while (i < n) 20 | { 21 | dest[i] = src[i]; 22 | i++; 23 | } 24 | 25 | return (dest); 26 | } 27 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/100-set_string.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * set_string - sets the value of a pointer to a char 4 | * @s: pointer to change 5 | * @to: string to change pointer to 6 | * 7 | * Return: void 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 - locates a character in a string 5 | * 6 | * @c: occurrence of the character 7 | * 8 | * @s: in the string 9 | * 10 | * Return: a pointer to the first occurrence of the character 11 | */ 12 | 13 | char *_strchr(char *s, char c) 14 | { 15 | while (*s) 16 | { 17 | if (*s != c) 18 | s++; 19 | else 20 | return (s); 21 | } 22 | if (c == '\0') 23 | return (s); 24 | 25 | return (NULL); 26 | } /**Prince Solomon*/ 27 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/3-strspn.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strspn - a function that gets the length of a prefix substring 5 | * 6 | * @s: the initial segment of 7 | * 8 | * @accept: which consist only of bytes from 9 | * 10 | * Return: the number of bytes 11 | */ 12 | 13 | unsigned int _strspn(char *s, char *accept) 14 | { 15 | int z = 0, x, y; 16 | 17 | for (x = 0; s[x] != '\0'; x++) 18 | { 19 | if (s[x] != 32) 20 | { 21 | for (y = 0; accept[y] != '\0'; y++) 22 | { 23 | if (s[x] == accept[y]) 24 | z++; 25 | } 26 | } 27 | else 28 | return (z); 29 | } 30 | return (z); 31 | 32 | } /**Code by: Princexz*/ 33 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/4-strpbrk.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strpbrk - function that searches a string for any of a set of bytes 5 | * 6 | * @s:first occurrence in the string 7 | * 8 | * @accept: matches one of the bytes, or @NULL if no such byte 9 | * 10 | * Return: a pointer to the byte 11 | */ 12 | char *_strpbrk(char *s, char *accept) 13 | { 14 | int i; 15 | while (*s) 16 | { 17 | for (i = 0; accept[i]; i++) 18 | { 19 | if (*s == accept[i]) 20 | return (s); 21 | } 22 | s++; 23 | } 24 | return (NULL); 25 | } 26 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/5-strstr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strstr - locates a substring 5 | * 6 | * @haystack: the longer string to search 7 | * @needle: the first occurrence of the substring 8 | * 9 | * Return: a pointer beg of substring or @Null if it not foound. 10 | */ 11 | char *_strstr(char *haystack, char *needle) 12 | { 13 | unsigned int i = 0, j = 0; 14 | 15 | while (haystack[i]) 16 | { 17 | while (needle[j] && (haystack[i] == needle[0])) 18 | { 19 | if (haystack[i + j] == needle[j]) 20 | j++; 21 | else 22 | break; 23 | } 24 | if (needle[j]) 25 | { 26 | i++; 27 | j = 0; 28 | } 29 | else 30 | return (haystack + i); 31 | } 32 | return (0); 33 | } 34 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/7-print_chessboard.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_chessboard - prints the chessboard 5 | * @a: pointer to pieces to print 6 | * 7 | * Return: void 8 | */ 9 | void print_chessboard(char (*a)[8]) 10 | { 11 | int i, j; 12 | 13 | for (i = 0; i < 8; i++) 14 | { 15 | for (j = 0; j < 8; j++) 16 | { 17 | _putchar(a[i][j]); 18 | } 19 | _putchar('\n'); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/8-print_diagsums.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * print_diagsums - prints the sums of the two diagonals of a square matrix 4 | * @a: pointer to start of matrix 5 | * @size: width of matrix column 6 | * 7 | * Return: void 8 | */ 9 | void print_diagsums(int *a, int size) 10 | { 11 | int i, j, p, l = 0, r = 0; 12 | 13 | for (i = 0; i < size; i++) 14 | { 15 | p = (i * size) + i; 16 | l += *(a + p); 17 | } 18 | for (j = 0; j < size; j++) 19 | { 20 | p = (j * size) + (size - 1 - j); 21 | r += *(a + p); 22 | } 23 | printf("%i, %i\n", l, r); 24 | } /**@prince Solomon*/ 25 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | 5 | #include 6 | #include 7 | int _putchar(char c); 8 | char *_memset(char *s, char b, unsigned int n); 9 | char *_memcpy(char *dest, char *src, unsigned int n); 10 | char *_strchr(char *s, char c); 11 | unsigned int _strspn(char *s, char *accept); 12 | char *_strpbrk(char *s, char *accept); 13 | char *_strstr(char *haystack, char *needle); 14 | void print_chessboard(char (*a)[8]); 15 | void print_diagsums(int *a, int size); 16 | void set_string(char **s, char *to); 17 | #endif/*MAIN_H*/ 18 | -------------------------------------------------------------------------------- /0x08-recursion/.0-puts_recursion.c.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x08-recursion/.0-puts_recursion.c.swp -------------------------------------------------------------------------------- /0x08-recursion/0-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * main - check the code 4 | * Return: Always 0. 5 | */ 6 | int main(void) 7 | { 8 | _puts_recursion("Puts with recursion"); 9 | return (0); 10 | } 11 | -------------------------------------------------------------------------------- /0x08-recursion/0-puts_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _puts_recursion - print a string 4 | * @s:string 5 | * Return:void 6 | */ 7 | void _puts_recursion(char *s) 8 | { 9 | if (*s) 10 | { 11 | _putchar(*s); 12 | _puts_recursion(s + 1); 13 | } 14 | else 15 | { 16 | _putchar('\n'); 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /0x08-recursion/1-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * main - check the code 4 | * Return: Always 0. 5 | */ 6 | int main(void) 7 | { 8 | _print_rev_recursion("\nColton Walker"); 9 | return (0); 10 | } 11 | -------------------------------------------------------------------------------- /0x08-recursion/1-print_rev_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _print_rev_recursion - print a string in reverse 4 | * @s: string 5 | * Return 0; 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-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * main - check the code 5 | * Return: Always 0. 6 | */ 7 | int main(void) 8 | { 9 | int r; 10 | 11 | r = is_palindrome("level"); 12 | printf("%d\n", r); 13 | r = is_palindrome("redder"); 14 | printf("%d\n", r); 15 | r = is_palindrome("test"); 16 | printf("%d\n", r); 17 | r = is_palindrome("step on no pets"); 18 | printf("%d\n", r); 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /0x08-recursion/2-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * main - check the code 5 | * Return: Always 0. 6 | */ 7 | int main(void) 8 | { 9 | int n; 10 | 11 | n = _strlen_recursion("Corbin Coleman"); 12 | printf("%d\n", n); 13 | return (0); 14 | } 15 | -------------------------------------------------------------------------------- /0x08-recursion/2-strlen_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strlen_recursion - length of string 4 | * @s:string 5 | * Return:int 6 | */ 7 | int _strlen_recursion(char *s) 8 | { 9 | int i = 0; 10 | 11 | if (*s) 12 | { 13 | i++; 14 | i += _strlen_recursion(s + 1); 15 | 16 | } 17 | return (i); 18 | } 19 | -------------------------------------------------------------------------------- /0x08-recursion/3-factorial.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * factorial - factorial of int 4 | * @n:int 5 | * Return:int 6 | */ 7 | int factorial(int n) 8 | { 9 | int x; 10 | 11 | if (n == 0) 12 | { 13 | return (1); 14 | } 15 | else if (n < 0) 16 | { 17 | return (-1); 18 | } 19 | else 20 | { 21 | x = n * factorial(n - 1); 22 | } 23 | return (x); 24 | 25 | } 26 | -------------------------------------------------------------------------------- /0x08-recursion/3-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * main - check the code 5 | * Return: Always 0. 6 | */ 7 | int main(void) 8 | { 9 | int r; 10 | 11 | r = factorial(1); 12 | printf("%d\n", r); 13 | r = factorial(5); 14 | printf("%d\n", r); 15 | r = factorial(10); 16 | printf("%d\n", r); 17 | r = factorial(-1024); 18 | printf("%d\n", r); 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /0x08-recursion/4-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * main - check the code 5 | * Return: Always 0. 6 | */ 7 | int main(void) 8 | { 9 | int r; 10 | 11 | r = _pow_recursion(1, 10); 12 | printf("%d\n", r); 13 | r = _pow_recursion(1024, 0); 14 | printf("%d\n", r); 15 | r = _pow_recursion(2, 16); 16 | printf("%d\n", r); 17 | r = _pow_recursion(5, 2); 18 | printf("%d\n", r); 19 | r = _pow_recursion(5, -2); 20 | printf("%d\n", r); 21 | r = _pow_recursion(-5, 3); 22 | printf("%d\n", r); 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x08-recursion/4-pow_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _pow_recursion - power 4 | * @x:int 5 | * @y:int 6 | * Return:int 7 | */ 8 | 9 | int _pow_recursion(int x, int y) 10 | { 11 | if (y < 0) 12 | { 13 | return (-1); 14 | } 15 | else if (y != 0) 16 | return (x * _pow_recursion(x, y - 1)); 17 | 18 | else 19 | { 20 | return (1); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /0x08-recursion/5-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * main - check the code 5 | * Return: Always 0. 6 | */ 7 | int main(void) 8 | { 9 | int r; 10 | 11 | r = _sqrt_recursion(1); 12 | printf("%d\n", r); 13 | r = _sqrt_recursion(1024); 14 | printf("%d\n", r); 15 | r = _sqrt_recursion(16); 16 | printf("%d\n", r); 17 | r = _sqrt_recursion(17); 18 | printf("%d\n", r); 19 | r = _sqrt_recursion(25); 20 | printf("%d\n", r); 21 | r = _sqrt_recursion(-1); 22 | printf("%d\n", r); 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x08-recursion/5-sqrt_recursion.c: -------------------------------------------------------------------------------- 1 | 2 | #include "main.h" 3 | 4 | /** 5 | * check - checks for the square root 6 | * @a:int 7 | * @b:int 8 | * 9 | * Return: int 10 | */ 11 | int check(int a, int b) 12 | { 13 | if (a * a == b) 14 | return (a); 15 | if (a * a > b) 16 | return (-1); 17 | return (check(a + 1, b)); 18 | } 19 | 20 | /** 21 | * _sqrt_recursion - returns the natural square root of a number 22 | * @n: integer to find sqrt of 23 | * Return: natural square root or -1 24 | */ 25 | int _sqrt_recursion(int n) 26 | { 27 | if (n == 0) 28 | return (0); 29 | return (check(1, n)); 30 | } 31 | -------------------------------------------------------------------------------- /0x08-recursion/6-is_prime_number.c: -------------------------------------------------------------------------------- 1 | 2 | #include "main.h" 3 | 4 | /** 5 | * check - checks to see if number is prime 6 | * @a:int 7 | * @b:int 8 | * Return:int 9 | */ 10 | int check(int a, int b) 11 | { 12 | if (b < 2 || b % a == 0) 13 | return (0); 14 | else if (a > b / 2) 15 | return (1); 16 | else 17 | return (check(a + 1, b)); 18 | } 19 | 20 | /** 21 | * is_prime_number - states if number is prime 22 | * @n:int 23 | * Return:int 24 | */ 25 | int is_prime_number(int n) 26 | { 27 | if (n == 2) 28 | return (1); 29 | return (check(2, n)); 30 | } 31 | -------------------------------------------------------------------------------- /0x08-recursion/6-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * main - check the code 5 | * Return: Always 0. 6 | */ 7 | int main(void) 8 | { 9 | int r; 10 | 11 | r = is_prime_number(1); 12 | printf("%d\n", r); 13 | r = is_prime_number(1024); 14 | printf("%d\n", r); 15 | r = is_prime_number(16); 16 | printf("%d\n", r); 17 | r = is_prime_number(17); 18 | printf("%d\n", r); 19 | r = is_prime_number(25); 20 | printf("%d\n", r); 21 | r = is_prime_number(-1); 22 | printf("%d\n", r); 23 | r = is_prime_number(113); 24 | printf("%d\n", r); 25 | r = is_prime_number(7919); 26 | printf("%d\n", r); 27 | return (0); 28 | } 29 | -------------------------------------------------------------------------------- /0x08-recursion/main.h: -------------------------------------------------------------------------------- 1 | int _putchar(char c); 2 | void _puts_recursion(char *s); 3 | void _print_rev_recursion(char *s); 4 | int _strlen_recursion(char *s); 5 | int factorial(int n); 6 | int _pow_recursion(int x, int y); 7 | int _sqrt_recursion(int n); 8 | int is_prime_number(int n); 9 | int is_palindrome(char *s); 10 | int wildcmp(char *s1, char *s2); 11 | -------------------------------------------------------------------------------- /0x09-static_libraries/0-isupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _isupper - checks whether a given input is an uppercase letter 4 | * @c: a parameter 5 | * Return: 1 if c is uppercase, 0 otherwise 6 | */ 7 | int _isupper(int c) 8 | { 9 | if (c >= 65 && c <= 90) 10 | { 11 | return (1); 12 | } 13 | else 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x09-static_libraries/0-isupper.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x09-static_libraries/0-isupper.o -------------------------------------------------------------------------------- /0x09-static_libraries/0-memset.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * *_memset - fills memory with a constant byte 4 | * @s: area to be filled 5 | * @b: constant to be filled 6 | * @n: number of bytes of s to be filled 7 | * Return: pointer to memory area s 8 | */ 9 | char *_memset(char *s, char b, unsigned int n) 10 | { 11 | unsigned int i; 12 | char *memory = s; 13 | unsigned int value = b; 14 | 15 | for (i = 0; i < n; i++) 16 | { 17 | memory[i] = value; 18 | } 19 | return (memory); 20 | }/**Princexz*/ 21 | -------------------------------------------------------------------------------- /0x09-static_libraries/0-memset.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x09-static_libraries/0-memset.o -------------------------------------------------------------------------------- /0x09-static_libraries/0-strcat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * _strcat - concatenates two strings 5 | * @dest: pointer to a string dest 6 | * @src: pointer to a string src 7 | * Return: string pointer 8 | */ 9 | char *_strcat(char *dest, char *src) 10 | { 11 | int dest_len; 12 | char *temp; 13 | 14 | dest_len = strlen(dest); 15 | temp = dest; 16 | temp += dest_len; 17 | 18 | while (*src != '\0') 19 | { 20 | *temp = *src; 21 | ++temp; 22 | ++src; 23 | } 24 | *temp = '\0'; 25 | return (dest); 26 | } 27 | -------------------------------------------------------------------------------- /0x09-static_libraries/0-strcat.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x09-static_libraries/0-strcat.o -------------------------------------------------------------------------------- /0x09-static_libraries/1-isdigit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isdigit - checks whether a given input is a digit 5 | * @c: a parameter 6 | * @c: a parameter 7 | * Return: 1 if c is a digit, 0 otherwise 8 | */ 9 | int _isdigit(int c) 10 | { 11 | if (c >= 48 && c <= 57) 12 | { 13 | return (1); 14 | } 15 | else 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x09-static_libraries/1-isdigit.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x09-static_libraries/1-isdigit.o -------------------------------------------------------------------------------- /0x09-static_libraries/1-memcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _memcpy - function to copy memory 4 | * @dest: memory to copy to 5 | * @src: memory t0 copy from 6 | * @n: number of bytes to copy 7 | * Return: return pointer to dest memory 8 | */ 9 | char *_memcpy(char *dest, char *src, unsigned int n) 10 | { 11 | char *from = src; 12 | 13 | char *to = dest; 14 | 15 | unsigned int index; 16 | 17 | for (index = 0; index < n; index++) 18 | { 19 | to[index] = from[index]; 20 | } 21 | return (dest); 22 | } 23 | -------------------------------------------------------------------------------- /0x09-static_libraries/1-memcpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x09-static_libraries/1-memcpy.o -------------------------------------------------------------------------------- /0x09-static_libraries/1-strncat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strncat - concatenates two strings 4 | * @dest: the destination value 5 | * @src: the source value 6 | * @n: the limit of the concatenation 7 | * Return: a pointer to the resulting string dest 8 | */ 9 | char *_strncat(char *dest, char *src, int n) 10 | { 11 | int index = 0, dest_len = 0; 12 | 13 | while (dest[index++]) 14 | dest_len++; 15 | 16 | for (index = 0; src[index] && index < n; index++) 17 | dest[dest_len++] = src[index]; 18 | 19 | return (dest); 20 | } 21 | -------------------------------------------------------------------------------- /0x09-static_libraries/1-strncat.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x09-static_libraries/1-strncat.o -------------------------------------------------------------------------------- /0x09-static_libraries/100-atoi.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _atoi - converts a string to an integer. 4 | * @s: input string. 5 | * Return: integer. 6 | */ 7 | int _atoi(char *s) 8 | { 9 | unsigned int count = 0, size = 0, oi = 0, pn = 1, m = 1, i; 10 | 11 | while (*(s + count) != '\0') 12 | { 13 | if (size > 0 && (*(s + count) < '0' || *(s + count) > '9')) 14 | break; 15 | 16 | if (*(s + count) == '-') 17 | pn *= -1; 18 | 19 | if ((*(s + count) >= '0') && (*(s + count) <= '9')) 20 | { 21 | if (size > 0) 22 | m *= 10; 23 | size++; 24 | } 25 | count++; 26 | } 27 | 28 | for (i = count - size; i < count; i++) 29 | { 30 | oi = oi + ((*(s + i) - 48) * m); 31 | m /= 10; 32 | } 33 | return (oi * pn); 34 | } 35 | -------------------------------------------------------------------------------- /0x09-static_libraries/100-atoi.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x09-static_libraries/100-atoi.o -------------------------------------------------------------------------------- /0x09-static_libraries/2-strchr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strchr - function to search for occurrence of a character in a string 4 | * @s: string pointer 5 | * @c: character to search for 6 | * Return: pointer to character if characer was found, else return null 7 | */ 8 | char *_strchr(char *s, char c) 9 | { 10 | int index; 11 | 12 | for (index = 0; s[index] >= '\0'; index++) 13 | { 14 | if (s[index] == c) 15 | return (s + index); 16 | } 17 | return ('\0'); 18 | } 19 | -------------------------------------------------------------------------------- /0x09-static_libraries/2-strchr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x09-static_libraries/2-strchr.o -------------------------------------------------------------------------------- /0x09-static_libraries/2-strlen.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strlen - prints the length of a string 4 | * @s: pointer to an array of characters 5 | * Return: always 0 (success) 6 | */ 7 | int _strlen(char *s) 8 | { 9 | int i = 0; 10 | 11 | while (*(s + i) != '\0') 12 | { 13 | i++; 14 | } 15 | return (i); 16 | } 17 | -------------------------------------------------------------------------------- /0x09-static_libraries/2-strlen.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x09-static_libraries/2-strlen.o -------------------------------------------------------------------------------- /0x09-static_libraries/2-strncpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strncpy - C function that copies a string, including the 4 | * terminating null byte, using at most an inputted number of bytes. 5 | * If the length of the source string is less than the maximum byte number 6 | * the remainder of the destination string is filled with null bytes. 7 | * Works identically to the standard library function `strncpy`. 8 | * @dest: buffer storing the string copy 9 | * @src:the source string 10 | * @n:max nummber of byte copied 11 | * Return: returns 12 | */ 13 | char *_strncpy(char *dest, char *src, int n) 14 | { 15 | int i; 16 | 17 | for (i = 0; i < n && src[i] != '\0'; i++) 18 | dest[i] = src[i]; 19 | 20 | for ( ; i < n; i++) 21 | dest[i] = '\0'; 22 | 23 | return (dest); 24 | } 25 | -------------------------------------------------------------------------------- /0x09-static_libraries/2-strncpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x09-static_libraries/2-strncpy.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-islower.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _islower - checksfor lowercase character 4 | * @c: the integer value it receives 5 | * Return: 1 if charcacter c is lowercase, 6 | * 0 if false 7 | */ 8 | int _islower(int c) 9 | { 10 | if (c >= 'a' && c <= 'z') 11 | { 12 | return (1); 13 | } 14 | else 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-islower.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x09-static_libraries/3-islower.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-puts.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _puts - prints a string followed by a newline 4 | * to stdout 5 | * @str: parameter that contains a string 6 | * Return: void 7 | */ 8 | void _puts(char *str) 9 | { 10 | int i = 0; 11 | 12 | while (str[i]) 13 | { 14 | _putchar(str[i]); 15 | i++; 16 | } 17 | _putchar('\n'); 18 | } 19 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-puts.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x09-static_libraries/3-puts.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-strcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strcmp - Function which compare two strings 4 | * @s1: first string 5 | * @s2:second string 6 | * Return: int value 7 | */ 8 | int _strcmp(char *s1, char *s2) 9 | { 10 | int i = 0, ret; 11 | 12 | for (i = 0; s1[i] && s2[i]; i++) 13 | { 14 | if (s1[i] == s2[i]) 15 | continue; 16 | else 17 | break; 18 | } 19 | 20 | ret = s1[i] - s2[i]; 21 | return (ret); 22 | } 23 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-strcmp.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x09-static_libraries/3-strcmp.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-strspn.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strspn - function calculates the length (in bytes) of the is which consists 4 | * entirely of bytes in accept 5 | * @s: the string to check 6 | * @accept: the bytes to count 7 | * Return: number of bytes 8 | */ 9 | unsigned int _strspn(char *s, char *accept) 10 | { 11 | int count = 0; 12 | 13 | int index; 14 | 15 | while (*s) 16 | { 17 | for (index = 0; accept[index]; index++) 18 | { 19 | if (*s == accept[index]) 20 | { 21 | count++; 22 | break; 23 | } 24 | else if (accept[index + 1] == '\0') 25 | return (count); 26 | } 27 | s++; 28 | 29 | } 30 | return (count); 31 | } 32 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-strspn.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x09-static_libraries/3-strspn.o -------------------------------------------------------------------------------- /0x09-static_libraries/4-isalpha.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _isalpha - checks whether given character is an alphabet 4 | * @c: an argument 5 | * Return: 1 if c is alphabet, 0 otherwise 6 | */ 7 | int _isalpha(int c) 8 | { 9 | if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) 10 | { 11 | return (1); 12 | } 13 | else 14 | { 15 | return (0); 16 | } 17 | _putchar('\n'); 18 | } 19 | -------------------------------------------------------------------------------- /0x09-static_libraries/4-isalpha.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x09-static_libraries/4-isalpha.o -------------------------------------------------------------------------------- /0x09-static_libraries/4-strpbrk.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strpbrk - locates the first occurrence in the string s of any of 4 | * the bytes in the string accept. 5 | * @s: string to pick char 6 | * @accept: string to compare to 7 | * Return: string pointer 8 | */ 9 | char *_strpbrk(char *s, char *accept) 10 | { 11 | int index; 12 | 13 | while (*s) 14 | { 15 | for (index = 0; accept[index]; index++) 16 | { 17 | if (*s == accept[index]) 18 | return (s); 19 | } 20 | s++; 21 | } 22 | return ('\0'); 23 | } 24 | -------------------------------------------------------------------------------- /0x09-static_libraries/4-strpbrk.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x09-static_libraries/4-strpbrk.o -------------------------------------------------------------------------------- /0x09-static_libraries/5-strstr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strstr - extract substring 4 | * @haystack: param 1 5 | * @needle: param 2 6 | * Return: haystack 7 | */ 8 | char *_strstr(char *haystack, char *needle) 9 | { 10 | int index; 11 | 12 | if (*needle == 0) 13 | return (haystack); 14 | 15 | while (*haystack) 16 | { 17 | index = 0; 18 | 19 | if (haystack[index] == needle[index]) 20 | { 21 | do { 22 | if (needle[index + 1] == '\0') 23 | return (haystack); 24 | 25 | index++; 26 | } while (haystack[index] == needle[index]); 27 | } 28 | haystack++; 29 | } 30 | return ('\0'); 31 | } 32 | -------------------------------------------------------------------------------- /0x09-static_libraries/5-strstr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x09-static_libraries/5-strstr.o -------------------------------------------------------------------------------- /0x09-static_libraries/6-abs.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _abs - compute absolute value of integer 4 | * @n: integer argument 5 | * Return: absolute value 6 | */ 7 | int _abs(int n) 8 | { 9 | if (n > 0) 10 | return (n); 11 | else if (n < 0) 12 | return (-n); 13 | return (0); 14 | } 15 | -------------------------------------------------------------------------------- /0x09-static_libraries/6-abs.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x09-static_libraries/6-abs.o -------------------------------------------------------------------------------- /0x09-static_libraries/9-strcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strcpy - Copies a string pointed to by @src, including the 4 | * terminating null byte, to a buffer pointed to by @dest. 5 | * @dest: A buffer to copy the string to. 6 | * @src: The source string to copy. 7 | * Return: A pointer to the destination string @dest. 8 | */ 9 | char *_strcpy(char *dest, char *src) 10 | { 11 | int i; 12 | 13 | for (i = 0; src[i]; i++) 14 | { 15 | dest[i] = src[i]; 16 | } 17 | dest[i] = src[i]; 18 | return (dest); 19 | } 20 | -------------------------------------------------------------------------------- /0x09-static_libraries/9-strcpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x09-static_libraries/9-strcpy.o -------------------------------------------------------------------------------- /0x09-static_libraries/README.md: -------------------------------------------------------------------------------- 1 | # 0x09. C - Static libraries 2 | 3 | ## Description 4 | What you should learn from this project: 5 | 6 | * What is a static library, how does it work, how to create one, and how to use it 7 | * Basic usage of ar, ranlib, nm 8 | 9 | --- 10 | 11 | ### [0. A library is not a luxury but one of the necessities of life](./libmy.a) 12 | * Create the static library libmy.a containing all the functions listed below: 13 | 14 | 15 | ### [1. Without libraries what have we? We have no past and no future](./create_static_lib.sh) 16 | * Create a script called create_static_lib.sh that creates a static library called liball.a from all the .c files that are in the current directory. 17 | 18 | 19 | --- 20 | 21 | ## Author 22 | * **Prince Solomon** - [princexz](https://github.com/princexz) 23 | -------------------------------------------------------------------------------- /0x09-static_libraries/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * _putchar - writes the character c to stdout 4 | * @c: The character to print 5 | * Return: On success 1. 6 | * On error, -1 is returned, and errno is set appropriately. 7 | */ 8 | int _putchar(char c) 9 | { 10 | return (write(1, &c, 1)); 11 | } 12 | -------------------------------------------------------------------------------- /0x09-static_libraries/_putchar.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/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/liball.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x09-static_libraries/liball.a -------------------------------------------------------------------------------- /0x09-static_libraries/libmy.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x09-static_libraries/libmy.a -------------------------------------------------------------------------------- /0x09-static_libraries/main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | int main(void) 3 | { 4 | _puts("\"At the end of the day, my goal was to be the best hacker\"\n\t- Kevin Mitnick"); 5 | return (0); 6 | } 7 | -------------------------------------------------------------------------------- /0x09-static_libraries/main.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x09-static_libraries/main.o -------------------------------------------------------------------------------- /0x0A-argc_argv/.100-change.c.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x0A-argc_argv/.100-change.c.swp -------------------------------------------------------------------------------- /0x0A-argc_argv/0-whatsmyname.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main - Prints the name of the program 5 | * followed by a new line 6 | * @argc : number of command-line arguments 7 | * @argv : string of argumenst passed to main fcn 8 | * Return: Always 0 (Success) 9 | */ 10 | int main(int __attribute__((__unused__)) argc, char *argv[]) 11 | { 12 | printf("%s\n", argv[0]); 13 | 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x0A-argc_argv/1-args.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main - Prints the number of arguments passed to the program 5 | * followed by a new line 6 | * @argc : number of command-line arguments 7 | * @argv : string of argumenst passed to main fcn 8 | * Return: Always 0 (Success) 9 | */ 10 | int main(int argc, char __attribute__((__unused__)) *argv[]) 11 | { 12 | printf("%d\n", argc - 1); 13 | 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x0A-argc_argv/2-args.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main - Prints the command line arguments it receives 5 | * followed by a new line 6 | * @argc : number of command-line arguments 7 | * @argv : string of argumenst passed to main fcn 8 | * Return: Always 0 (Success) 9 | */ 10 | int main(int argc, char *argv[]) 11 | { 12 | int index; 13 | 14 | for (index = 0; index < argc; index++) 15 | { 16 | printf("%s\n", argv[index]); 17 | } 18 | 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /0x0A-argc_argv/3-mul.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main - Prints the result of multiplying two numbers 5 | * followed by a new line 6 | * @argc : number of command-line arguments 7 | * @argv : string of argumenst passed to main fcn 8 | * Return: 0 (Success), 1 on failure 9 | */ 10 | int main(int argc, char *argv[]) 11 | { 12 | int arg1, arg2, product; 13 | 14 | if (argc != 3) 15 | { 16 | printf("Error\n"); 17 | return (1); 18 | } 19 | 20 | arg1 = atoi(argv[1]); 21 | arg2 = atoi(argv[2]); 22 | 23 | product = arg1 * arg2; 24 | 25 | printf("%d\n", product); 26 | 27 | return (0); 28 | } 29 | -------------------------------------------------------------------------------- /0x0A-argc_argv/4-add.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | /** 5 | * main - adds argument from the cmd 6 | * @argc: number of arguments 7 | * @argv: the arguments 8 | * Return: 0 on success, 1on fail 9 | */ 10 | int main(int argc, char *argv[]) 11 | { 12 | int sum, num, digit; 13 | 14 | sum = 0; 15 | 16 | for (num = 1; num < argc; num++) 17 | { 18 | for (digit = 0; argv[num][digit] != '\0'; digit++) 19 | { 20 | if (!isdigit(argv[num][digit])) 21 | { 22 | printf("Error\n"); 23 | return (1); 24 | } 25 | } 26 | sum += atoi(argv[num]); 27 | } 28 | printf("%d\n", sum); 29 | return (0); 30 | } 31 | -------------------------------------------------------------------------------- /0x0A-argc_argv/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | /** 4 | * Header file that contains needed function prototypes 5 | */ 6 | int _putchar(char c); 7 | 8 | #endif 9 | -------------------------------------------------------------------------------- /0x0B-malloc_free/0-create_array.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | /** 4 | * create_array - creates an array of chars and 5 | * initialises it with a specific char 6 | * @size: size of the array 7 | * @c: the character that initialises the array 8 | * Return: a pointer to the array, NULL on failure 9 | */ 10 | char *create_array(unsigned int size, char c) 11 | { 12 | unsigned int i; 13 | 14 | char *array = malloc(sizeof(char) * size); 15 | 16 | if (size == 0) 17 | return (NULL); 18 | 19 | if (array == NULL) 20 | return (NULL); 21 | /*Allocating char c to all array slots*/ 22 | for (i = 0; i < size; i++) 23 | array[i] = c; 24 | 25 | return (array); 26 | } /**Prince solomon*/ 27 | -------------------------------------------------------------------------------- /0x0B-malloc_free/1-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | /** 5 | * main - check the code for ALX School students. 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | char *s; 11 | 12 | s = _strdup("ALX SE"); 13 | if (s == NULL) 14 | { 15 | printf("failed to allocate memory\n"); 16 | return (1); 17 | } 18 | printf("%s\n", s); 19 | free(s); 20 | return (0); 21 | }/*code by: Princexz*/ 22 | -------------------------------------------------------------------------------- /0x0B-malloc_free/1-strdup.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | /** 4 | * _strdup - returns a pointer to anewly allocated 5 | * space in memory which contains a copy of the 6 | * string given as a parameter 7 | * @str: string to be newly allocated in memory 8 | * Return: NULL if string is empty or insufficient memory 9 | * a pointer to the duplicated string (Success) 10 | */ 11 | char *_strdup(char *str) 12 | { 13 | char *dup; 14 | unsigned int i, len; 15 | 16 | i = 0; 17 | len = 0; 18 | 19 | if (str == NULL) 20 | return (NULL); 21 | 22 | while (str[len]) 23 | len++; 24 | 25 | dup = malloc(sizeof(char) * (len + 1)); 26 | 27 | if (dup == NULL) 28 | return (NULL); 29 | 30 | while ((dup[i] = str[i]) != '\0') 31 | i++; 32 | 33 | return (dup); 34 | } 35 | -------------------------------------------------------------------------------- /0x0B-malloc_free/100-argstostr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * argstostr - cats cmdline arguments t string 5 | * @ac: no. of args 6 | * @av: the args 7 | * Return: string 8 | */ 9 | char *argstostr(int ac, char **av) 10 | { 11 | char *str; 12 | int arg, byte, index, size = ac; 13 | 14 | if (ac == 0 || av == NULL) 15 | return (NULL); 16 | 17 | for (arg = 0; arg < ac; arg++) 18 | { 19 | for (byte = 0; av[arg][byte]; byte++) 20 | size++; 21 | } 22 | 23 | str = malloc(sizeof(char) * size + 1); 24 | 25 | if (str == NULL) 26 | return (NULL); 27 | 28 | index = 0; 29 | 30 | for (arg = 0; arg < ac; arg++) 31 | { 32 | for (byte = 0; av[arg][byte]; byte++) 33 | str[index++] = av[arg][byte]; 34 | 35 | str[index++] = '\n'; 36 | } 37 | str[size] = '\0'; 38 | return (str); 39 | } 40 | -------------------------------------------------------------------------------- /0x0B-malloc_free/100-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | /** 5 | * main - check the code for ALX School students. 6 | * Return: Always 0. 7 | */ 8 | int main(int ac, char *av[]) 9 | { 10 | char *s; 11 | 12 | s = argstostr(ac, av); 13 | if (s == NULL) 14 | { 15 | return (1); 16 | } 17 | printf("%s", s); 18 | free(s); 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /0x0B-malloc_free/101-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * print_tab - Prints an array of string 7 | * @tab: The array to print 8 | * Return: nothing 9 | */ 10 | void print_tab(char **tab) 11 | { 12 | int i; 13 | 14 | for (i = 0; tab[i] != NULL; ++i) 15 | { 16 | printf("%s\n", tab[i]); 17 | } 18 | } 19 | 20 | /** 21 | * main - check the code for ALX School students. 22 | * Return: 1 if an error occurred, 0 otherwise 23 | */ 24 | int main(void) 25 | { 26 | char **tab; 27 | 28 | tab = strtow(" ALX School #cisfun "); 29 | if (tab == NULL) 30 | { 31 | printf("Failed\n"); 32 | return (1); 33 | } 34 | print_tab(tab); 35 | return (0); 36 | }/*Princexz*/ 37 | -------------------------------------------------------------------------------- /0x0B-malloc_free/2-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | /** 5 | * main - check the code for ALX School students. 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | char *s; 11 | 12 | s = str_concat("Betty ", "Holberton"); 13 | if (s == NULL) 14 | { 15 | printf("failed\n"); 16 | return (1); 17 | } 18 | printf("%s\n", s); 19 | free(s); 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /0x0B-malloc_free/4-free_grid.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | /** 4 | * free_grid - frees the memory allocate for the grid 5 | * created by alloc_grid() 6 | * @grid: grid to free 7 | * @height: height of the grid 8 | */ 9 | void free_grid(int **grid, int height) 10 | { 11 | int i; 12 | 13 | if (grid == NULL || height == 0) 14 | return; 15 | 16 | for (i = 0; i < height; i++) 17 | free(grid[i]); 18 | 19 | free(grid); 20 | } 21 | -------------------------------------------------------------------------------- /0x0B-malloc_free/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * _putchar - writes the character c to stdout 4 | * @c: The character to print 5 | * Return: On success 1. 6 | * On error, -1 is returned, and errno is set appropriately. 7 | */ 8 | int _putchar(char c) 9 | { 10 | return (write(1, &c, 1)); 11 | } 12 | -------------------------------------------------------------------------------- /0x0B-malloc_free/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | /** 4 | * This is a header file that contains function 5 | * prototypes that will be used in this project 6 | */ 7 | char *create_array(unsigned int size, char c); 8 | char *_strdup(char *str); 9 | char *str_concat(char *s1, char *s2); 10 | int **alloc_grid(int width, int height); 11 | void free_grid(int **grid, int height); 12 | char *argstostr(int ac, char **av); 13 | char **strtow(char *str); 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/0-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | #include 5 | /** 6 | * main - check the code 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char *c; 12 | int *i; 13 | float *f; 14 | double *d; 15 | 16 | c = malloc_checked(sizeof(char) * 1024); 17 | printf("%p\n", (void *)c); 18 | i = malloc_checked(sizeof(int) * 402); 19 | printf("%p\n", (void *)i); 20 | f = malloc_checked(sizeof(float) * 100000000); 21 | printf("%p\n", (void *)f); 22 | d = malloc_checked(INT_MAX); 23 | printf("%p\n", (void *)d); 24 | free(c); 25 | free(i); 26 | free(f); 27 | free(d); 28 | return (0); 29 | } 30 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/0-malloc_checked.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | /** 4 | * malloc_checked - allocates memory using malloc 5 | * @b: number of bytes in memory to allocate 6 | * Return: pointer to allocated memory(Success), 98 on failure 7 | */ 8 | void *malloc_checked(unsigned int b) 9 | { 10 | void *ptr; 11 | 12 | ptr = malloc(b); 13 | 14 | if (ptr == NULL) 15 | exit(98); 16 | 17 | return (ptr); 18 | } 19 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/1-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | /** 5 | * main - check the code 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | char *concat; 11 | 12 | concat = string_nconcat("Best ", "School !!!", 6); 13 | printf("%s\n", concat); 14 | free(concat); 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/2-calloc.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | /** 4 | * _calloc - implements calloc with malloc 5 | * Allocates memory for an array using malloc 6 | * @nmemb: number of elements in the array 7 | * @size: the size of the array to be created 8 | * Return: NULL if nmemb/size is 0, 9 | * if malloc fails, return NULL, 10 | */ 11 | void *_calloc(unsigned int nmemb, unsigned int size) 12 | { 13 | void *memory; 14 | 15 | char *buffer; 16 | 17 | unsigned int i; 18 | 19 | if (nmemb == 0 || size == 0) 20 | return (NULL); 21 | 22 | memory = malloc(nmemb * size); 23 | 24 | if (memory == NULL) 25 | return (NULL); 26 | 27 | buffer = memory; 28 | 29 | for (i = 0; i < (nmemb * size); i++) 30 | buffer[i] = '\0'; 31 | 32 | return (memory); 33 | } 34 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/3-array_range.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * array_range - Creates an array of integers ordered 5 | * from min to max, inclusive. 6 | * @min: The first value of the array. 7 | * @max: The last value of the array. 8 | * Return: If min > max or the function fails - NULL. 9 | * Otherwise - a pointer to the newly created array. 10 | */ 11 | int *array_range(int min, int max) 12 | { 13 | int *array, index, size; 14 | 15 | if (min > max) 16 | return (NULL); 17 | 18 | size = max - min + 1; 19 | 20 | array = malloc(sizeof(int) * size); 21 | 22 | if (array == NULL) 23 | return (NULL); 24 | 25 | for (index = 0; index < size; index++) 26 | array[index] = min++; 27 | 28 | return (array); 29 | } 30 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * _putchar - writes the character c to stdout 4 | * @c: The character to print 5 | * Return: On success 1. 6 | * On error, -1 is returned, and errno is set appropriately. 7 | */ 8 | int _putchar(char c) 9 | { 10 | return (write(1, &c, 1)); 11 | } /** Prince Solomon */ 12 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | /** 4 | * This is a header file that contains function 5 | * prototypes that will be used in this project 6 | */ 7 | int _putchar(char c); 8 | void *malloc_checked(unsigned int b); 9 | char *string_nconcat(char *s1, char *s2, unsigned int n); 10 | void *_calloc(unsigned int nmemb, unsigned int size); 11 | int *array_range(int min, int max); 12 | void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size); 13 | void errors(void); 14 | int _strlen(char *s); 15 | int is_digit(char *s); 16 | 17 | #endif /**Prince Solomon*/ 18 | -------------------------------------------------------------------------------- /0x0D-preprocessor/0-main.c: -------------------------------------------------------------------------------- 1 | #include "0-object_like_macro.h" 2 | #include "0-object_like_macro.h" 3 | #include 4 | /** 5 | * main - check the code 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | int s; 11 | 12 | s = 98 + SIZE; 13 | printf("%d\n", s); 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x0D-preprocessor/0-object_like_macro.h: -------------------------------------------------------------------------------- 1 | #ifndef _OBJECT_LIKE_MACRO_ 2 | #define _OBJECT_LIKE_MACRO_ 3 | 4 | #define SIZE 1024 5 | 6 | #endif /**Prince Solomon*/ 7 | -------------------------------------------------------------------------------- /0x0D-preprocessor/1-main.c: -------------------------------------------------------------------------------- 1 | #include "1-pi.h" 2 | #include "1-pi.h" 3 | #include 4 | /** 5 | * main - check the code 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | float a; 11 | float r; 12 | 13 | r = 98; 14 | a = PI * r * r; 15 | printf("%.3f\n", a); 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x0D-preprocessor/1-pi.h: -------------------------------------------------------------------------------- 1 | #ifndef _PI_ 2 | #define _PI_ 3 | 4 | #define PI 3.14159265359 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /0x0D-preprocessor/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints the name of the file. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | printf("%s\n", __FILE__); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x0D-preprocessor/3-function_like_macro.h: -------------------------------------------------------------------------------- 1 | #ifndef _FUNCTION_LIKE_MACRO_ 2 | #define _FUNCTION_LIKE_MACRO_ 3 | 4 | #define ABS(x) ((x) < (0) ? ((x) * (-1)) : (x)) 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /0x0D-preprocessor/3-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "3-function_like_macro.h" 3 | #include "3-function_like_macro.h" 4 | /** 5 | * main - check the code 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | int i; 11 | int j; 12 | 13 | i = ABS(-98) * 10; 14 | j = ABS(98) * 10; 15 | printf("%d, %d\n", i, j); 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x0D-preprocessor/4-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "4-sum.h" 3 | #include "4-sum.h" 4 | /** 5 | * main - check the code 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | int s; 11 | 12 | s = SUM(98, 1024); 13 | printf("%d\n", s); 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x0D-preprocessor/4-sum.h: -------------------------------------------------------------------------------- 1 | #ifndef _SUM_LIKE_MACRO_ 2 | #define _SUM_LIKE_MACRO_ 3 | 4 | #define SUM(x, y) ((x) + (y)) 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /0x0D-preprocessor/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * _putchar - writes the character c to stdout 4 | * @c: The character to print 5 | * Return: On success 1. 6 | * On error, -1 is returned, and errno is set appropriately. 7 | */ 8 | int _putchar(char c) 9 | { 10 | return (write(1, &c, 1)); 11 | } 12 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | /** 4 | * main - check the code 5 | * Return: Always 0. 6 | */ 7 | int main(void) 8 | { 9 | struct dog my_dog; 10 | 11 | my_dog.name = "Poppy"; 12 | my_dog.age = 3.5; 13 | my_dog.owner = "Bob"; 14 | printf("My name is %s, and I am %.1f :) - Woof!\n", my_dog.name, my_dog.age); 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/1-init_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | 3 | /** 4 | * init_dog - initializes a variable of type struct dog. 5 | * @d: struct dog. 6 | * @name: name of the dog. 7 | * @age: age of the dog. 8 | * @owner: owner of the dog. 9 | * 10 | * Return: no return. 11 | */ 12 | void init_dog(struct dog *d, char *name, float age, char *owner) 13 | { 14 | if (d) 15 | { 16 | d->name = name; 17 | d->age = age; 18 | d->owner = owner; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | /** 4 | * main - check the code 5 | * Return: Always 0. 6 | */ 7 | int main(void) 8 | { 9 | struct dog my_dog; 10 | 11 | init_dog(&my_dog, "Poppy", 3.5, "Bob"); 12 | printf("My name is %s, and I am %.1f :) - Woof!\n", my_dog.name, my_dog.age); 13 | return (0); 14 | } 15 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | /** 4 | * main - check the code 5 | * Return: Always 0. 6 | */ 7 | int main(void) 8 | { 9 | struct dog my_dog; 10 | 11 | my_dog.name = "Poppy"; 12 | my_dog.age = 3.5; 13 | my_dog.owner = "Bob"; 14 | print_dog(&my_dog); 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/2-print_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | 4 | /** 5 | * print_dog - prints a struct dog. 6 | * @d: struct dog. 7 | * 8 | * Return: no return. 9 | */ 10 | void print_dog(struct dog *d) 11 | { 12 | if (d) 13 | { 14 | if (!(d->name)) 15 | printf("Name: (nil)\n"); 16 | else 17 | printf("Name: %s\n", d->name); 18 | 19 | printf("Age: %f\n", d->age); 20 | 21 | if (!(d->owner)) 22 | printf("Owner: (nil)\n"); 23 | else 24 | printf("Owner: %s\n", d->owner); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/3-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | /** 4 | * main - check the code 5 | * Return: Always 0. 6 | */ 7 | int main(void) 8 | { 9 | dog_t my_dog; 10 | 11 | my_dog.name = "Poppy"; 12 | my_dog.age = 3.5; 13 | my_dog.owner = "Bob"; 14 | printf("My name is %s, and I am %.1f :) - Woof!\n", my_dog.name, my_dog.age); 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/4-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | /** 4 | * main - check the code 5 | * Return: Always 0. 6 | */ 7 | int main(void) 8 | { 9 | dog_t *my_dog; 10 | 11 | my_dog = new_dog("Poppy", 3.5, "Bob"); 12 | printf("My name is %s, and I am %.1f :) - Woof!\n", my_dog->name, my_dog->age); 13 | return (0); 14 | } 15 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/5-free_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | 4 | /** 5 | * free_dog - frees dogs. 6 | * @d: struct dog. 7 | * 8 | * Return: no return. 9 | */ 10 | void free_dog(dog_t *d) 11 | { 12 | if (d) 13 | { 14 | free(d->name); 15 | free(d->owner); 16 | free(d); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/5-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | /** 4 | * main - check the code 5 | * Return: Always 0. 6 | */ 7 | int main(void) 8 | { 9 | dog_t *my_dog; 10 | 11 | my_dog = new_dog("Poppy", 3.5, "Bob"); 12 | printf("My name is %s, and I am %.1f :) - Woof!\n", my_dog->name, my_dog->age); 13 | free_dog(my_dog); 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/dog.h: -------------------------------------------------------------------------------- 1 | #ifndef _DOG_H_ 2 | #define _DOG_H_ 3 | 4 | /** 5 | * dog_t - Typedef for struct dog 6 | */ 7 | typedef struct dog dog_t; 8 | 9 | /** 10 | * struct dog - struct that stores some information of a dog 11 | * @name: name of the dog 12 | * @age: age of the dog 13 | * @owner: owner of the dog 14 | * 15 | * Description: struct called "dog" that stores its name, its age 16 | * and the name of its owner. 17 | */ 18 | struct dog 19 | { 20 | char *name; 21 | float age; 22 | char *owner; 23 | }; 24 | 25 | void init_dog(struct dog *d, char *name, float age, char *owner); 26 | void print_dog(struct dog *d); 27 | dog_t *new_dog(char *name, float age, char *owner); 28 | void free_dog(dog_t *d); 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /0x0F-function_pointers/0-print_name.c: -------------------------------------------------------------------------------- 1 | #include "function_pointers.h" 2 | 3 | /** 4 | * print_name - prints a name. 5 | * @name: input name. 6 | * @f: function pointer. 7 | * 8 | * Return: no return. 9 | */ 10 | void print_name(char *name, void (*f)(char *)) 11 | { 12 | if (name && f) 13 | f(name); 14 | } /**Prince Solomon*/ 15 | -------------------------------------------------------------------------------- /0x0F-function_pointers/1-array_iterator.c: -------------------------------------------------------------------------------- 1 | #include "function_pointers.h" 2 | 3 | /** 4 | * array_iterator - executes a function given as a 5 | * parameter on each element of an array. 6 | * @array: input integer array. 7 | * @size: size of the array. 8 | * @action: pointer to the function. 9 | * 10 | * Return: no return. 11 | */ 12 | void array_iterator(int *array, size_t size, void (*action)(int)) 13 | { 14 | unsigned int i; 15 | 16 | if (array && action) 17 | for (i = 0; i < size; i++) 18 | action(array[i]); 19 | } 20 | -------------------------------------------------------------------------------- /0x0F-function_pointers/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "function_pointers.h" 3 | /** 4 | * print_elem - prints an integer 5 | * @elem: the integer to print 6 | * Return: Nothing. 7 | */ 8 | void print_elem(int elem) 9 | { 10 | printf("%d\n", elem); 11 | } 12 | 13 | /** 14 | * print_elem_hex - prints an integer, in hexadecimal 15 | * @elem: the integer to print 16 | * Return: Nothing. 17 | */ 18 | void print_elem_hex(int elem) 19 | { 20 | printf("0x%x\n", elem); 21 | } 22 | 23 | /** 24 | * main - check the code 25 | * Return: Always 0. 26 | */ 27 | int main(void) 28 | { 29 | int array[5] = {0, 98, 402, 1024, 4096}; 30 | 31 | array_iterator(array, 5, &print_elem); 32 | array_iterator(array, 5, &print_elem_hex); 33 | return (0); 34 | } 35 | -------------------------------------------------------------------------------- /0x0F-function_pointers/100-main_opcodes.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * main - check the code for ALX Afrcia SE students. 6 | * @argc: argument count. 7 | * @argv: argument vector. 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(int argc, char *argv[]) 12 | { 13 | char *opc = (char *) main; 14 | int i, nbytes; 15 | 16 | if (argc != 2) 17 | { 18 | printf("Error\n"); 19 | exit(1); 20 | } 21 | 22 | nbytes = atoi(argv[1]); 23 | 24 | if (nbytes < 0) 25 | { 26 | printf("Error\n"); 27 | exit(2); 28 | } 29 | 30 | for (i = 0; i < nbytes; i++) 31 | { 32 | printf("%02x", opc[i] & 0xFF); 33 | if (i != nbytes - 1) 34 | printf(" "); 35 | } 36 | 37 | printf("\n"); 38 | return (0); 39 | } /**Follow me Pls*/ 40 | -------------------------------------------------------------------------------- /0x0F-function_pointers/2-int_index.c: -------------------------------------------------------------------------------- 1 | #include "function_pointers.h" 2 | 3 | /** 4 | * int_index - searches for an integer 5 | * @array: input integer array. 6 | * @size: size of the array. 7 | * @cmp: pointer to the function to be used 8 | * to compare values. 9 | * 10 | * Return: index of the first eement for which the cmp 11 | * function does not return 0. If no elements matches, 12 | * return -1. If size <= 0, return -1. 13 | */ 14 | int int_index(int *array, int size, int (*cmp)(int)) 15 | { 16 | int i; 17 | 18 | if (array && cmp) 19 | { 20 | if (size <= 0) 21 | return (-1); 22 | 23 | for (i = 0; i < size; i++) 24 | if (cmp(array[i])) 25 | return (i); 26 | } 27 | 28 | return (-1); 29 | } 30 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-calc.h: -------------------------------------------------------------------------------- 1 | #ifndef _CALC_H_ 2 | #define _CALC_H_ 3 | 4 | #include 5 | #include 6 | /** 7 | * struct op - Struct op 8 | * 9 | * @op: The operator 10 | * @f: The function associated 11 | */ 12 | typedef struct op 13 | { 14 | char *op; 15 | int (*f)(int a, int b); 16 | } op_t; 17 | 18 | int op_add(int a, int b); 19 | int op_sub(int a, int b); 20 | int op_mul(int a, int b); 21 | int op_div(int a, int b); 22 | int op_mod(int a, int b); 23 | int (*get_op_func(char *s))(int, int); 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-get_op_func.c: -------------------------------------------------------------------------------- 1 | #include "3-calc.h" 2 | 3 | /** 4 | * get_op_func - selects the correct function to perform 5 | * the operation asked by the user. 6 | * @s: char operator. 7 | * 8 | * Return: pointer to the function that corresponds to the operator. 9 | */ 10 | int (*get_op_func(char *s))(int, int) 11 | { 12 | op_t ops[] = { 13 | {"+", op_add}, 14 | {"-", op_sub}, 15 | {"*", op_mul}, 16 | {"/", op_div}, 17 | {"%", op_mod}, 18 | {NULL, NULL} 19 | }; 20 | int i = 0; 21 | 22 | while (i < 10) 23 | { 24 | if (s[0] == ops->op[i]) 25 | break; 26 | i++; 27 | } 28 | 29 | return (ops[i / 2].f); 30 | } 31 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-main.c: -------------------------------------------------------------------------------- 1 | #include "3-calc.h" 2 | 3 | /** 4 | * main - check the code for Holberton School students. 5 | * @argc: argument count. 6 | * @argv: argument vector. 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(int argc, char *argv[]) 11 | { 12 | int a, b; 13 | int (*operation)(int, int); 14 | 15 | if (argc != 4) 16 | { 17 | printf("Error\n"); 18 | exit(98); 19 | } 20 | 21 | if (argv[2][1]) 22 | { 23 | printf("Error\n"); 24 | exit(99); 25 | } 26 | 27 | operation = get_op_func(argv[2]); 28 | 29 | if (operation == NULL) 30 | { 31 | printf("Error\n"); 32 | exit(99); 33 | } 34 | 35 | a = atoi(argv[1]); 36 | b = atoi(argv[3]); 37 | 38 | printf("%d\n", operation(a, b)); 39 | return (0); 40 | } 41 | -------------------------------------------------------------------------------- /0x0F-function_pointers/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * _putchar - writes the character c to stdout 4 | * @c: The character to print 5 | * 6 | * Return: On success 1. 7 | * On error, -1 is returned, and errno is set appropriately. 8 | */ 9 | int _putchar(char c) 10 | { 11 | return (write(1, &c, 1)); 12 | } 13 | -------------------------------------------------------------------------------- /0x0F-function_pointers/function_pointers.h: -------------------------------------------------------------------------------- 1 | #ifndef _FUNCTION_POINTERS_ 2 | #define _FUNCTION_POINTERS_ 3 | 4 | #include 5 | 6 | void print_name(char *name, void (*f)(char *)); 7 | void array_iterator(int *array, size_t size, void (*action)(int)); 8 | int int_index(int *array, int size, int (*cmp)(int)); 9 | 10 | #endif/**princexz*/ 11 | -------------------------------------------------------------------------------- /0x10-variadic_functions/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "variadic_functions.h" 3 | /** 4 | * main - check the code 5 | * Return: Always 0. 6 | */ 7 | int main(void) 8 | { 9 | int sum; 10 | 11 | sum = sum_them_all(2, 98, 1024); 12 | printf("%d\n", sum); 13 | sum = sum_them_all(4, 98, 1024, 402, -1024); 14 | printf("%d\n", sum); 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /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: amount of the arguments. 6 | * 7 | * Return: sum of its parameters. 8 | */ 9 | int sum_them_all(const unsigned int n, ...) 10 | { 11 | va_list valist; 12 | unsigned int i; 13 | int sum = 0; 14 | 15 | if (n == 0) 16 | return (0); 17 | 18 | va_start(valist, n); 19 | 20 | for (i = 0; i < n; i++) 21 | sum += va_arg(valist, int); 22 | 23 | va_end(valist); 24 | 25 | return (sum); 26 | } /**Prince_solomon@yahoo.com*/ 27 | -------------------------------------------------------------------------------- /0x10-variadic_functions/1-main.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | /** 3 | * main - check the code 4 | * Return: Always 0. 5 | */ 6 | int main(void) 7 | { 8 | print_numbers(", ", 4, 0, 98, -1024, 402); 9 | return (0); 10 | } 11 | -------------------------------------------------------------------------------- /0x10-variadic_functions/1-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | 3 | /** 4 | * print_numbers - prints numbers. 5 | * @separator: string to be printed between numbers. 6 | * @n: number of integers passed to the function. 7 | * 8 | * Return: no return. 9 | */ 10 | void print_numbers(const char *separator, const unsigned int n, ...) 11 | { 12 | va_list valist; 13 | unsigned int i; 14 | 15 | va_start(valist, n); 16 | 17 | for (i = 0; i < n; i++) 18 | { 19 | printf("%d", va_arg(valist, int)); 20 | if (separator && i < n - 1) 21 | printf("%s", separator); 22 | } 23 | 24 | printf("\n"); 25 | va_end(valist); 26 | } 27 | -------------------------------------------------------------------------------- /0x10-variadic_functions/2-main.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | /** 3 | * main - check the code 4 | * Return: Always 0. 5 | */ 6 | int main(void) 7 | { 8 | print_strings(", ", 2, "Jay", "Django"); 9 | return (0); 10 | } 11 | -------------------------------------------------------------------------------- /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 | void print_strings(const char *separator, const unsigned int n, ...) 11 | { 12 | va_list valist; 13 | unsigned int i; 14 | char *str; 15 | 16 | va_start(valist, n); 17 | 18 | for (i = 0; i < n; i++) 19 | { 20 | str = va_arg(valist, char *); 21 | 22 | if (str) 23 | printf("%s", str); 24 | else 25 | printf("(nil)"); 26 | 27 | if (i < n - 1) 28 | if (separator) 29 | printf("%s", separator); 30 | } 31 | 32 | printf("\n"); 33 | va_end(valist); 34 | } /**princexz*/ 35 | -------------------------------------------------------------------------------- /0x10-variadic_functions/3-main.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | /** 3 | * main - check the code 4 | * Return: Always 0. 5 | */ 6 | int main(void) 7 | { 8 | print_all("ceis", 'B', 3, "stSchool"); 9 | return (0); 10 | } /**princexz*/ 11 | -------------------------------------------------------------------------------- /0x10-variadic_functions/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * _putchar - writes the character c to stdout 4 | * @c: The character to print 5 | * Return: On success 1. 6 | * On error, -1 is returned, and errno is set appropriately. 7 | */ 8 | int _putchar(char c) 9 | { 10 | return (write(1, &c, 1)); 11 | } 12 | -------------------------------------------------------------------------------- /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-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | /** 6 | * main - check the code 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | list_t *head; 12 | list_t *new; 13 | list_t hello = {"World", 5, NULL}; 14 | size_t n; 15 | 16 | head = &hello; 17 | new = malloc(sizeof(list_t)); 18 | if (new == NULL) 19 | { 20 | printf("Error\n"); 21 | return (1); 22 | } 23 | 24 | new->str = strdup("Hello"); 25 | new->len = 5; 26 | new->next = head; 27 | head = new; 28 | n = print_list(head); 29 | printf("-> %lu elements\n", n); 30 | 31 | printf("\n"); 32 | free(new->str); 33 | new->str = NULL; 34 | n = print_list(head); 35 | printf("-> %lu elements\n", n); 36 | 37 | free(new); 38 | return (0); 39 | } 40 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/0-print_list.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * print_list - prints all the elements of a list_t list. 4 | * @h: singly linked list. 5 | * Return: number of elements in the list. 6 | */ 7 | 8 | size_t print_list(const list_t *h) 9 | { 10 | size_t nelem; 11 | 12 | nelem = 0; 13 | while (h != NULL) 14 | { 15 | if (h->str == NULL) 16 | printf("[%d] %s\n", 0, "(nil)"); 17 | else 18 | printf("[%d] %s\n", h->len, h->str); 19 | h = h->next; 20 | nelem++; 21 | } 22 | return (nelem); 23 | } 24 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/1-list_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * list_len - returns then number of elements in a list. 4 | * @h: singly linked list. 5 | * Return: number of elements in the list. 6 | */ 7 | 8 | size_t list_len(const list_t *h) 9 | { 10 | size_t nelem; 11 | 12 | nelem = 0; 13 | while (h != NULL) 14 | { 15 | h = h->next; 16 | nelem++; 17 | } 18 | return (nelem); 19 | } 20 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code 8 | * Return: Always 0. 9 | */ 10 | int main(void) 11 | { 12 | list_t *head; 13 | list_t *new; 14 | list_t hello = {"World", 5, NULL}; 15 | size_t n; 16 | 17 | head = &hello; 18 | new = malloc(sizeof(list_t)); 19 | if (new == NULL) 20 | { 21 | printf("Error\n"); 22 | return (1); 23 | } 24 | new->str = strdup("Hello"); 25 | new->len = 5; 26 | new->next = head; 27 | head = new; 28 | n = list_len(head); 29 | printf("-> %lu elements\n", n); 30 | free(new->str); 31 | free(new); 32 | return (0); 33 | } 34 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/100-first.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * bmain - function executed before main 5 | * Return: no return. 6 | */ 7 | 8 | void __attribute__ ((constructor)) bmain() 9 | { 10 | printf("You're beat! and yet, you must allow"); 11 | printf(",\nI bore my house upon my back!\n"); 12 | } 13 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/101-hello_holberton.asm: -------------------------------------------------------------------------------- 1 | SECTION .data 2 | msg: db "Hello, Holberton", 0 3 | fmt: db "%s", 10, 0 4 | 5 | SECTION .text 6 | extern printf 7 | global main 8 | main: 9 | mov esi, msg 10 | mov edi, fmt 11 | mov eax, 0 12 | call printf 13 | 14 | mov eax, 0 15 | ret 16 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/101-hello_holberton.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x12-singly_linked_lists/101-hello_holberton.o -------------------------------------------------------------------------------- /0x12-singly_linked_lists/2-add_node.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * add_node - adds a new node at the beginning 4 | * of a list_t list. 5 | * @head: head of the linked list. 6 | * @str: string to store in the list. 7 | * Return: address of the head. 8 | */ 9 | 10 | list_t *add_node(list_t **head, const char *str) 11 | { 12 | list_t *new; 13 | size_t nchar; 14 | 15 | new = malloc(sizeof(list_t)); 16 | if (new == NULL) 17 | return (NULL); 18 | 19 | new->str = strdup(str); 20 | 21 | for (nchar = 0; str[nchar]; nchar++) 22 | ; 23 | 24 | new->len = nchar; 25 | new->next = *head; 26 | *head = new; 27 | 28 | return (*head); 29 | } 30 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/3-add_node_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_node_end - adds a new node at the end 5 | * of a list_t list. 6 | * @head: head of the linked list. 7 | * @str: string to store in the list. 8 | * Return: address of the head. 9 | */ 10 | 11 | list_t *add_node_end(list_t **head, const char *str) 12 | { 13 | list_t *new, *temp; 14 | size_t nchar; 15 | 16 | new = malloc(sizeof(list_t)); 17 | if (new == NULL) 18 | return (NULL); 19 | 20 | new->str = strdup(str); 21 | 22 | for (nchar = 0; str[nchar]; nchar++) 23 | ; 24 | 25 | new->len = nchar; 26 | new->next = NULL; 27 | temp = *head; 28 | 29 | if (temp == NULL) 30 | { 31 | *head = new; 32 | } 33 | else 34 | { 35 | while (temp->next != NULL) 36 | temp = temp->next; 37 | temp->next = new; 38 | } 39 | 40 | return (*head); 41 | } 42 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/4-free_list.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_list - frees a list 5 | * @head: head of the linked list. 6 | * Return: no return. 7 | */ 8 | 9 | void free_list(list_t *head) 10 | { 11 | list_t *current; 12 | 13 | while ((current = head) != NULL) 14 | { 15 | head = head->next; 16 | free(current->str); 17 | free(current); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/4-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | /** 6 | * main - check the code 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | list_t *head; 12 | 13 | head = NULL; 14 | add_node_end(&head, "Bob"); 15 | add_node_end(&head, "&"); 16 | add_node_end(&head, "Kris"); 17 | add_node_end(&head, "love"); 18 | add_node_end(&head, "asm"); 19 | print_list(head); 20 | free_list(head); 21 | head = NULL; 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * _putchar - writes the character c to stdout 4 | * @c: The character to print 5 | * Return: On success 1. 6 | * On error, -1 is returned, and errno is set appropriately. 7 | */ 8 | int _putchar(char c) 9 | { 10 | return (write(1, &c, 1)); 11 | } /** princexz */ 12 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef _LISTS_ 2 | #define _LISTS_ 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | /** 9 | * struct list_s - singly linked list 10 | * @str: string - (malloc'ed string) 11 | * @len: length of the string 12 | * @next: points to the next node 13 | * 14 | * Description: singly linked list node structure 15 | * for ALX SE project 16 | */ 17 | typedef struct list_s 18 | { 19 | char *str; 20 | unsigned int len; 21 | struct list_s *next; 22 | } list_t; 23 | 24 | size_t print_list(const list_t *h); 25 | size_t list_len(const list_t *h); 26 | list_t *add_node(list_t **head, const char *str); 27 | list_t *add_node_end(list_t **head, const char *str); 28 | void free_list(list_t *head); 29 | 30 | #endif/**Prince Solomon*/ 31 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | listint_t *head; 14 | listint_t *new; 15 | listint_t hello = {8, NULL}; 16 | size_t n; 17 | 18 | head = &hello; 19 | new = malloc(sizeof(listint_t)); 20 | if (new == NULL) 21 | { 22 | printf("Error\n"); 23 | return (1); 24 | } 25 | new->n = 9; 26 | new->next = head; 27 | head = new; 28 | n = print_listint(head); 29 | printf("-> %lu elements\n", n); 30 | free(new); 31 | return (0); 32 | } 33 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/0-print_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * print_listint - the program prints all the elements of a list. 5 | * @h: head of a list. 6 | * 7 | * Return: numbers of nodes. 8 | */ 9 | size_t print_listint(const listint_t *h) 10 | { 11 | size_t nnodes = 0; 12 | 13 | while (h != NULL) 14 | { 15 | printf("%d\n", h->n); 16 | h = h->next; 17 | nnodes++; 18 | } 19 | return (nnodes); 20 | } 21 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/1-listint_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * listint_len - program returns the number of elements in 5 | * a linked list. 6 | * @h: head of a list. 7 | * 8 | * Return: numbers of nodes. 9 | */ 10 | size_t listint_len(const listint_t *h) 11 | { 12 | size_t nnodes = 0; 13 | 14 | while (h != NULL) 15 | { 16 | h = h->next; 17 | nnodes++; 18 | } 19 | return (nnodes); 20 | } 21 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | listint_t *head; 14 | listint_t *new; 15 | listint_t hello = {8, NULL}; 16 | size_t n; 17 | 18 | head = &hello; 19 | new = malloc(sizeof(listint_t)); 20 | if (new == NULL) 21 | { 22 | printf("Error\n"); 23 | return (1); 24 | } 25 | new->n = 9; 26 | new->next = head; 27 | head = new; 28 | n = listint_len(head); 29 | printf("-> %lu elements\n", n); 30 | free(new); 31 | return (0); 32 | } 33 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/100-reverse_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * reverse_listint - reverses a linked list. 5 | * @head: head of a list. 6 | * 7 | * Return: pointer to the first node. 8 | */ 9 | listint_t *reverse_listint(listint_t **head) 10 | { 11 | listint_t *p; 12 | listint_t *n; 13 | 14 | p = NULL; 15 | n = NULL; 16 | 17 | while (*head != NULL) 18 | { 19 | n = (*head)->next; 20 | (*head)->next = p; 21 | p = *head; 22 | *head = n; 23 | } 24 | 25 | *head = p; 26 | return (*head); 27 | } 28 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/2-add_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_nodeint - program add a new node at the beginning 5 | * of a linked list 6 | * @head: head of a list. 7 | * @n: n element. 8 | * 9 | * Return: address of the new element. NUll if it failed. 10 | */ 11 | listint_t *add_nodeint(listint_t **head, const int n) 12 | { 13 | listint_t *new; 14 | 15 | new = malloc(sizeof(listint_t)); 16 | 17 | if (new == NULL) 18 | return (NULL); 19 | 20 | new->n = n; 21 | new->next = *head; 22 | *head = new; 23 | 24 | return (*head); 25 | } 26 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | listint_t *head; 14 | 15 | head = NULL; 16 | add_nodeint(&head, 0); 17 | add_nodeint(&head, 1); 18 | add_nodeint(&head, 2); 19 | add_nodeint(&head, 3); 20 | add_nodeint(&head, 4); 21 | add_nodeint(&head, 98); 22 | add_nodeint(&head, 402); 23 | add_nodeint(&head, 1024); 24 | print_listint(head); 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/3-add_nodeint_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_nodeint_end - program adds a new node at the end 5 | * of a linked list 6 | * @head: head of a list. 7 | * @n: n element. 8 | * 9 | * Return: address of the new element. NUll if it failed. 10 | */ 11 | listint_t *add_nodeint_end(listint_t **head, const int n) 12 | { 13 | listint_t *new; 14 | listint_t *temp; 15 | 16 | (void)temp; 17 | 18 | new = malloc(sizeof(listint_t)); 19 | 20 | if (new == NULL) 21 | return (NULL); 22 | 23 | new->n = n; 24 | new->next = NULL; 25 | temp = *head; 26 | if (*head == NULL) 27 | { 28 | *head = new; 29 | } 30 | else 31 | { 32 | while (temp->next != NULL) 33 | { 34 | temp = temp->next; 35 | } 36 | temp->next = new; 37 | } 38 | 39 | return (*head); 40 | } 41 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/3-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | listint_t *head; 14 | 15 | head = NULL; 16 | add_nodeint_end(&head, 0); 17 | add_nodeint_end(&head, 1); 18 | add_nodeint_end(&head, 2); 19 | add_nodeint_end(&head, 3); 20 | add_nodeint_end(&head, 4); 21 | add_nodeint_end(&head, 98); 22 | add_nodeint_end(&head, 402); 23 | add_nodeint_end(&head, 1024); 24 | print_listint(head); 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/4-free_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_listint - program frees a linked list 5 | * @head: head of a list. 6 | * 7 | * Return: no return. 8 | */ 9 | void free_listint(listint_t *head) 10 | { 11 | listint_t *temp; 12 | 13 | while ((temp = head) != NULL) 14 | { 15 | head = head->next; 16 | free(temp); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/4-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | listint_t *head; 14 | 15 | head = NULL; 16 | add_nodeint_end(&head, 0); 17 | add_nodeint_end(&head, 1); 18 | add_nodeint_end(&head, 2); 19 | add_nodeint_end(&head, 3); 20 | add_nodeint_end(&head, 4); 21 | add_nodeint_end(&head, 98); 22 | add_nodeint_end(&head, 402); 23 | add_nodeint_end(&head, 1024); 24 | print_listint(head); 25 | free_listint(head); 26 | head = NULL; 27 | return (0); 28 | } 29 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/5-free_listint2.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_listint2 - program thatfrees a linked list 5 | * @head: head of a list. 6 | * 7 | * Return: no return. 8 | */ 9 | void free_listint2(listint_t **head) 10 | { 11 | listint_t *temp; 12 | listint_t *curr; 13 | 14 | if (head != NULL) 15 | { 16 | curr = *head; 17 | while ((temp = curr) != NULL) 18 | { 19 | curr = curr->next; 20 | free(temp); 21 | } 22 | *head = NULL; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/5-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | listint_t *head; 14 | 15 | head = NULL; 16 | add_nodeint_end(&head, 0); 17 | add_nodeint_end(&head, 1); 18 | add_nodeint_end(&head, 2); 19 | add_nodeint_end(&head, 3); 20 | add_nodeint_end(&head, 4); 21 | add_nodeint_end(&head, 98); 22 | add_nodeint_end(&head, 402); 23 | add_nodeint_end(&head, 1024); 24 | print_listint(head); 25 | free_listint2(&head); 26 | printf("%p\n", (void *)head); 27 | return (0); 28 | } 29 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/6-pop_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * pop_listint - program deletes the head node of 5 | * a linked list 6 | * @head: head of a list. 7 | * 8 | * Return: head node's data. 9 | */ 10 | int pop_listint(listint_t **head) 11 | { 12 | int hnode; 13 | listint_t *h; 14 | listint_t *curr; 15 | 16 | if (*head == NULL) 17 | return (0); 18 | 19 | curr = *head; 20 | 21 | hnode = curr->n; 22 | 23 | h = curr->next; 24 | 25 | free(curr); 26 | 27 | *head = h; 28 | 29 | return (hnode); 30 | } 31 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/7-get_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * get_nodeint_at_index - returns the nth node of a linked list 5 | * @head: head of a list. 6 | * @index: index of the node. 7 | * 8 | * Return: nth node. If node does not exist, returns NULL. 9 | */ 10 | listint_t *get_nodeint_at_index(listint_t *head, unsigned int index) 11 | { 12 | unsigned int i; 13 | 14 | for (i = 0; i < index && head != NULL; i++) 15 | { 16 | head = head->next; 17 | } 18 | 19 | return (head); 20 | } 21 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/7-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | listint_t *head; 14 | listint_t *node; 15 | 16 | head = NULL; 17 | add_nodeint_end(&head, 0); 18 | add_nodeint_end(&head, 1); 19 | add_nodeint_end(&head, 2); 20 | add_nodeint_end(&head, 3); 21 | add_nodeint_end(&head, 4); 22 | add_nodeint_end(&head, 98); 23 | add_nodeint_end(&head, 402); 24 | add_nodeint_end(&head, 1024); 25 | print_listint(head); 26 | node = get_nodeint_at_index(head, 5); 27 | printf("%d\n", node->n); 28 | print_listint(head); 29 | free_listint2(&head); 30 | return (0); 31 | } 32 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/8-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | listint_t *head; 14 | int sum; 15 | 16 | head = NULL; 17 | add_nodeint_end(&head, 0); 18 | add_nodeint_end(&head, 1); 19 | add_nodeint_end(&head, 2); 20 | add_nodeint_end(&head, 3); 21 | add_nodeint_end(&head, 4); 22 | add_nodeint_end(&head, 98); 23 | add_nodeint_end(&head, 402); 24 | add_nodeint_end(&head, 1024); 25 | sum = sum_listint(head); 26 | printf("sum = %d\n", sum); 27 | free_listint2(&head); 28 | return (0); 29 | } 30 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/8-sum_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * sum_listint - program returns the sum of all the data (n) of 5 | * a linked list. 6 | * @head: head of a list. 7 | * 8 | * Return: sum of all the data (n). 9 | */ 10 | int sum_listint(listint_t *head) 11 | { 12 | int sum; 13 | 14 | sum = 0; 15 | while (head != NULL) 16 | { 17 | sum += head->n; 18 | head = head->next; 19 | } 20 | 21 | return (sum); 22 | } 23 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/9-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | listint_t *head; 14 | 15 | head = NULL; 16 | add_nodeint_end(&head, 0); 17 | add_nodeint_end(&head, 1); 18 | add_nodeint_end(&head, 2); 19 | add_nodeint_end(&head, 3); 20 | add_nodeint_end(&head, 4); 21 | add_nodeint_end(&head, 98); 22 | add_nodeint_end(&head, 402); 23 | add_nodeint_end(&head, 1024); 24 | print_listint(head); 25 | printf("-----------------\n"); 26 | insert_nodeint_at_index(&head, 5, 4096); 27 | print_listint(head); 28 | free_listint2(&head); 29 | return (0); 30 | } 31 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/_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 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/0-binary_to_uint.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * binary_to_uint - program converts a binary number to an 5 | * unsigned int. 6 | * @b: binary. 7 | * 8 | * Return: unsigned int. 9 | */ 10 | unsigned int binary_to_uint(const char *b) 11 | { 12 | unsigned int ui; 13 | int len, base_two; 14 | 15 | if (!b) 16 | return (0); 17 | 18 | ui = 0; 19 | 20 | for (len = 0; b[len] != '\0'; len++) 21 | ; 22 | 23 | for (len--, base_two = 1; len >= 0; len--, base_two *= 2) 24 | { 25 | if (b[len] != '0' && b[len] != '1') 26 | { 27 | return (0); 28 | } 29 | 30 | if (b[len] & 1) 31 | { 32 | ui += base_two; 33 | } 34 | } 35 | 36 | return (ui); 37 | } /**princexz*/ 38 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | unsigned int n; 12 | 13 | n = binary_to_uint("1"); 14 | printf("%u\n", n); 15 | n = binary_to_uint("101"); 16 | printf("%u\n", n); 17 | n = binary_to_uint("1e01"); 18 | printf("%u\n", n); 19 | n = binary_to_uint("1100010"); 20 | printf("%u\n", n); 21 | n = binary_to_uint("0000000000000000000110010010"); 22 | printf("%u\n", n); 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | print_binary(0); 12 | printf("\n"); 13 | print_binary(1); 14 | printf("\n"); 15 | print_binary(98); 16 | printf("\n"); 17 | print_binary(1024); 18 | printf("\n"); 19 | print_binary((1 << 10) + 1); 20 | printf("\n"); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/1-print_binary.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_binary - program prints the binary representation 5 | * of a number. 6 | * @n: unsigned long int. 7 | * 8 | * Return: no return. 9 | */ 10 | void print_binary(unsigned long int n) 11 | { 12 | if (n >> 0) 13 | { 14 | if (n >> 1) 15 | print_binary(n >> 1); 16 | _putchar((n & 1) + '0'); 17 | } 18 | else 19 | { 20 | _putchar('0'); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/100-get_endianness.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * get_endianness - function that checks the endianness 5 | * 6 | * Return: 0 if big endian, 1 if little endian 7 | */ 8 | int get_endianness(void) 9 | { 10 | unsigned int x; 11 | char *c; 12 | 13 | x = 1; 14 | c = (char *) &x; 15 | 16 | return ((int)*c); 17 | } /**princexz*/ 18 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/100-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | int main(void) 5 | { 6 | int n; 7 | 8 | n = get_endianness(); 9 | if (n != 0) 10 | { 11 | printf("Little Endian\n"); 12 | } 13 | else 14 | { 15 | printf("Big Endian\n"); 16 | } 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/101-password: -------------------------------------------------------------------------------- 1 | Hol 2 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/2-get_bit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * get_bit - returns the value of a bit at a given 5 | * index. 6 | * @n: unsigned long int input. 7 | * @index: index of the bit. 8 | * 9 | * Return: value of the bit. 10 | */ 11 | int get_bit(unsigned long int n, unsigned int index) 12 | { 13 | unsigned int i; 14 | 15 | if (n == 0 && index < 64) 16 | return (0); 17 | 18 | for (i = 0; i <= 63; n >>= 1, i++) 19 | { 20 | if (index == i) 21 | { 22 | return (n & 1); 23 | } 24 | } 25 | 26 | return (-1); 27 | } 28 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int n; 12 | 13 | n = get_bit(1024, 10); 14 | printf("%d\n", n); 15 | n = get_bit(98, 1); 16 | printf("%d\n", n); 17 | n = get_bit(1024, 0); 18 | printf("%d\n", n); 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/3-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | unsigned long int n; 12 | 13 | n = 1024; 14 | set_bit(&n, 5); 15 | printf("%lu\n", n); 16 | n = 0; 17 | set_bit(&n, 10); 18 | printf("%lu\n", n); 19 | n = 98; 20 | set_bit(&n, 0); 21 | printf("%lu\n", n); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/3-set_bit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * set_bit - program sets the value of a bit to 1. 5 | * at a given index. 6 | * @n: pointer of an unsigned long int. 7 | * @index: index of the bit. 8 | * 9 | * Return: 1 if it worked, -1 if it didn't. 10 | */ 11 | int set_bit(unsigned long int *n, unsigned int index) 12 | { 13 | unsigned int m; 14 | 15 | if (index > 63) 16 | return (-1); 17 | 18 | m = 1 << index; 19 | *n = (*n | m); 20 | 21 | return (1); 22 | } /*princexz*/ 23 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/4-clear_bit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * clear_bit - program sets the value of a bit to 0. 5 | * at a given index. 6 | * @n: pointer of an unsigned long int. 7 | * @index: index of the bit. 8 | * 9 | * Return: 1 if it worked, -1 if it didn't. 10 | */ 11 | int clear_bit(unsigned long int *n, unsigned int index) 12 | { 13 | unsigned int m; 14 | 15 | if (index > 63) 16 | return (-1); 17 | 18 | m = 1 << index; 19 | 20 | if (*n & m) 21 | *n ^= m; 22 | 23 | return (1); 24 | } /*Prince Solomon (princexz)*/ 25 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/4-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | unsigned long int n; 12 | 13 | n = 1024; 14 | clear_bit(&n, 10); 15 | printf("%lu\n", n); 16 | n = 0; 17 | clear_bit(&n, 10); 18 | printf("%lu\n", n); 19 | n = 98; 20 | clear_bit(&n, 1); 21 | printf("%lu\n", n); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/5-flip_bits.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * flip_bits - program returns the number of bits you would 5 | * need to flip to get from one number to another 6 | * @n: number one. 7 | * @m: number two. 8 | * 9 | * Return: number of bits. 10 | */ 11 | unsigned int flip_bits(unsigned long int n, unsigned long int m) 12 | { 13 | unsigned int nbits; 14 | 15 | for (nbits = 0; n || m; n >>= 1, m >>= 1) 16 | { 17 | if ((n & 1) != (m & 1)) 18 | nbits++; 19 | } 20 | 21 | return (nbits); 22 | } /*I'm princexz*/ 23 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/5-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | unsigned int n; 12 | 13 | n = flip_bits(1024, 1); 14 | printf("%u\n", n); 15 | n = flip_bits(402, 98); 16 | printf("%u\n", n); 17 | n = flip_bits(1024, 3); 18 | printf("%u\n", n); 19 | n = flip_bits(1024, 1025); 20 | printf("%u\n", n); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /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 | int _putchar(char c) 11 | { 12 | return (write(1, &c, 1)); 13 | } 14 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/main.h: -------------------------------------------------------------------------------- 1 | #ifndef _MAIN_H 2 | #define _MAIN_H 3 | 4 | unsigned int binary_to_uint(const char *b); 5 | void print_binary(unsigned long int n); 6 | int _putchar(char c); 7 | int get_bit(unsigned long int n, unsigned int index); 8 | int set_bit(unsigned long int *n, unsigned int index); 9 | int clear_bit(unsigned long int *n, unsigned int index); 10 | unsigned int flip_bits(unsigned long int n, unsigned long int m); 11 | int get_endianness(void); 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /0x15-file_io/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "main.h" 4 | 5 | /** 6 | * main - check the code 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(int ac, char **av) 11 | { 12 | ssize_t n; 13 | 14 | if (ac != 2) 15 | { 16 | dprintf(2, "Usage: %s filename\n", av[0]); 17 | exit(1); 18 | } 19 | n = read_textfile(av[1], 114); 20 | printf("\n(printed chars: %li)\n", n); 21 | n = read_textfile(av[1], 1024); 22 | printf("\n(printed chars: %li)\n", n); 23 | return (0); 24 | } /*princexz*/ 25 | -------------------------------------------------------------------------------- /0x15-file_io/0-read_textfile.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * read_textfile - program reads a text file and prints the letters 5 | * @filename: filename. 6 | * @letters: numbers of letters printed. 7 | * 8 | * Return: numbers of letters printed. It fails, returns 0. 9 | */ 10 | ssize_t read_textfile(const char *filename, size_t letters) 11 | { 12 | int fd; 13 | ssize_t nrd, nwr; 14 | char *buf; 15 | 16 | if (!filename) 17 | return (0); 18 | 19 | fd = open(filename, O_RDONLY); 20 | 21 | if (fd == -1) 22 | return (0); 23 | 24 | buf = malloc(sizeof(char) * (letters)); 25 | if (!buf) 26 | return (0); 27 | 28 | nrd = read(fd, buf, letters); 29 | nwr = write(STDOUT_FILENO, buf, nrd); 30 | 31 | close(fd); 32 | 33 | free(buf); 34 | 35 | return (nwr); 36 | } /*princexz*/ 37 | -------------------------------------------------------------------------------- /0x15-file_io/1-create_file.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * create_file - program creates a file 5 | * @filename: filename. 6 | * @text_content: content writed in the file. 7 | * 8 | * Return: 1 if it success. -1 if it fails. 9 | */ 10 | int create_file(const char *filename, char *text_content) 11 | { 12 | int fd; 13 | int nletters; 14 | int rwr; 15 | 16 | if (!filename) 17 | return (-1); 18 | 19 | fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0600); 20 | 21 | if (fd == -1) 22 | return (-1); 23 | 24 | if (!text_content) 25 | text_content = ""; 26 | 27 | for (nletters = 0; text_content[nletters]; nletters++) 28 | ; 29 | 30 | rwr = write(fd, text_content, nletters); 31 | 32 | if (rwr == -1) 33 | return (-1); 34 | 35 | close(fd); 36 | 37 | return (1); 38 | } 39 | -------------------------------------------------------------------------------- /0x15-file_io/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "main.h" 4 | 5 | /** 6 | * main - check the code 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(int ac, char **av) 11 | { 12 | int res; 13 | 14 | if (ac != 3) 15 | { 16 | dprintf(2, "Usage: %s filename text\n", av[0]); 17 | exit(1); 18 | } 19 | res = create_file(av[1], av[2]); 20 | printf("-> %i)\n", res); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x15-file_io/2-append_text_to_file.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * append_text_to_file - program appends text at the end of a file 5 | * @filename: filename. 6 | * @text_content: added content. 7 | * 8 | * Return: 1 if the file exists. -1 if the fails does not exist 9 | * or if it fails. 10 | */ 11 | int append_text_to_file(const char *filename, char *text_content) 12 | { 13 | int fd; 14 | int nletters; 15 | int rwr; 16 | 17 | if (!filename) 18 | return (-1); 19 | 20 | fd = open(filename, O_WRONLY | O_APPEND); 21 | 22 | if (fd == -1) 23 | return (-1); 24 | 25 | if (text_content) 26 | { 27 | for (nletters = 0; text_content[nletters]; nletters++) 28 | ; 29 | 30 | rwr = write(fd, text_content, nletters); 31 | 32 | if (rwr == -1) 33 | return (-1); 34 | } 35 | 36 | close(fd); 37 | 38 | return (1); 39 | } 40 | -------------------------------------------------------------------------------- /0x15-file_io/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "main.h" 4 | 5 | /** 6 | * main - check the code 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(int ac, char **av) 11 | { 12 | int res; 13 | 14 | if (ac != 3) 15 | { 16 | dprintf(2, "Usage: %s filename text\n", av[0]); 17 | exit(1); 18 | } 19 | res = append_text_to_file(av[1], av[2]); 20 | printf("-> %i)\n", res); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x15-file_io/_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 | } /*princexz*/ 14 | -------------------------------------------------------------------------------- /0x15-file_io/main.h: -------------------------------------------------------------------------------- 1 | #ifndef _HOLBERTON_ 2 | #define _HOLBERTON_ 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | ssize_t read_textfile(const char *filename, size_t letters); 11 | int create_file(const char *filename, char *text_content); 12 | int append_text_to_file(const char *filename, char *text_content); 13 | 14 | #endif 15 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/0-print_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * print_dlistint - Prints all the elements of a dlistint_t list. 5 | * @h: The head of the dlistint_t list. 6 | * 7 | * Return: The number of nodes in the list. 8 | */ 9 | size_t print_dlistint(const dlistint_t *h) 10 | { 11 | size_t nodes = 0; 12 | 13 | while (h) 14 | { 15 | nodes++; 16 | printf("%d\n", h->n); 17 | h = h->next; 18 | } 19 | 20 | return (nodes); 21 | } 22 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/1-dlistint_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * dlistint_len - Counts the number of elements in a linked dlistint_t list. 5 | * @h: The head of the dlistint_t list. 6 | * 7 | * Return: The number of elements in the dlistint_t list. 8 | */ 9 | size_t dlistint_len(const dlistint_t *h) 10 | { 11 | size_t nodes = 0; 12 | 13 | while (h) 14 | { 15 | nodes++; 16 | h = h->next; 17 | } 18 | 19 | return (nodes); 20 | } 21 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/100-password: -------------------------------------------------------------------------------- 1 | en C Pyfo neZ 2 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/102-result: -------------------------------------------------------------------------------- 1 | 906609 2 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/2-add_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_dnodeint - Adds a new node at the beginning of a dlistint_t list. 5 | * @head: A pointer to the head of the dlistint_t list. 6 | * @n: The integer for the new node to contain. 7 | * 8 | * Return: If the function fails - NULL. 9 | * Otherwise - the address of the new node. 10 | */ 11 | dlistint_t *add_dnodeint(dlistint_t **head, const int n) 12 | { 13 | dlistint_t *new; 14 | 15 | new = malloc(sizeof(dlistint_t)); 16 | if (new == NULL) 17 | return (NULL); 18 | 19 | new->n = n; 20 | new->prev = NULL; 21 | new->next = *head; 22 | if (*head != NULL) 23 | (*head)->prev = new; 24 | *head = new; 25 | 26 | return (new); 27 | } 28 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/4-free_dlistint.c: -------------------------------------------------------------------------------- 1 | /* 2 | * File: 4-free_dlistint.c 3 | * Auth: Brennan D Baraban 4 | */ 5 | 6 | #include "lists.h" 7 | 8 | /** 9 | * free_dlistint - Frees a linked dlistint_t list. 10 | * @head: The head of the dlistint_t list. 11 | */ 12 | void free_dlistint(dlistint_t *head) 13 | { 14 | dlistint_t *tmp; 15 | 16 | while (head) 17 | { 18 | tmp = head->next; 19 | free(head); 20 | head = tmp; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/5-get_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * get_dnodeint_at_index - Locates a node in a dlistint_t list. 5 | * @head: The head of the dlistint_t list. 6 | * @index: The node to locate. 7 | * 8 | * Return: If the node does not exist - NULL. 9 | * Otherwise - the address of the located node. 10 | */ 11 | dlistint_t *get_dnodeint_at_index(dlistint_t *head, unsigned int index) 12 | { 13 | for (; index != 0; index--) 14 | { 15 | if (head == NULL) 16 | return (NULL); 17 | head = head->next; 18 | } 19 | 20 | return (head); 21 | } 22 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/6-sum_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * sum_dlistint - Sums all the data of a dlistint_t list. 5 | * @head: The head of the dlistint_t list. 6 | * 7 | * Return: The sum of all the data. 8 | */ 9 | int sum_dlistint(dlistint_t *head) 10 | { 11 | int sum = 0; 12 | 13 | while (head) 14 | { 15 | sum += head->n; 16 | head = head->next; 17 | } 18 | 19 | return (sum); 20 | } 21 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/tests/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "../lists.h" 5 | 6 | /** 7 | * main - check the code for Holberton School students. 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | dlistint_t *head; 14 | dlistint_t *new; 15 | dlistint_t hello = {8, NULL, NULL}; 16 | size_t n; 17 | 18 | head = &hello; 19 | new = malloc(sizeof(dlistint_t)); 20 | if (new == NULL) 21 | { 22 | dprintf(2, "Error: Can't malloc\n"); 23 | return (EXIT_FAILURE); 24 | } 25 | new->n = 9; 26 | head->prev = new; 27 | new->next = head; 28 | new->prev = NULL; 29 | head = new; 30 | n = print_dlistint(head); 31 | printf("-> %lu elements\n", n); 32 | free(new); 33 | return (EXIT_SUCCESS); 34 | } /* princexz */ 35 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/tests/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "../lists.h" 5 | 6 | /** 7 | * main - check the code for alx School students. 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | dlistint_t *head; 14 | dlistint_t *new; 15 | dlistint_t hello = {8, NULL, NULL}; 16 | size_t n; 17 | 18 | head = &hello; 19 | new = malloc(sizeof(dlistint_t)); 20 | if (new == NULL) 21 | { 22 | dprintf(2, "Error: Can't malloc\n"); 23 | return (EXIT_FAILURE); 24 | } 25 | new->n = 9; 26 | head->prev = new; 27 | new->next = head; 28 | new->prev = NULL; 29 | head = new; 30 | n = dlistint_len(head); 31 | printf("-> %lu elements\n", n); 32 | free(new); 33 | return (EXIT_SUCCESS); 34 | } 35 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/tests/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "../lists.h" 5 | 6 | /** 7 | * main - check the code for ALX School students. 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | dlistint_t *head; 14 | 15 | head = NULL; 16 | add_dnodeint(&head, 0); 17 | add_dnodeint(&head, 1); 18 | add_dnodeint(&head, 2); 19 | add_dnodeint(&head, 3); 20 | add_dnodeint(&head, 4); 21 | add_dnodeint(&head, 98); 22 | add_dnodeint(&head, 402); 23 | add_dnodeint(&head, 1024); 24 | print_dlistint(head); 25 | return (EXIT_SUCCESS); 26 | } 27 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/tests/3-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "../lists.h" 5 | 6 | /** 7 | * main - check the code for ALX School students. 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | dlistint_t *head; 14 | 15 | head = NULL; 16 | add_dnodeint_end(&head, 0); 17 | add_dnodeint_end(&head, 1); 18 | add_dnodeint_end(&head, 2); 19 | add_dnodeint_end(&head, 3); 20 | add_dnodeint_end(&head, 4); 21 | add_dnodeint_end(&head, 98); 22 | add_dnodeint_end(&head, 402); 23 | add_dnodeint_end(&head, 1024); 24 | print_dlistint(head); 25 | return (EXIT_SUCCESS); 26 | } 27 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/tests/4-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "../lists.h" 5 | 6 | /** 7 | * main - check the code for alx School students. 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | dlistint_t *head; 14 | 15 | head = NULL; 16 | add_dnodeint_end(&head, 0); 17 | add_dnodeint_end(&head, 1); 18 | add_dnodeint_end(&head, 2); 19 | add_dnodeint_end(&head, 3); 20 | add_dnodeint_end(&head, 4); 21 | add_dnodeint_end(&head, 98); 22 | add_dnodeint_end(&head, 402); 23 | add_dnodeint_end(&head, 1024); 24 | print_dlistint(head); 25 | free_dlistint(head); 26 | head = NULL; 27 | return (EXIT_SUCCESS); 28 | } 29 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/tests/5-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "../lists.h" 5 | 6 | /** 7 | * main - check the code for alx School students. 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | dlistint_t *head; 14 | dlistint_t *node; 15 | 16 | head = NULL; 17 | add_dnodeint_end(&head, 0); 18 | add_dnodeint_end(&head, 1); 19 | add_dnodeint_end(&head, 2); 20 | add_dnodeint_end(&head, 3); 21 | add_dnodeint_end(&head, 4); 22 | add_dnodeint_end(&head, 98); 23 | add_dnodeint_end(&head, 402); 24 | add_dnodeint_end(&head, 1024); 25 | print_dlistint(head); 26 | node = get_dnodeint_at_index(head, 5); 27 | printf("%d\n", node->n); 28 | free_dlistint(head); 29 | head = NULL; 30 | return (EXIT_SUCCESS); 31 | } 32 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/tests/6-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "../lists.h" 5 | 6 | /** 7 | * main - check the code for alx School students. 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | dlistint_t *head; 14 | int sum; 15 | 16 | head = NULL; 17 | add_dnodeint_end(&head, 0); 18 | add_dnodeint_end(&head, 1); 19 | add_dnodeint_end(&head, 2); 20 | add_dnodeint_end(&head, 3); 21 | add_dnodeint_end(&head, 4); 22 | add_dnodeint_end(&head, 98); 23 | add_dnodeint_end(&head, 402); 24 | add_dnodeint_end(&head, 1024); 25 | sum = sum_dlistint(head); 26 | printf("sum = %d\n", sum); 27 | free_dlistint(head); 28 | head = NULL; 29 | return (EXIT_SUCCESS); 30 | } 31 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/tests/7-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "../lists.h" 5 | 6 | /** 7 | * main - check the code for Alx School students. 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | dlistint_t *head; 14 | 15 | head = NULL; 16 | add_dnodeint_end(&head, 0); 17 | add_dnodeint_end(&head, 1); 18 | add_dnodeint_end(&head, 2); 19 | add_dnodeint_end(&head, 3); 20 | add_dnodeint_end(&head, 4); 21 | add_dnodeint_end(&head, 98); 22 | add_dnodeint_end(&head, 402); 23 | add_dnodeint_end(&head, 1024); 24 | print_dlistint(head); 25 | printf("-----------------\n"); 26 | insert_dnodeint_at_index(&head, 5, 4096); 27 | print_dlistint(head); 28 | free_dlistint(head); 29 | head = NULL; 30 | return (EXIT_SUCCESS); 31 | } 32 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/.main.h.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x18-dynamic_libraries/.main.h.swp -------------------------------------------------------------------------------- /0x18-dynamic_libraries/0-isupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isupper - uppercase letters 5 | * @c: char to check 6 | * 7 | * Prince Solomon 8 | * Return: 0 or 1 9 | */ 10 | 11 | int _isupper(int c) 12 | 13 | { 14 | if (c >= 'A' && c <= 'Z') 15 | return (1); 16 | else 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/0-isupper.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x18-dynamic_libraries/0-isupper.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/0-memset.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _memset - function fill the first @n bytes of the memory area pointed 5 | * to by @s with the constant byte @b 6 | * 7 | * @n: bytes of the memory area pointed to by @s 8 | * 9 | * @s: with the constant byte @b 10 | * 11 | * @b: memory area pointer 12 | * 13 | * Return: a pointer to the memory area @s 14 | */ 15 | char *_memset(char *s, char b, unsigned int n) 16 | { 17 | unsigned int i = 0; 18 | 19 | while (i < n) 20 | { 21 | s[i] = b; 22 | i++; 23 | } 24 | 25 | return (s); 26 | } 27 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/0-memset.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x18-dynamic_libraries/0-memset.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/0-strcat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strcat - concat 2 string 5 | * @dest:char 6 | * @src:char 7 | * Return:char 8 | */ 9 | 10 | char *_strcat(char *dest, char *src) 11 | { 12 | char *s = dest; 13 | 14 | while (*dest != '\0') 15 | { 16 | dest++; 17 | } 18 | 19 | while (*src != '\0') 20 | { 21 | *dest = *src; 22 | dest++; 23 | src++; 24 | } 25 | *dest = '\0'; 26 | return (s); 27 | } /*Prince Solomon*/ 28 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/0-strcat.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x18-dynamic_libraries/0-strcat.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/1-create_dynamic_lib.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -fPIC -c *.c 3 | gcc -shared -o liball.so *.o 4 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/1-isdigit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isdigit - write a function that check for a digit (0 throuh 9) 5 | * @c: characters to check 6 | * Prince Solomon 7 | * Return: 0 or 1 8 | */ 9 | 10 | int _isdigit(int c) 11 | { 12 | if (c >= '0' && c <= '9') 13 | return (1); 14 | else 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/1-isdigit.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x18-dynamic_libraries/1-isdigit.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/1-memcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _memcpy - function copies @n bytes from memory area @src 5 | * to memory area @dest 6 | * 7 | * @n: function copies 8 | * 9 | * @src: bytes from memory area 10 | * 11 | * @dest: to memory area 12 | * 13 | * Return: a pointer to @dest 14 | */ 15 | char *_memcpy(char *dest, char *src, unsigned int n) 16 | { 17 | unsigned int i = 0; 18 | 19 | while (i < n) 20 | { 21 | dest[i] = src[i]; 22 | i++; 23 | } 24 | 25 | return (dest); 26 | } 27 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/1-memcpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x18-dynamic_libraries/1-memcpy.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/1-strncat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strncat - concatenates two strings 5 | * 6 | * @src: The source of strings 7 | * @dest: The destination of the string 8 | * @n: The length of int 9 | * 10 | * Return: pointer to the resulting string dest 11 | */ 12 | char *_strncat(char *dest, char *src, int n) 13 | { 14 | int i, j; 15 | 16 | for (i = 0; dest[i] != '\0'; i++) 17 | { 18 | continue; 19 | } 20 | for (j = 0; src[j] != '\0' && j < n; j++) 21 | { 22 | dest[i + j] = src[j]; 23 | } 24 | dest[i + j] = '\0'; 25 | return (dest); 26 | } 27 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/1-strncat.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x18-dynamic_libraries/1-strncat.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/100-atoi.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x18-dynamic_libraries/100-atoi.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/100-operations.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x18-dynamic_libraries/100-operations.so -------------------------------------------------------------------------------- /0x18-dynamic_libraries/100-test.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/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 /tmp https://github.com/Lexxyla/alx-low_level_programming/raw/master/0x18-dynamic_libraries/nrandom.so 3 | export LD_PRELOAD=/tmp/nrandom.so 4 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/2-strchr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * _strchr - locates a character in a string 5 | * 6 | * @c: occurrence of the character 7 | * 8 | * @s: in the string 9 | * 10 | * Return: a pointer to the first occurrence of the character 11 | */ 12 | 13 | char *_strchr(char *s, char c) 14 | { 15 | while (*s) 16 | { 17 | if (*s != c) 18 | s++; 19 | else 20 | return (s); 21 | } 22 | if (c == '\0') 23 | return (s); 24 | 25 | return (NULL); 26 | } 27 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/2-strchr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x18-dynamic_libraries/2-strchr.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/2-strlen.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strlen - return the length of a string 5 | * [Prince Solomon] 6 | * @s: char to check 7 | * Description: this will return the length of a string 8 | * Return: 0 is (success) 9 | */ 10 | 11 | int _strlen(char *s) 12 | { 13 | int a = 0; 14 | 15 | for (; *s++;) 16 | a++; 17 | return (a); 18 | } 19 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/2-strlen.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x18-dynamic_libraries/2-strlen.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/2-strncpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | *_strncpy - function that copies a string 5 | * 6 | * @src: The source of strings 7 | * @dest: The destination of the string 8 | * @n: The length of int 9 | * 10 | * Return: pointer to the resulting string dest 11 | */ 12 | char *_strncpy(char *dest, char *src, int n) 13 | { 14 | int i; 15 | 16 | for (i = 0; i < n && *(src + i); i++) 17 | { 18 | *(dest + i) = *(src + i); 19 | } 20 | for (; i < n; i++) 21 | { 22 | *(dest + i) = '\0'; 23 | } 24 | return (dest); 25 | 26 | } 27 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/2-strncpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x18-dynamic_libraries/2-strncpy.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/3-islower.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _islower - Checks if a character is lowercase. 5 | * @c: The character to be checked. 6 | * 7 | * Return: 1 if character is lower, 0 otherwise 8 | */ 9 | 10 | int _islower(int c) 11 | { 12 | if (c >= 'a' && c <= 'z') 13 | return (1); 14 | else 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/3-islower.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x18-dynamic_libraries/3-islower.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/3-puts.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _puts - prints a string 5 | * @str: the string 6 | * 7 | * Return: the length of the string 8 | */ 9 | void _puts(char *str) 10 | { 11 | int i = 0; 12 | 13 | while (str[i] != '\0') 14 | { 15 | _putchar(str[i]); 16 | i++; 17 | } 18 | _putchar('\n'); 19 | } 20 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/3-puts.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x18-dynamic_libraries/3-puts.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/3-strcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strcmp - compare 2 string 4 | * @s1:string 5 | * @s2:strmp 6 | * Return:int 7 | */ 8 | int _strcmp(char *s1, char *s2) 9 | { 10 | while (((*s1 != '\0') && (*s2 != '\0')) && (*s1 == *s2)) 11 | { 12 | s1++; 13 | s2++; 14 | } 15 | 16 | if (*s1 == *s2) 17 | { 18 | return (0); 19 | } 20 | 21 | else 22 | { 23 | return (*s1 - *s2); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/3-strcmp.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x18-dynamic_libraries/3-strcmp.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/3-strspn.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strspn - a function that gets the length of a prefix substring 5 | * 6 | * @s: the initial segment of 7 | * 8 | * @accept: which consist only of bytes from 9 | * 10 | * Return: the number of bytes 11 | */ 12 | 13 | unsigned int _strspn(char *s, char *accept) 14 | { 15 | int z = 0, x, y; 16 | 17 | for (x = 0; s[x] != '\0'; x++) 18 | { 19 | if (s[x] != 32) 20 | { 21 | for (y = 0; accept[y] != '\0'; y++) 22 | { 23 | if (s[x] == accept[y]) 24 | z++; 25 | } 26 | } 27 | else 28 | return (z); 29 | } 30 | return (z); 31 | 32 | } /**Code by: Princexz*/ 33 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/3-strspn.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x18-dynamic_libraries/3-strspn.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/4-isalpha.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isalpha - Check if a character is alphabet 5 | * @c: the character to be check 6 | * Prince Solomon 7 | * Return: 1 if charater is letter, lowercase or upper, o upper 8 | */ 9 | int _isalpha(int c) 10 | { 11 | if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) 12 | return (1); 13 | else 14 | return (0); 15 | } 16 | 17 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/4-isalpha.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x18-dynamic_libraries/4-isalpha.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/4-strpbrk.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * _strpbrk - function that searches a string for any of a set of bytes 5 | * 6 | * @s:first occurrence in the string 7 | * 8 | * @accept: matches one of the bytes, or @NULL if no such byte 9 | * 10 | * Return: a pointer to the byte 11 | */ 12 | 13 | char *_strpbrk(char *s, char *accept) 14 | { 15 | int i; 16 | while (*s) 17 | { 18 | for (i = 0; accept[i]; i++) 19 | { 20 | if (*s == accept[i]) 21 | return (s); 22 | } 23 | s++; 24 | } 25 | return (NULL); 26 | } 27 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/4-strpbrk.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x18-dynamic_libraries/4-strpbrk.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/5-strstr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strstr - locates a substring 5 | * 6 | * @haystack: the longer string to search 7 | * @needle: the first occurrence of the substring 8 | * 9 | * Return: a pointer beg of substring or @Null if it not foound. 10 | */ 11 | char *_strstr(char *haystack, char *needle) 12 | { 13 | unsigned int i = 0, j = 0; 14 | 15 | while (haystack[i]) 16 | { 17 | while (needle[j] && (haystack[i] == needle[0])) 18 | { 19 | if (haystack[i + j] == needle[j]) 20 | j++; 21 | else 22 | break; 23 | } 24 | if (needle[j]) 25 | { 26 | i++; 27 | j = 0; 28 | } 29 | else 30 | return (haystack + i); 31 | } 32 | return (0); 33 | } 34 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/5-strstr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x18-dynamic_libraries/5-strstr.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/6-abs.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _abs - computes the value of an integer. 5 | * @n: the integer to be computed. 6 | * 7 | * Return: the absolute value of the integer. 8 | */ 9 | int _abs(int n) 10 | { 11 | if (n >= 0) 12 | return (n); 13 | else 14 | return (-n); 15 | } 16 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/6-abs.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x18-dynamic_libraries/6-abs.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/9-strcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strcpy - copy the string pointed to by src to dest 4 | * @dest: char to check 5 | * @src: char to check 6 | * Return: 0 is success 7 | */ 8 | 9 | char *_strcpy(char *dest, char *src) 10 | { 11 | int a; 12 | 13 | for (a = 0; src[a] != '0'; a++) 14 | dest[a] = src[a]; 15 | dest[a] = '\0'; 16 | return (dest); 17 | } 18 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/9-strcpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x18-dynamic_libraries/9-strcpy.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _putchar - writes the character c to stdout 5 | * @c: The character to print 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | int _putchar(char c) 11 | { 12 | return (write(1, &c, 1)); 13 | } 14 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/_putchar.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x18-dynamic_libraries/TEST/_putchar.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/abs.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_abs - compute the absolute value of an integer 4 | *@n: print character 5 | *Return: 0 6 | */ 7 | int _abs(int n) 8 | { 9 | if (n > 0) 10 | { 11 | return (n); 12 | } 13 | else 14 | return (-n); 15 | } 16 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/abs.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x18-dynamic_libraries/TEST/abs.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/atoi.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x18-dynamic_libraries/TEST/atoi.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/isalpha.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | *_isalpha - check for alphabetic character 5 | *@c: verify character 6 | *Return: 0 7 | */ 8 | int _isalpha(int c) 9 | { 10 | if (c >= 65 && c <= 90) 11 | { 12 | return (1); 13 | } 14 | else if (c >= 97 && c <= 122) 15 | { 16 | return (1); 17 | } 18 | else 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/isalpha.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x18-dynamic_libraries/TEST/isalpha.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/isdigit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | *_isdigit - function to check for digit 5 | *@c: integer 6 | *Return: a 7 | */ 8 | 9 | int _isdigit(int c) 10 | { 11 | int a = 0; 12 | 13 | if (c >= 48 && c <= 57) 14 | a = 1; 15 | return (a); 16 | } 17 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/isdigit.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x18-dynamic_libraries/TEST/isdigit.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/islower.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | *_islower - check for lowercase alphabet 5 | *@c: the character to print 6 | *Return: 0 7 | */ 8 | int _islower(int c) 9 | { 10 | if (c >= 97 && c <= 122) 11 | { 12 | return (1); 13 | } 14 | else 15 | return (0); 16 | } 17 | 18 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/islower.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x18-dynamic_libraries/TEST/islower.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/isupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _isupper - function block 6 | * Description: check for uppercase 7 | * @c: integer 8 | * Return: 0 9 | */ 10 | 11 | int _isupper(int c) 12 | { 13 | int a = 0; 14 | 15 | if (c >= 65 && c <= 90) 16 | a = 1; 17 | return (a); 18 | } 19 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/isupper.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x18-dynamic_libraries/TEST/isupper.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/libfilename.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x18-dynamic_libraries/TEST/libfilename.so -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/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/*MAIN_H*/ 26 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/memcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | *_memcpy - copies bytes from memory area src to memory area dest 5 | *@dest: destination area memory 6 | *@src: source memory area 7 | *@n: data 8 | * 9 | *Return: dest 10 | */ 11 | 12 | char *_memcpy(char *dest, char *src, unsigned int n) 13 | { 14 | unsigned int i; 15 | i = 0; 16 | 17 | while (i < n) 18 | { 19 | dest[i] = src[i]; 20 | i++; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/memcpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x18-dynamic_libraries/TEST/memcpy.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/memset.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | *_memset - fill memory with constant 5 | *@s: space of memory 6 | *@b: byte 7 | *@n: memory area 8 | * 9 | *Return: pointer to the memory area 10 | */ 11 | 12 | char *_memset(char *s, char b, unsigned int n) 13 | { 14 | unsigned int i; 15 | i = 0; 16 | 17 | while (i < n) 18 | { 19 | *(s + i) = b; 20 | i++; 21 | } 22 | return (s); 23 | } 24 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/memset.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x18-dynamic_libraries/TEST/memset.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/puts.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | *_puts - prints a string 5 | *@s: string 6 | * 7 | *Return : nothing 8 | */ 9 | 10 | void _puts(char *s) 11 | { 12 | int i = 0; 13 | 14 | while (s[i] != '\0') 15 | { 16 | _putchar(s[i]); 17 | i++; 18 | } 19 | _putchar('\n'); 20 | } 21 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/puts.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x18-dynamic_libraries/TEST/puts.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/strcat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strcat - appends src to the dest string 5 | * @dest: string to append by src 6 | * @src: string to append to dest 7 | * 8 | * Return: address of dest 9 | */ 10 | char *_strcat(char *dest, char *src) 11 | { 12 | int i, j; 13 | 14 | i = j = 0; 15 | while (*(dest + i)) 16 | i++; 17 | while ((*(dest + i) = *(src + j))) 18 | { 19 | i++; 20 | j++; 21 | } 22 | return (dest); 23 | } 24 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/strcat.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x18-dynamic_libraries/TEST/strcat.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/strchr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | *_strchr - locates character in a string 5 | *@s: string 6 | *@c: character 7 | * 8 | *Return: 0 9 | */ 10 | 11 | char *_strchr(char *s, char c) 12 | { 13 | 14 | do { 15 | if (*s == c) 16 | return ((char *)s); 17 | } 18 | while (*s++) 19 | ; 20 | 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/strchr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x18-dynamic_libraries/TEST/strchr.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/strcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | *_strcmp - compare strings 5 | *@s1: string 6 | *@s2: string 7 | * 8 | *Return: integer j 9 | */ 10 | 11 | int _strcmp(char *s1, char *s2) 12 | { 13 | int i = 0; 14 | int j = 0; 15 | 16 | while (s1[i] == s2[i] && s1[i] != '\0') 17 | { 18 | i++; 19 | } 20 | j += s1[i] - s2[i]; 21 | return (j); 22 | } 23 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/strcmp.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x18-dynamic_libraries/TEST/strcmp.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/strcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | *_strcpy - copy string including the terminating null byte 6 | *@dest: destination of string 7 | *@src: source of string 8 | * 9 | *Return: the value dest 10 | */ 11 | 12 | char *_strcpy(char *dest, char *src) 13 | { 14 | int i = 0; 15 | 16 | for (i = 0; src[i]; i++) 17 | dest[i] = src[i]; 18 | dest[i] = src[i]; 19 | return (dest); 20 | } 21 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/strcpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x18-dynamic_libraries/TEST/strcpy.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/strlen.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | *_strlen - count the length of a string 5 | *@s: string 6 | * 7 | *Return: the value of the length i 8 | */ 9 | 10 | int _strlen(char *s) 11 | { 12 | int i = 0; 13 | 14 | while (s[i]) 15 | i++; 16 | 17 | return (i); 18 | } 19 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/strlen.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x18-dynamic_libraries/TEST/strlen.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/strncat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | *_strncat - concatenate strings 5 | *@dest: destination 6 | *@src: source 7 | *@n: integer 8 | * 9 | *Return: destination 10 | */ 11 | 12 | char *_strncat(char *dest, char *src, int n) 13 | { 14 | int j = 0; 15 | int i = 0; 16 | 17 | while (dest[i] != '\0') 18 | { 19 | i++; 20 | } 21 | 22 | for (j = 0; j < n && src[j] != '\0'; j++) 23 | 24 | dest[j + 1] = src[i]; 25 | dest[j + i + 1] = '\0'; 26 | 27 | return (dest); 28 | } 29 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/strncat.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x18-dynamic_libraries/TEST/strncat.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/strncpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | *_strncpy - copy strings 5 | *@dest: destination 6 | *@src: source 7 | *@n: integer 8 | * 9 | *Return: destination 10 | */ 11 | 12 | char *_strncpy(char *dest, char *src, int n) 13 | { 14 | 15 | int j = 0; 16 | 17 | for (j = 0; src[j] != '\0'; j++) 18 | { 19 | if (j < n) 20 | { 21 | dest[j] = src[j]; 22 | } 23 | } 24 | for (; j < n; j++) 25 | { 26 | dest[j] = '\0'; 27 | } 28 | return (dest); 29 | } 30 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/strncpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x18-dynamic_libraries/TEST/strncpy.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/strpbrk.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | #include 4 | 5 | /** 6 | *_strpbrk - locate the first occurence in the string 7 | *@s: string 8 | *@accept: string 9 | * 10 | *Return: NULL 11 | */ 12 | 13 | char *_strpbrk(char *s, char *accept) 14 | { 15 | int i = 0; 16 | int j = 0; 17 | 18 | for (i = 0; s[i]; i++) 19 | { 20 | for (j = 0; accept[j]; j++) 21 | if (*(accept + j) == s[i]) 22 | return (&(s[i])); 23 | } 24 | return (NULL); 25 | } 26 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/strpbrk.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x18-dynamic_libraries/TEST/strpbrk.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/strspn.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | *_strspn - get the length of a prefix substring 5 | *@s: string 6 | *@accept: string 7 | * 8 | *Return: integer 9 | */ 10 | 11 | unsigned int _strspn(char *s, char *accept) 12 | { 13 | unsigned int a, b; 14 | 15 | for (a = 0; s[a]; a++) 16 | { 17 | for (b = 0; accept[b]; b++) 18 | { 19 | if (s[a] == accept[b]) 20 | break; 21 | } 22 | if (s[a] != accept[b]) 23 | break; 24 | } 25 | return (a); 26 | } 27 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/strspn.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x18-dynamic_libraries/TEST/strspn.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/strstr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | *_strstr - find the first occurence 6 | *@haystack: string 7 | *@needle: string 8 | * 9 | *Return: the pointer to the first occurence 10 | */ 11 | #include 12 | char *_strstr(char *haystack, char *needle) 13 | { 14 | 15 | int i = 0; 16 | int j = 0; 17 | int k, s; 18 | 19 | for (s = 0; needle[s]; s++) 20 | ; 21 | if (s == 0) 22 | return (haystack); 23 | while (haystack[i]) 24 | { 25 | if (haystack[i] == needle[j]) 26 | { 27 | i++; 28 | j++; 29 | for (k = i; haystack[k]; k++) 30 | { 31 | if (needle[j] == '\0') 32 | return (&(haystack[i - 1])); 33 | if (haystack[k] != needle[j]) 34 | break; 35 | j++; 36 | } 37 | } 38 | i++; 39 | j = 0; 40 | } 41 | return (NULL); 42 | } 43 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/strstr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x18-dynamic_libraries/TEST/strstr.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _putchar - writes the character c to stdout 5 | * @c: The character to print 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | int _putchar(char c) 11 | { 12 | return (write(1, &c, 1)); 13 | } 14 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/_putchar.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x18-dynamic_libraries/_putchar.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/iwin.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int rand() 5 | { 6 | static int ct = -1; 7 | 8 | ct++; 9 | if (ct == 0) 10 | return 8; 11 | if (ct == 1) 12 | return 8; 13 | if (ct == 2) 14 | return 7; 15 | if (ct == 3) 16 | return 9; 17 | if (ct == 4) 18 | return 23; 19 | if (ct == 5) 20 | return 74; 21 | return ct * ct % 30000; 22 | } 23 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/iwin.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x18-dynamic_libraries/iwin.so -------------------------------------------------------------------------------- /0x18-dynamic_libraries/libdynamic.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x18-dynamic_libraries/libdynamic.so -------------------------------------------------------------------------------- /0x18-dynamic_libraries/main.h: -------------------------------------------------------------------------------- 1 | #ifndef _MAIN_H_ 2 | #define _MAIN_H_ 3 | 4 | int _putchar(char c); 5 | int _islower(int c); 6 | int _isalpha(int c); 7 | int _abs(int n); 8 | int _isupper(int c); 9 | int _isdigit(int c); 10 | int _strlen(char *s); 11 | void _puts(char *s); 12 | char *_strcpy(char *dest, char *src); 13 | int _atoi(char *s); 14 | char *_strcat(char *dest, char *src); 15 | char *_strncat(char *dest, char *src, int n); 16 | char *_strncpy(char *dest, char *src, int n); 17 | int _strcmp(char *s1, char *s2); 18 | char *_memset(char *s, char b, unsigned int n); 19 | char *_memcpy(char *dest, char *src, unsigned int n); 20 | char *_strchr(char *s, char c); 21 | unsigned int _strspn(char *s, char *accept); 22 | char *_strpbrk(char *s, char *accept); 23 | char *_strstr(char *haystack, char *needle); 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/nrandom.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Princexz/alx-low_level_programming/428316f728e8f8b5d01413e9c2a895cf3b836ef5/0x18-dynamic_libraries/nrandom.so -------------------------------------------------------------------------------- /0x18-dynamic_libraries/test.c: -------------------------------------------------------------------------------- 1 | int add(int a, int b) 2 | { 3 | return (a + b); 4 | } 5 | 6 | int sub(int a, int b) 7 | { 8 | return (a - b); 9 | } 10 | 11 | int mul(int a, int b) 12 | { 13 | return(a * b); 14 | } 15 | 16 | int div(int a, int b) 17 | { 18 | if (b != 0) 19 | return (a / b); 20 | return (0); 21 | } 22 | 23 | int mod(int a, int b) 24 | { 25 | if (b != 0) 26 | return (a % b); 27 | return (0); 28 | } 29 | -------------------------------------------------------------------------------- /0x1A-hash_tables/0-hash_table_create.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_create - creates a hash table 5 | * @size: size of the array 6 | * 7 | * Return: a pointer to the newly created hash table 8 | * NULL if something went wrong 9 | */ 10 | hash_table_t *hash_table_create(unsigned long int size) 11 | { 12 | hash_table_t *hash_table; 13 | 14 | hash_table = malloc(sizeof(hash_table_t)); 15 | if (!hash_table) 16 | return (NULL); 17 | 18 | hash_table->size = size; 19 | hash_table->array = calloc(size, sizeof(hash_node_t *)); 20 | if (!(hash_table->array)) 21 | { 22 | free(hash_table); 23 | return (NULL); 24 | } 25 | 26 | return (hash_table); 27 | } 28 | -------------------------------------------------------------------------------- /0x1A-hash_tables/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "hash_tables.h" 5 | 6 | /** 7 | * main - check the code for 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | hash_table_t *ht; 14 | 15 | ht = hash_table_create(1024); 16 | printf("%p\n", (void *)ht); 17 | return (EXIT_SUCCESS); 18 | } 19 | -------------------------------------------------------------------------------- /0x1A-hash_tables/1-djb2.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_djb2 - Hash function implementing the djb2 algorithm. 5 | * @str: The string to hash. 6 | * 7 | * Return: The calculated hash. 8 | */ 9 | unsigned long int hash_djb2(const unsigned char *str) 10 | { 11 | unsigned long int hash; 12 | int c; 13 | 14 | hash = 5381; 15 | while ((c = *str++)) 16 | hash = ((hash << 5) + hash) + c; /* hash * 33 + c */ 17 | 18 | return (hash); 19 | } 20 | -------------------------------------------------------------------------------- /0x1A-hash_tables/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "hash_tables.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | char *s; 14 | 15 | s = "cisfun"; 16 | printf("%lu\n", hash_djb2((unsigned char *)s)); 17 | s = "Don't forget to tweet today"; 18 | printf("%lu\n", hash_djb2((unsigned char *)s)); 19 | s = "98"; 20 | printf("%lu\n", hash_djb2((unsigned char *)s)); 21 | return (EXIT_SUCCESS); 22 | } 23 | -------------------------------------------------------------------------------- /0x1A-hash_tables/2-key_index.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * key_index - Get the index at which a key/value 5 | * pair should be stored in array of a hash table. 6 | * @key: The key to get the index of. 7 | * @size: The size of the array of the hash table. 8 | * 9 | * Return: The index of the key. 10 | * Description: Uses the djb2 algorithm. 11 | */ 12 | unsigned long int key_index(const unsigned char *key, unsigned long int size) 13 | { 14 | return (hash_djb2(key) % size); 15 | } 16 | -------------------------------------------------------------------------------- /0x1A-hash_tables/3-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "hash_tables.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | hash_table_t *ht; 14 | 15 | ht = hash_table_create(1024); 16 | hash_table_set(ht, "betty", "cool"); 17 | return (EXIT_SUCCESS); 18 | } 19 | -------------------------------------------------------------------------------- /0x1A-hash_tables/5-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "hash_tables.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | hash_table_t *ht; 14 | 15 | ht = hash_table_create(1024); 16 | hash_table_print(ht); 17 | hash_table_set(ht, "c", "fun"); 18 | hash_table_set(ht, "python", "awesome"); 19 | hash_table_set(ht, "Bob", "and Kris love asm"); 20 | hash_table_set(ht, "N", "queens"); 21 | hash_table_set(ht, "Asterix", "Obelix"); 22 | hash_table_set(ht, "Betty", "Cool"); 23 | hash_table_set(ht, "98", "Battery Street"); 24 | hash_table_print(ht); 25 | return (EXIT_SUCCESS); 26 | } 27 | -------------------------------------------------------------------------------- /0x1A-hash_tables/6-hash_table_delete.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_delete - Deletes a hash table. 5 | * @ht: A pointer to a hash table. 6 | */ 7 | void hash_table_delete(hash_table_t *ht) 8 | { 9 | hash_table_t *head = ht; 10 | hash_node_t *node, *tmp; 11 | unsigned long int i; 12 | 13 | for (i = 0; i < ht->size; i++) 14 | { 15 | if (ht->array[i] != NULL) 16 | { 17 | node = ht->array[i]; 18 | while (node != NULL) 19 | { 20 | tmp = node->next; 21 | free(node->key); 22 | free(node->value); 23 | free(node); 24 | node = tmp; 25 | } 26 | } 27 | } 28 | free(head->array); 29 | free(head); 30 | } 31 | -------------------------------------------------------------------------------- /0x1A-hash_tables/tests/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "../hash_tables.h" 5 | 6 | /** 7 | * main - check the code for ALX-Africa Cohort 5 Students. 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | hash_table_t *ht; 14 | 15 | ht = hash_table_create(1024); 16 | printf("%p\n", (void *)ht); 17 | return (EXIT_SUCCESS); 18 | } 19 | -------------------------------------------------------------------------------- /0x1A-hash_tables/tests/3-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "../hash_tables.h" 5 | 6 | /** 7 | * main - check the code for ALX-Africa Cohort 5 Students. 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | hash_table_t *ht; 14 | 15 | ht = hash_table_create(1024); 16 | hash_table_set(ht, "betty", "holberton"); 17 | return (EXIT_SUCCESS); 18 | } 19 | -------------------------------------------------------------------------------- /0x1A-hash_tables/tests/5-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "../hash_tables.h" 5 | 6 | /** 7 | * main - check the code for ALX-Africa Cohort 5 Students. 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | hash_table_t *ht; 14 | 15 | ht = hash_table_create(1024); 16 | hash_table_print(ht); 17 | hash_table_set(ht, "c", "fun"); 18 | hash_table_set(ht, "python", "awesome"); 19 | hash_table_set(ht, "Jennie", "and Jay love asm"); 20 | hash_table_set(ht, "N", "queens"); 21 | hash_table_set(ht, "Asterix", "Obelix"); 22 | hash_table_set(ht, "Betty", "Holberton"); 23 | hash_table_set(ht, "98", "Battery Street"); 24 | hash_table_print(ht); 25 | return (EXIT_SUCCESS); 26 | } 27 | -------------------------------------------------------------------------------- /0x1C-makefiles/0-Makefile: -------------------------------------------------------------------------------- 1 | all: main.c school.c 2 | gcc main.c school.c -o school 3 | -------------------------------------------------------------------------------- /0x1C-makefiles/1-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | all: $(SRC) 4 | $(CC) $(SRC) -o school 5 | -------------------------------------------------------------------------------- /0x1C-makefiles/100-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | OBJ = $(SRC:.c=.o) 4 | NAME = school 5 | CFLAGS = -Wall -Werror -Wextra -pedantic 6 | 7 | .PHONY: all clean oclean fclean re 8 | 9 | all: m.h $(OBJ) 10 | $(CC) $(OBJ) -o $(NAME) 11 | 12 | clean: 13 | $(RM) *~ $(NAME) 14 | 15 | oclean: 16 | $(RM) $(OBJ) 17 | 18 | fclean: clean oclean 19 | 20 | re: fclean all 21 | -------------------------------------------------------------------------------- /0x1C-makefiles/2-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | OBJ = $(SRC:.c=.o) 4 | NAME = school 5 | 6 | all: $(OBJ) 7 | $(CC) $(OBJ) -o $(NAME) 8 | -------------------------------------------------------------------------------- /0x1C-makefiles/3-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | OBJ = $(SRC:.c=.o) 4 | NAME = school 5 | RM = rm -f 6 | 7 | all: $(OBJ) 8 | $(CC) $(OBJ) -o $(NAME) 9 | 10 | clean: 11 | $(RM) *~ $(NAME) 12 | 13 | oclean: 14 | $(RM) $(OBJ) 15 | 16 | fclean: clean oclean 17 | 18 | re: fclean all 19 | -------------------------------------------------------------------------------- /0x1C-makefiles/4-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | OBJ = $(SRC:.c=.o) 4 | NAME = school 5 | RM = rm -f 6 | CFLAGS = -Wall -Werror -Wextra -pedantic 7 | 8 | all: $(OBJ) 9 | $(CC) $(OBJ) -o $(NAME) 10 | 11 | clean: 12 | $(RM) *~ $(NAME) 13 | 14 | oclean: 15 | $(RM) $(OBJ) 16 | 17 | fclean: clean oclean 18 | 19 | re: fclean all 20 | -------------------------------------------------------------------------------- /0x1C-makefiles/m.h: -------------------------------------------------------------------------------- 1 | #ifndef __M_H__ 2 | #define __M_H__ 3 | 4 | #include 5 | #include 6 | 7 | void print_school(void); 8 | 9 | #endif 10 | -------------------------------------------------------------------------------- /0x1C-makefiles/main.c: -------------------------------------------------------------------------------- 1 | #include "m.h" 2 | 3 | /** 4 | * main - Entry point 5 | * 6 | * Return: Always 7 | */ 8 | int main(void) 9 | { 10 | print_school(); 11 | return (EXIT_SUCCESS); 12 | } 13 | -------------------------------------------------------------------------------- /0x1C-makefiles/tests/5-main.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | """ 3 | 5-main 4 | """ 5 | island_perimeter = __import__('5-island_perimeter').island_perimeter 6 | 7 | if __name__ == "__main__": 8 | grid = [ 9 | [0, 0, 0, 0, 0, 0], 10 | [0, 1, 0, 0, 0, 0], 11 | [0, 1, 0, 0, 0, 0], 12 | [0, 1, 1, 1, 0, 0], 13 | [0, 0, 0, 0, 0, 0] 14 | ] 15 | print(island_perimeter(grid)) 16 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/0-linear.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * linear_search - searches for a value in an array of 5 | * integers using the Linear search algorithm 6 | * 7 | * @array: input array 8 | * @size: size of the array 9 | * @value: value to search in 10 | * Return: Always EXIT_SUCCESS 11 | */ 12 | int linear_search(int *array, size_t size, int value) 13 | { 14 | int i; 15 | 16 | if (array == NULL) 17 | return (-1); 18 | 19 | for (i = 0; i < (int)size; i++) 20 | { 21 | printf("Value checked array[%u] = [%d]\n", i, array[i]); 22 | if (value == array[i]) 23 | return (i); 24 | } 25 | return (-1); 26 | } 27 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/101-O: -------------------------------------------------------------------------------- 1 | O(sqrt(n)) 2 | -------------------------------------------------------------------------------- /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 | --------------------------------------------------------------------------------