├── 0x00-hello_world ├── 0-preprocessor ├── 1-compiler ├── 100-intel ├── 101-quote.c ├── 2-assembler ├── 3-name ├── 4-puts.c ├── 5-printf.c ├── 6-size.c └── README.md ├── 0x01-variables_if_else_while ├── 0-positive_or_negative.c ├── 1-last_digit.c ├── 100-print_comb3.c ├── 101-print_comb4.c ├── 102-print_comb5.c ├── 2-print_alphabet.c ├── 3-print_alphabets.c ├── 4-print_alphabt.c ├── 5-print_numbers.c ├── 6-print_numberz.c ├── 7-print_tebahpla.c ├── 8-print_base16.c ├── 9-print_comb.c └── README.md ├── 0x02-functions_nested_loops ├── 0-putchar.c ├── 1-alphabet.c ├── 10-add.c ├── 100-times_table.c ├── 101-natural.c ├── 102-fibonacci.c ├── 103-fibonacci.c ├── 104-fibonacci.c ├── 11-print_to_98.c ├── 2-print_alphabet_x10.c ├── 3-islower.c ├── 4-isalpha.c ├── 5-sign.c ├── 6-abs.c ├── 7-print_last_digit.c ├── 8-24_hours.c ├── 9-times_table.c ├── README.md └── main.h ├── 0x03-debugging ├── 0-main.c ├── 1-main.c ├── 2-largest_number.c ├── 3-print_remaining_days.c ├── README.md └── main.h ├── 0x04-more_functions_nested_loops ├── 0-isupper.c ├── 1-isdigit.c ├── 10-main.c ├── 10-print_triangle.c ├── 10-triangles ├── 100-prime_factor.c ├── 101-print_number.c ├── 2-mul.c ├── 3-print_numbers.c ├── 4-print_most_numbers.c ├── 5-more_numbers.c ├── 6-print_line.c ├── 7-print_diagonal.c ├── 8-print_square.c ├── 9-fizz_buzz.c ├── README.md ├── _putchar.c └── main.h ├── 0x05-pointers_arrays_strings ├── .6-puts2.c.swp ├── .README.md.swp ├── 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 ├── 2-strncpy.c ├── 3-strcmp.c ├── 4-rev_array.c ├── 5-string_toupper.c ├── 6-cap_string.c ├── 7-leet.c ├── README.md └── main.h ├── 0x07-pointers_arrays_strings ├── 0-memset.c ├── 1-memcpy.c ├── 100-set_string.c ├── 101-crackme_password ├── 2-main.c ├── 2-strchr ├── 2-strchr.c ├── 3-strspn.c ├── 4-strpbrk.c ├── 5-strstr.c ├── 7-print_chessboard.c ├── 8-print_diagsums.c ├── README.md ├── main.h └── temp ├── 0x08-recursion ├── 0-puts_recursion.c ├── 1-print_rev_recursion.c ├── 101-wildcmp.c ├── 2-strlen_recursion.c ├── 3-factorial.c ├── 4-pow_recursion.c ├── 5-sqrt_recursion.c ├── 6-is_prime_number.c ├── 7-is_palindrome.c ├── README.md └── main.h ├── 0x09-static_libraries ├── README.md ├── c-files │ ├── 0-isupper.c │ ├── 0-memset.c │ ├── 0-strcat.c │ ├── 1-isdigit.c │ ├── 1-memcpy.c │ ├── 1-strncat.c │ ├── 100-atoi.c │ ├── 2-strchr.c │ ├── 2-strlen.c │ ├── 2-strncpy.c │ ├── 3-islower.c │ ├── 3-puts.c │ ├── 3-strcmp.c │ ├── 3-strspn.c │ ├── 4-isalpha.c │ ├── 4-strpbrk.c │ ├── 5-strstr.c │ ├── 6-abs.c │ ├── 9-strcpy.c │ └── _putchar.c ├── create_static_lib.sh ├── libmy.a ├── main.h └── object-files │ ├── 0-isupper.o │ ├── 0-memset.o │ ├── 0-strcat.o │ ├── 1-isdigit.o │ ├── 1-memcpy.o │ ├── 1-strncat.o │ ├── 100-atoi.o │ ├── 2-strchr.o │ ├── 2-strlen.o │ ├── 2-strncpy.o │ ├── 3-islower.o │ ├── 3-puts.o │ ├── 3-strcmp.o │ ├── 3-strspn.o │ ├── 4-isalpha.o │ ├── 4-strpbrk.o │ ├── 5-strstr.o │ ├── 6-abs.o │ ├── 9-strcpy.o │ └── _putchar.o ├── 0x0A-argc_argv ├── 0-whatsmyname.c ├── 1-args.c ├── 100-change.c ├── 2-args.c ├── 3-mul.c ├── 4-add.c └── README.md ├── 0x0B-malloc_free ├── 0-create_array.c ├── 1-strdup.c ├── 100-argstostr.c ├── 101-strtow.c ├── 2-str_concat.c ├── 3-alloc_grid.c ├── 4-free_grid.c ├── README.md ├── main.h └── test_files │ ├── 0-main.c │ ├── 1-main.c │ ├── 2-main.c │ └── 3-main.c ├── 0x0C-more_malloc_free ├── 0-malloc_checked.c ├── 1-string_nconcat.c ├── 100-realloc ├── 100-realloc.c ├── 101-mul.c ├── 2-calloc.c ├── 3-array_range.c ├── README.md ├── main.h └── test │ ├── 0-main.c │ ├── 1-main.c │ ├── 100-main.c │ ├── 100-realloc.exe │ ├── 2-calloc │ ├── 2-calloc.exe │ ├── 2-main.c │ ├── 3-array_range.exe │ ├── 3-main.c │ ├── result │ └── result.exe ├── 0x0D-preprocessor ├── 0-object_like_macro.h ├── 1-pi.h ├── 2-main.c ├── 3-function_like_macro.h ├── 3-main.c ├── 3-main.exe ├── 4-main.c ├── 4-main.exe ├── 4-sum.h ├── README.md ├── Test │ ├── 0-main.c │ └── 1-main.c └── d.exe ├── 0x0E-structures_typedef ├── 0-main.c ├── 1-init_dog.c ├── 2-print_dog.c ├── 4-new_dog.c ├── 5-free_dog.c ├── README.md ├── dog.h └── test │ ├── 0-main.exe │ ├── 1-main.c │ ├── 2-main.c │ ├── 4-main.c │ ├── 5-main.c │ ├── b.exe │ ├── c.exe │ └── e.exe ├── 0x0F-function_pointers ├── 0-main.c ├── 0-print_name.c ├── 1-array_iterator.c ├── 1-main.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 ├── a.exe ├── b.exe ├── c.exe ├── calc.exe └── function_pointers.h ├── 0x10-variadic_functions ├── 0-sum_them_all.c ├── 1-print_numbers.c ├── 2-print_strings.c ├── 3-main.c ├── 3-print_all.c ├── README.md ├── d.exe └── variadic_functions.h ├── 0x12-singly_linked_lists ├── 0-print_list.c ├── 1-list_len.c ├── 100-first.c ├── 101-hello_holberton.asm ├── 101-hello_holberton.o ├── 2-add_node.c ├── 3-add_node_end.c ├── 4-free_list.c ├── README.md ├── Tests │ ├── 0-main.c │ ├── 1-main.c │ ├── 100-main.c │ ├── 101-hello_holberton.o │ ├── 2-main.c │ ├── 4-main.c │ └── a.out ├── a.out ├── hello └── lists.h ├── 0x13-more_singly_linked_lists ├── 0-print_listint.c ├── 1-listint_len.c ├── 10-delete_nodeint.c ├── 2-add_nodeint.c ├── 3-add_nodeint_end.c ├── 4-free_listint.c ├── 5-free_listint2.c ├── 6-pop_listint.c ├── 7-get_nodeint.c ├── 8-sum_listint.c ├── 9-insert_nodeint.c ├── README.md ├── Tests │ ├── 0-main.c │ ├── 1-main.c │ ├── 10-main.c │ ├── 2-main.c │ ├── 3-main.c │ ├── 6-main.c │ ├── 7-main.c │ ├── 8-main.c │ ├── 9-main.c │ ├── a.exe │ ├── b.exe │ ├── c.exe │ ├── d.exe │ ├── g.exe │ ├── h.exe │ ├── i.exe │ ├── j.exe │ └── k.exe └── lists.h ├── 0x14-bit_manipulation ├── 0-binary_to_uint.c ├── 0-main.c ├── 1-main.c ├── 1-print_binary.c ├── 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 ├── a.exe ├── b.exe ├── c.exe └── main.h ├── 0x15-file_io ├── 0-main.c ├── 0-read_textfile.c ├── 1-create_file.c ├── 1-main.c ├── 2-append_text_to_file.c ├── 2-main.c ├── 3-cp.c ├── README.md ├── Requiescat └── main.h ├── 0x17-doubly_linked_lists ├── 0-main.c ├── 0-print_dlistint.c ├── 1-dlistint_len.c ├── 1-main.c ├── 100-password ├── 102-result ├── 103-keygen.c ├── 2-add_dnodeint.c ├── 2-main.c ├── 3-add_dnodeint_end.c ├── 3-main.c ├── 4-free_dlistint.c ├── 4-m ├── 4-main.c ├── 5-get_dnodeint.c ├── 5-main.c ├── 6-main.c ├── 6-sum_dlistint.c ├── 7-insert_dnodeint.c ├── 7-main.c ├── 8-delete_dnodeint.c ├── 8-main.c ├── README.md ├── e ├── h ├── j ├── k └── lists.h ├── 0x18-dynamic_libraries ├── 1-create_dynamic_lib.sh ├── 100-operations.c ├── 100-operations.o ├── 100-operations.so ├── 100-tests.py ├── 101-make_me_win.sh ├── README.md ├── c-files │ ├── 0-isupper.c │ ├── 0-memset.c │ ├── 0-strcat.c │ ├── 1-isdigit.c │ ├── 1-memcpy.c │ ├── 1-strncat.c │ ├── 100-atoi.c │ ├── 2-strchr.c │ ├── 2-strlen.c │ ├── 2-strncpy.c │ ├── 3-islower.c │ ├── 3-puts.c │ ├── 3-strcmp.c │ ├── 3-strspn.c │ ├── 4-isalpha.c │ ├── 4-strpbrk.c │ ├── 5-strstr.c │ ├── 6-abs.c │ ├── 9-strcpy.c │ └── _putchar.c ├── libdynamic.so ├── libmask.so ├── main.h └── object-files │ ├── 0-isupper.o │ ├── 0-memset.o │ ├── 0-strcat.o │ ├── 1-isdigit.o │ ├── 1-memcpy.o │ ├── 1-strncat.o │ ├── 100-atoi.o │ ├── 2-strchr.o │ ├── 2-strlen.o │ ├── 2-strncpy.o │ ├── 3-islower.o │ ├── 3-puts.o │ ├── 3-strcmp.o │ ├── 3-strspn.o │ ├── 4-isalpha.o │ ├── 4-strpbrk.o │ ├── 5-strstr.o │ ├── 6-abs.o │ ├── 9-strcpy.o │ └── _putchar.o ├── 0x1A-hash_tables ├── 0-hash_table_create.c ├── 0-main.c ├── 1-djb2.c ├── 1-main.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 ├── a ├── b ├── c ├── d ├── e ├── f ├── g └── hash_tables.h ├── 0x1C-makefiles ├── 0-Makefile ├── 1-Makefile ├── 100-Makefile ├── 2-Makefile ├── 3-Makefile ├── 4-Makefile ├── 5-island_perimeter.py ├── 5-main.py ├── README.md ├── main.c ├── main.h ├── main.o ├── school ├── school.c └── school.o ├── 0x1E-search_algorithms ├── 0-linear.c ├── 0-main.c ├── 1-binary.c ├── 1-main.c ├── 100-jump.c ├── 101-O ├── 102-interpolation.c ├── 103-exponential.c ├── 104-advanced_binary.c ├── 105-jump_list.c ├── 106-linear_skip.c ├── 107-O ├── 108-O ├── 2-O ├── 3-O ├── 4-O ├── 5-O ├── 6-O ├── README.md └── search_algos.h └── README.md /0x00-hello_world/0-preprocessor: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # other alternative solutions: 3 | # gcc -E $CFILE -o c 4 | # gcc -save-temps source_file && mv source_file.i file_name 5 | gcc -E $CFILE > c 6 | -------------------------------------------------------------------------------- /0x00-hello_world/1-compiler: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -c $CFILE 3 | -------------------------------------------------------------------------------- /0x00-hello_world/100-intel: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -S -masm=intel $CFILE 3 | -------------------------------------------------------------------------------- /0x00-hello_world/101-quote.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - write a phrase to the standard error 4 | * 5 | * Return: 1 at the end 6 | * 7 | */ 8 | int main(void) 9 | { 10 | 11 | char str[] = "and that piece of art is useful\" - Dora Korpar, 2015-10-19\n"; 12 | 13 | write(2, str, 59); 14 | return (1); 15 | 16 | } 17 | -------------------------------------------------------------------------------- /0x00-hello_world/2-assembler: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -S $CFILE 3 | -------------------------------------------------------------------------------- /0x00-hello_world/3-name: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc $CFILE > cisfun 3 | -------------------------------------------------------------------------------- /0x00-hello_world/4-puts.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - prints a phrase 4 | * 5 | * Return: 0 at the end of the program 6 | * 7 | * 8 | */ 9 | int main(void) 10 | { 11 | 12 | puts("\"Programming is like building a multilingual puzzle"); 13 | return (0); 14 | 15 | } 16 | -------------------------------------------------------------------------------- /0x00-hello_world/5-printf.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - prints a phrase 4 | * 5 | * Return: 0 at the end of the program 6 | * 7 | * 8 | */ 9 | int main(void) 10 | { 11 | 12 | printf("with proper grammar, but the outcome is a piece of art,\n"); 13 | return (0); 14 | 15 | } 16 | -------------------------------------------------------------------------------- /0x00-hello_world/6-size.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - prints the sizes of various types on the computer 4 | * 5 | * Return: 0 at the end of the program 6 | * 7 | * 8 | */ 9 | int main(void) 10 | { 11 | printf("Size of a char: %lu byte(s)\n", sizeof(char)); 12 | printf("Size of an int: %lu byte(s)\n", sizeof(int)); 13 | printf("Size of a long int: %lu byte(s)\n", sizeof(long int)); 14 | printf("Size of a long long int: %lu byte(s)\n", sizeof(long long int)); 15 | printf("Size of a float: %lu byte(s)\n", sizeof(float)); 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/0-positive_or_negative.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | /* more headers goes there */ 5 | 6 | /** 7 | * main - determines if the value of the random number 8 | * * generated is positive, zero or negative 9 | * 10 | * Return: returns zero ar the end 11 | */ 12 | 13 | /* betty style doc for function main goes there */ 14 | int main(void) 15 | { 16 | int n; 17 | 18 | srand(time(0)); 19 | n = rand() - RAND_MAX / 2; 20 | /* your code goes there */ 21 | if (n > 0) 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 30 | { 31 | printf("%d is negative\n", n); 32 | } 33 | return (0); 34 | } 35 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/1-last_digit.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | /* more headers goes there */ 5 | 6 | /** 7 | * main - determines if the value of the random number 8 | * * generated is positive, zero or negative 9 | * 10 | * Return: returns zero ar the end 11 | */ 12 | 13 | /* betty style doc for function main goes there */ 14 | int main(void) 15 | { 16 | int n, LastDigit; 17 | 18 | srand(time(0)); 19 | n = rand() - RAND_MAX / 2; 20 | /* your code goes there */ 21 | LastDigit = n % 10; 22 | if (LastDigit > 5) 23 | { 24 | printf("Last digit of %d is %d and is greater than 5\n", n, LastDigit); 25 | } 26 | else if (LastDigit == 0) 27 | { 28 | printf("Last digit of %d is %d and is 0\n", n, LastDigit); 29 | } 30 | else 31 | { 32 | printf("Last digit of %d is %d and is less than 6 and not 0\n", n, LastDigit); 33 | } 34 | return (0); 35 | } 36 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/100-print_comb3.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - print single digit numbers starting from 0 4 | * 5 | * Return: returns zero ar the end 6 | */ 7 | int main(void) 8 | { 9 | int i, j; 10 | 11 | for (i = 0; i <= 9; i++) 12 | { 13 | for (j = 1; j <= 9; j++) 14 | { 15 | if (j > i) 16 | { 17 | putchar(i + '0'); 18 | putchar(j + '0'); 19 | if (i != 8) 20 | { 21 | putchar(','); 22 | putchar(' '); 23 | } 24 | } 25 | } 26 | } 27 | putchar('\n'); 28 | return (0); 29 | } 30 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/101-print_comb4.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - print possible combo of 3 4 | * 5 | * Return: returns zero ar the end 6 | */ 7 | int main(void) 8 | { 9 | int i, j, k; 10 | 11 | for (i = 0; i <= 9; i++) 12 | { 13 | for (j = 1; j <= 9; j++) 14 | { 15 | for (k = 2; k <= 9; k++) 16 | { 17 | if (k > j && j > i) 18 | { 19 | putchar(i + '0'); 20 | putchar(j + '0'); 21 | putchar(k + '0'); 22 | if (i != 7) 23 | { 24 | putchar(','); 25 | putchar(' '); 26 | } 27 | } 28 | } 29 | } 30 | } 31 | putchar('\n'); 32 | return (0); 33 | } 34 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/102-print_comb5.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - print possible combo of two 2-digit 4 | * numbers 5 | * 6 | * Return: returns zero ar the end 7 | */ 8 | int main(void) 9 | { 10 | int i, j; 11 | 12 | for (i = 0; i < 100; i++) 13 | { 14 | for (j = 0; j < 100; j++) 15 | { 16 | if (j > i) 17 | { 18 | putchar((i / 10) + '0'); 19 | putchar((i % 10) + '0'); 20 | putchar(' '); 21 | putchar((j / 10) + '0'); 22 | putchar((j % 10) + '0'); 23 | if (i != 98) 24 | { 25 | putchar(','); 26 | putchar(' '); 27 | } 28 | } 29 | } 30 | } 31 | putchar('\n'); 32 | return (0); 33 | } 34 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/2-print_alphabet.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - determines if the value of the random number 4 | * * generated is positive, zero or negative 5 | * 6 | * Return: returns zero ar the end 7 | */ 8 | int main(void) 9 | { 10 | char ch; 11 | 12 | for (ch = 'a'; ch <= 'z'; ch++) 13 | { 14 | putchar(ch); 15 | } 16 | putchar('\n'); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/3-print_alphabets.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - ilphabet in lowercase, and then uppercase 4 | * 5 | * 6 | * Return: returns zero ar the end 7 | */ 8 | int main(void) 9 | { 10 | char ch; 11 | 12 | for (ch = 'a'; ch <= 'z'; ch++) 13 | { 14 | putchar(ch); 15 | } 16 | for (ch = 'A'; ch <= 'Z'; ch++) 17 | { 18 | putchar(ch); 19 | } 20 | putchar('\n'); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/4-print_alphabt.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - print alphabet in lowercase, and then uppercase 4 | * except q and e 5 | * 6 | * Return: returns zero ar the end 7 | */ 8 | int main(void) 9 | { 10 | char ch; 11 | 12 | for (ch = 'a'; ch <= 'z'; ch++) 13 | { 14 | if (ch != 'q' && ch != 'e') 15 | { 16 | putchar(ch); 17 | } 18 | } 19 | putchar('\n'); 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/5-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - print single digit numbers starting from 0 4 | * 5 | * Return: returns zero ar the end 6 | */ 7 | int main(void) 8 | { 9 | int i; 10 | 11 | for (i = 0; i < 10; i++) 12 | { 13 | printf("%d", i); 14 | } 15 | printf("\n"); 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/6-print_numberz.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - print single digit numbers starting from 0 4 | * 5 | * Return: returns zero ar the end 6 | */ 7 | int main(void) 8 | { 9 | int i; 10 | 11 | for (i = 0; i < 10; i++) 12 | { 13 | putchar(i + '0'); 14 | } 15 | putchar('\n'); 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/7-print_tebahpla.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - print alphabet in lowercase, but in reverse 4 | * 5 | * Return: returns zero ar the end 6 | */ 7 | int main(void) 8 | { 9 | char ch; 10 | 11 | for (ch = 'z'; ch >= 'a'; ch--) 12 | { 13 | putchar(ch); 14 | } 15 | putchar('\n'); 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/8-print_base16.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - print single digit numbers starting from 0 4 | * 5 | * Return: returns zero ar the end 6 | */ 7 | int main(void) 8 | { 9 | int i; 10 | char ch; 11 | 12 | for (i = 0; i < 10; i++) 13 | { 14 | putchar(i + '0'); 15 | } 16 | for (ch = 'a'; ch <= 'f'; ch++) 17 | { 18 | putchar(ch); 19 | } 20 | putchar('\n'); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/9-print_comb.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - print single digit numbers starting from 0 4 | * 5 | * Return: returns zero ar the end 6 | */ 7 | int main(void) 8 | { 9 | int i; 10 | 11 | for (i = 0; i < 10; i++) 12 | { 13 | putchar(i + '0'); 14 | if (i < 9) 15 | { 16 | putchar(','); 17 | putchar(' '); 18 | } 19 | } 20 | putchar('\n'); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/0-putchar.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - Print the word "_putchar" 5 | * 6 | * Return: Always 0 7 | */ 8 | 9 | int main(void) 10 | { 11 | int i; 12 | char p[] = "_putchar"; 13 | 14 | for (i = 0; i < 8; i++) 15 | { 16 | _putchar(p[i]); 17 | } 18 | _putchar('\n'); 19 | 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/1-alphabet.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_alphabet - Print the alphabet in lowercase. 5 | * 6 | * Return: void. 7 | */ 8 | 9 | void print_alphabet(void) 10 | { 11 | int i; 12 | 13 | for (i = 97; i < 123; i++) 14 | { 15 | _putchar(i); 16 | } 17 | _putchar('\n'); 18 | } 19 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/10-add.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * add - a function that adds two integers 5 | * and returns the result. 6 | * 7 | * @a: number one. 8 | * @b: number two. 9 | * 10 | * Return: Add of number one and number two. 11 | */ 12 | 13 | int add(int a, int b) 14 | { 15 | return (a + b); 16 | } 17 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/101-natural.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - Lists all the natural numbers below 1024 (excluded) 4 | * that are multiples of 3 or 5. 5 | * 6 | * Return: Always (0). 7 | */ 8 | 9 | int main(void) 10 | { 11 | int i, sum; 12 | 13 | sum = 0; 14 | for (i = 0; i < 1024; i++) 15 | { 16 | if ((i % 3) == 0 || (i % 5) == 0) 17 | sum += i; 18 | } 19 | printf("%d\n", sum); 20 | 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/102-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - Prints the first 50 Fibonacci numbers, starting with 1 and 2, 4 | * 5 | * Return: Always (0). 6 | */ 7 | 8 | int main(void) 9 | { 10 | int i; 11 | unsigned long fib, prevFib, prev2Fib; 12 | 13 | prevFib = 1, prev2Fib = 0; 14 | 15 | for (i = 0; i < 50; i++) 16 | { 17 | fib = prevFib + prev2Fib; 18 | printf("%lu", fib); 19 | 20 | prev2Fib = prevFib; 21 | prevFib = fib; 22 | if (i == 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 | /** 4 | * main - Prints the sum of even-valued Fibonacci sequence 5 | * terms not exceeding 4000000. 6 | * 7 | * Return: Always 0. 8 | */ 9 | 10 | int main(void) 11 | { 12 | unsigned long fib, prevFib, prev2Fib; 13 | float sum; 14 | 15 | prevFib = 1; 16 | prev2Fib = 0; 17 | 18 | while (1) 19 | { 20 | fib = prevFib + prev2Fib; 21 | 22 | if (fib > 4000000) 23 | break; 24 | 25 | if ((fib % 2) == 0) 26 | sum += fib; 27 | prev2Fib = prevFib; 28 | prevFib = fib; 29 | 30 | } 31 | 32 | printf("%.0f\n", sum); 33 | 34 | return (0); 35 | } 36 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/11-print_to_98.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * print_to_98 - prints all natural numbers 5 | * from n to 98, followed by a new line 6 | * 7 | * @n: input number. 8 | * 9 | * Return: no return. 10 | */ 11 | 12 | void print_to_98(int n) 13 | { 14 | if (n > 98) 15 | { 16 | for (; n > 98; n--) 17 | { 18 | printf("%d, ", n); 19 | } 20 | } 21 | else if (n < 98) 22 | { 23 | for (; n < 98; n++) 24 | { 25 | printf("%d, ", n); 26 | } 27 | } 28 | printf("%d\n", n); 29 | } 30 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/2-print_alphabet_x10.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_alphabet_x10 - Prints the alphabet 10 times. 5 | * 6 | * Return: Always 0 7 | */ 8 | 9 | void print_alphabet_x10(void) 10 | { 11 | int i, j; 12 | 13 | for (i = 0; i <= 9; i++) 14 | { 15 | for (j = 97; j <= 122; j++) 16 | { 17 | _putchar(j); 18 | } 19 | _putchar('\n'); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/3-islower.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _islower - Retuns 1 if char is lowercase character 5 | * else, return 0 6 | * 7 | * @c: The character in ASCII code 8 | * 9 | * Return: 1 for lowercase. 0 for the rest. 10 | */ 11 | 12 | int _islower(int c) 13 | { 14 | if (c >= 97 && c <= 122) 15 | { 16 | return (1); 17 | } 18 | else 19 | { 20 | return (0); 21 | } 22 | _putchar('\n'); 23 | } 24 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/4-isalpha.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isalpha - Retuns 1 if char c is a letter, 5 | * and a lowercase or uppercase 6 | * 7 | * @c: The character in ASCII code 8 | * 9 | * Return: 1 for lowercase. 0 for the rest. 10 | */ 11 | 12 | int _isalpha(int c) 13 | { 14 | if ((c >= 97 && c <= 122) || (c >= 65 && c <= 90)) 15 | { 16 | return (1); 17 | } 18 | else 19 | { 20 | return (0); 21 | } 22 | _putchar('\n'); 23 | } 24 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/5-sign.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_sign - Determines if the input number 5 | * is positive, negative or equal to zero. 6 | * 7 | * @n: The input number as an integer. 8 | * 9 | * Return: (1) if n > 0, (0) if n == 0, 10 | * (-1) if n < 0 11 | */ 12 | 13 | int print_sign(int n) 14 | { 15 | if (n > 0) 16 | { 17 | _putchar(43); 18 | return (1); 19 | } 20 | else if (n < 0) 21 | { 22 | _putchar(45); 23 | return (-1); 24 | } 25 | else 26 | { 27 | _putchar(48); 28 | return (0); 29 | } 30 | _putchar('\n'); 31 | } 32 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/6-abs.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _abs - gives the absolute value 5 | * of an integer. 6 | * 7 | * @n: input number as an integer. 8 | * 9 | * Return: the absolute value of int n 10 | */ 11 | 12 | int _abs(int n) 13 | { 14 | if (n < 0) 15 | { 16 | return (n * -1); 17 | } 18 | return (n); 19 | } 20 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/7-print_last_digit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_last_digit - Prints the last digit 5 | * of a number. 6 | * 7 | * @n: input number as an integer. 8 | * 9 | * Return: last digit. 10 | */ 11 | 12 | int print_last_digit(int n) 13 | { 14 | int l; 15 | 16 | l = n % 10; 17 | 18 | if (l < 0) 19 | { 20 | _putchar(-l + 48); 21 | return (-l); 22 | } 23 | else 24 | { 25 | _putchar(l + 48); 26 | return (l); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/8-24_hours.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * jack_bauer - prints every minute of the day 5 | * 6 | * Return: no return 7 | */ 8 | 9 | void jack_bauer(void) 10 | { 11 | int a, b, c, d; 12 | 13 | for (a = 0; a <= 2; a++) 14 | { 15 | for (b = 0; b <= 9; b++) 16 | { 17 | for (c = 0; c <= 5; c++) 18 | { 19 | for (d = 0; d <= 9; d++) 20 | { 21 | if (a >= 2 && b >= 4) 22 | 23 | break; 24 | 25 | _putchar(a + '0'); 26 | _putchar(b + '0'); 27 | _putchar(':'); 28 | _putchar(c + '0'); 29 | _putchar(d + '0'); 30 | _putchar('\n'); 31 | } 32 | } 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /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 | * Returns- void 7 | */ 8 | 9 | void times_table(void) 10 | { 11 | 12 | int i, j, k; 13 | 14 | for (i = 0; i <= 9; i++) 15 | { 16 | for (j = 0; j <= 9; j++) 17 | { 18 | k = j * i; 19 | if (j == 0) 20 | { 21 | _putchar(k + '0'); 22 | } 23 | if (k < 10 && j != 0) 24 | { 25 | _putchar(','); 26 | _putchar(' '); 27 | _putchar(' '); 28 | _putchar(k + '0'); 29 | } 30 | else if (k >= 10) 31 | { 32 | _putchar(','); 33 | _putchar(' '); 34 | _putchar((k / 10) + '0'); 35 | _putchar((k % 10) + '0'); 36 | } 37 | } 38 | _putchar('\n'); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H_ 2 | #define MAIN_H_ 3 | 4 | int _putchar(char c); 5 | void print_alphabet(void); 6 | void print_alphabet_x10(void); 7 | int _islower(int c); 8 | int _isalpha(int c); 9 | int print_sign(int n); 10 | int _abs(int); 11 | int print_last_digit(int); 12 | void jack_bauer(void); 13 | void times_table(void); 14 | void pr_digit(int charcode, int fmt); 15 | int add(int a, int b); 16 | void pr_with_fmt(int charcode, int fmt); 17 | void print_to_98(int n); 18 | void pr_3_digit(int charcode, int fmt); 19 | void print_times_table(int n); 20 | void print_spaces(int n); 21 | void print_zero(void); 22 | 23 | #endif 24 | -------------------------------------------------------------------------------- /0x03-debugging/0-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - tests function that prints if integer is positive or negative 5 | * Return: 0 6 | */ 7 | 8 | int main(void) 9 | { 10 | int i; 11 | 12 | i = 0; 13 | positive_or_negative(i); 14 | 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x03-debugging/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - causes an infinite loop 5 | * Return: 0 6 | */ 7 | 8 | int main(void) 9 | { 10 | int i; 11 | 12 | printf("Infinite loop incoming :(\n"); 13 | 14 | i = 0; 15 | 16 | /* while (i < 10) */ 17 | /* { */ 18 | /* putchar(i); */ 19 | /* } */ 20 | 21 | printf("Infinite loop avoided! \\o/\n"); 22 | 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x03-debugging/2-largest_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * largest_number - returns the largest of 3 numbers 5 | * @a: first integer 6 | * @b: second integer 7 | * @c: third integer 8 | * Return: largest number 9 | */ 10 | 11 | int largest_number(int a, int b, int c) 12 | { 13 | int largest; 14 | 15 | if (a >= b && a >= c) 16 | { 17 | largest = a; 18 | } 19 | else if (b >= a && b >= c) 20 | { 21 | largest = b; 22 | } 23 | else 24 | { 25 | largest = c; 26 | } 27 | 28 | return (largest); 29 | } 30 | -------------------------------------------------------------------------------- /0x03-debugging/3-print_remaining_days.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * print_remaining_days - takes a date and prints how many days are 6 | * left in the year, taking leap years into account 7 | * @month: month in number format 8 | * @day: day of month 9 | * @year: year 10 | * Return: void 11 | */ 12 | 13 | void print_remaining_days(int month, int day, int year) 14 | { 15 | if (year % 4 == 0 && ((year % 400 == 0) || !(year % 100 == 0))) 16 | { 17 | if (month > 2 && day >= 60) 18 | { 19 | day++; 20 | } 21 | 22 | printf("Day of the year: %d\n", day); 23 | printf("Remaining days: %d\n", 366 - day); 24 | } 25 | else 26 | { 27 | if (month == 2 && day == 60) 28 | { 29 | printf("Invalid date: %02d/%02d/%04d\n", month, day - 31, year); 30 | } 31 | else 32 | { 33 | printf("Day of the year: %d\n", day); 34 | printf("Remaining days: %d\n", 365 - day); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /0x03-debugging/README.md: -------------------------------------------------------------------------------- 1 | # 0x03. C - Debugging 2 | -------------------------------------------------------------------------------- /0x03-debugging/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | void positive_or_negative(int i); 5 | int largest_number(int a, int b, int c); 6 | int convert_day(int month, int day); 7 | void print_remaining_days(int month, int day, int year); 8 | 9 | #endif 10 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/0-isupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _isupper - checks for uppercase character 6 | * @c: Variable text 7 | * Return: Always 0. 8 | */ 9 | 10 | int _isupper(int c) 11 | { 12 | if (c >= 'A' && c <= 'Z') 13 | { 14 | return (1); 15 | } 16 | else 17 | { 18 | return (0); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/1-isdigit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isdigit - function that check if it's a digit 5 | * @c: takes in a character/digit 6 | * Return: 1 if digit and 0 if anything else 7 | */ 8 | 9 | int _isdigit(int c) 10 | { 11 | if (c >= '0' && c <= '9') 12 | return (1); 13 | return (0); 14 | } 15 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/10-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | 4 | 5 | /** 6 | 7 | * main - check the code 8 | 9 | * 10 | 11 | * Return: Always 0. 12 | 13 | */ 14 | 15 | int main(void) 16 | 17 | { 18 | 19 | print_triangle(2); 20 | 21 | print_triangle(10); 22 | 23 | print_triangle(1); 24 | 25 | print_triangle(0); 26 | 27 | return (0); 28 | 29 | } 30 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/10-print_triangle.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_triangle - prints a triangle 5 | * @size: takes in a integer for size of triangle 6 | */ 7 | 8 | void print_triangle(int size) 9 | { 10 | int row, column; 11 | 12 | if (size <= 0) 13 | _putchar('\n'); 14 | for (row = 0; row < size; row++) 15 | { 16 | for (column = 0; column < size; column++) 17 | { 18 | if (column + 1 >= size - row) 19 | _putchar('#'); 20 | else 21 | _putchar(' '); 22 | } 23 | _putchar('\n'); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/10-triangles: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x04-more_functions_nested_loops/10-triangles -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/100-prime_factor.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - entry block 5 | * @void: no argument 6 | * Return: 0 7 | */ 8 | 9 | int main(void) 10 | { 11 | long i, number = 612852475143; 12 | 13 | for (i = 2; i <= number; i++) 14 | { 15 | if (number % i == 0) 16 | { 17 | number = number / i; 18 | i--; 19 | } 20 | } 21 | 22 | printf("%lu\n", i); 23 | 24 | return (0); 25 | } 26 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/101-print_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_number - Prints an integer. 5 | * @n: integer to be printed. 6 | */ 7 | 8 | void print_number(int n) 9 | { 10 | unsigned int num = n; 11 | 12 | if (n < 0) 13 | { 14 | _putchar('-'); 15 | num = -num; 16 | } 17 | 18 | if ((num / 10) > 0) 19 | print_number(num / 10); 20 | 21 | _putchar((num % 10) + '0'); 22 | } 23 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/2-mul.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * mul - function that multiplies two integers 6 | * @a: First integer 7 | * @b: Second integer 8 | * Return: mul. 9 | */ 10 | 11 | int mul(int a, int b) 12 | { 13 | return (a * b); 14 | } 15 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/3-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_numbers - function that prints the numbers, from 0 to 9. 5 | */ 6 | 7 | void print_numbers(void) 8 | { 9 | char c; 10 | 11 | for (c = '0'; c <= '9'; c++) 12 | _putchar(c); 13 | 14 | _putchar('\n'); 15 | } 16 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/4-print_most_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_most_numbers - prints the numbers except 2 and 4. 5 | */ 6 | 7 | void print_most_numbers(void) 8 | { 9 | char c; 10 | 11 | for (c = '0'; c <= '9'; c++) 12 | { 13 | if (c != '2' && c != '4') 14 | _putchar(c); 15 | } 16 | 17 | _putchar('\n'); 18 | } 19 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/5-more_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * more_numbers - prints 10 times the numbers, from 0 to 14. 5 | */ 6 | 7 | void more_numbers(void) 8 | { 9 | int i, j; 10 | 11 | for (i = 0; i < 10; i++) 12 | { 13 | for (j = 0; j <= 14; j++) 14 | { 15 | if (j > 9) 16 | _putchar((j / 10) + '0'); 17 | _putchar((j % 10) + '0'); 18 | } 19 | 20 | _putchar('\n'); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/6-print_line.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_line - draws a straight line in the terminal 5 | * @n: number of times the character _ should be printed 6 | */ 7 | 8 | void print_line(int n) 9 | { 10 | if (n <= 0) 11 | { 12 | _putchar('\n'); 13 | } 14 | else 15 | { 16 | int i; 17 | 18 | for (i = 0; i < n; i++) 19 | { 20 | _putchar('_'); 21 | } 22 | 23 | _putchar('\n'); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/7-print_diagonal.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_diagonal - draws a diagonal line 5 | * @n: takes in an integer 6 | */ 7 | 8 | void print_diagonal(int n) 9 | { 10 | int i, j; 11 | 12 | if (n <= 0) 13 | _putchar('\n'); 14 | 15 | for (i = 0; i < n; i++) 16 | { 17 | for (j = 0; j <= i; j++) 18 | { 19 | if (i == j) 20 | { 21 | _putchar('\\'); 22 | _putchar('\n'); 23 | } 24 | else 25 | _putchar(' '); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/8-print_square.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_square - function to print a square 5 | * @size: takes in the size of the square 6 | */ 7 | 8 | void print_square(int size) 9 | { 10 | int i, j; 11 | 12 | if (size <= 0) 13 | _putchar('\n'); 14 | 15 | for (i = 0; i < size; i++) 16 | { 17 | for (j = 0; j < size; j++) 18 | _putchar('#'); 19 | _putchar('\n'); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/9-fizz_buzz.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - entry block 5 | * @void: no argument 6 | * Return: 0 Success 7 | */ 8 | 9 | int main(void) 10 | { 11 | int i; 12 | 13 | for (i = 1; i <= 100; i++) 14 | { 15 | if (i % 15 == 0) 16 | { 17 | printf("FizzBuzz "); 18 | } 19 | else if (i % 5 == 0) 20 | { 21 | if (i == 100) 22 | printf("Buzz"); 23 | else 24 | printf("Buzz "); 25 | } 26 | else if (i % 3 == 0) 27 | { 28 | printf("Fizz "); 29 | } 30 | else 31 | { 32 | printf("%d ", i); 33 | } 34 | } 35 | 36 | printf("\n"); 37 | 38 | return (0); 39 | } 40 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | 5 | /** 6 | 7 | * _putchar - writes the character c to stdout 8 | 9 | * @c: The character to print 10 | 11 | * 12 | 13 | * Return: On success 1. 14 | 15 | * On error, -1 is returned, and errno is set appropriately. 16 | 17 | */ 18 | 19 | int _putchar(char c) 20 | 21 | { 22 | 23 | return (write(1, &c, 1)); 24 | 25 | } 26 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/main.h: -------------------------------------------------------------------------------- 1 | #ifndef _MAIN_H_ 2 | #define _MAIN_H_ 3 | 4 | int _isupper(int c); 5 | int _isdigit(int c); 6 | int mul(int a, int b); 7 | void print_numbers(void); 8 | int _putchar(char c); 9 | void print_most_numbers(void); 10 | void more_numbers(void); 11 | void print_line(int n); 12 | void print_diagonal(int n); 13 | void print_square(int size); 14 | void print_triangle(int size); 15 | void print_number(int n); 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/.6-puts2.c.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x05-pointers_arrays_strings/.6-puts2.c.swp -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/.README.md.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x05-pointers_arrays_strings/.README.md.swp -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/0-reset_to_98.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * reset_to_98 - takes a pointer to a parameter and updates its value. 5 | * @n: input integer. 6 | * Return: no return. 7 | */ 8 | 9 | void reset_to_98(int *n) 10 | { 11 | *n = 98; 12 | } 13 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/1-swap.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * swap_int - Swaps the values of two integers. 5 | * @a: first integer. 6 | * @b: second integer. 7 | */ 8 | 9 | void swap_int(int *a, int *b) 10 | { 11 | int tmp = *a; 12 | *a = *b; 13 | *b = tmp; 14 | } 15 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/100-atoi.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _atoi - converts a string to an integer 5 | * @s: string to be converted 6 | * 7 | * Return: the int converted from the string 8 | */ 9 | 10 | int _atoi(char *s) 11 | { 12 | int i, d, n, len, f, digit; 13 | 14 | i = 0; 15 | d = 0; 16 | n = 0; 17 | len = 0; 18 | f = 0; 19 | digit = 0; 20 | 21 | while (s[len] != '\0') 22 | len++; 23 | 24 | while (i < len && f == 0) 25 | { 26 | if (s[i] == '-') 27 | ++d; 28 | 29 | if (s[i] >= '0' && s[i] <= '9') 30 | { 31 | digit = s[i] - '0'; 32 | 33 | if (d % 2) 34 | digit = -digit; 35 | n = n * 10 + digit; 36 | f = 1; 37 | 38 | if (s[i + 1] < '0' || s[i + 1] > '9') 39 | break; 40 | f = 0; 41 | } 42 | i++; 43 | } 44 | 45 | if (f == 0) 46 | return (0); 47 | 48 | return (n); 49 | } 50 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/101-keygen.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /** 6 | * main - program that generates random valid 7 | * passwords for the program 101-crackme 8 | * 9 | * Return: Always 0 (Success) 10 | */ 11 | 12 | int main(void) 13 | { 14 | int pass[100]; 15 | int i, sum, n; 16 | 17 | sum = 0; 18 | 19 | srand(time(NULL)); 20 | 21 | for (i = 0; i < 100; i++) 22 | { 23 | pass[i] = rand() % 78; 24 | sum += (pass[i] + '0'); 25 | putchar(pass[i] + '0'); 26 | if ((2772 - sum) - '0' < 78) 27 | { 28 | n = 2772 - sum - '0'; 29 | sum += n; 30 | putchar(n + '0'); 31 | break; 32 | } 33 | } 34 | 35 | return (0); 36 | } 37 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/2-strlen.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strlen - Returns the length of a string. 5 | * @s: The string to get the length of. 6 | * 7 | * Return: The length of @s. 8 | */ 9 | 10 | int _strlen(char *s) 11 | { 12 | int lenStr = 0; 13 | 14 | while (*s++) 15 | lenStr++; 16 | 17 | return (lenStr); 18 | } 19 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/3-puts.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _puts - prints a string to stdout 5 | * @str: pointer to the string to print 6 | */ 7 | 8 | void _puts(char *str) 9 | { 10 | int i; 11 | 12 | for (i = 0; str[i] != '\0'; i++) 13 | { 14 | _putchar(str[i]); 15 | } 16 | _putchar('\n'); 17 | } 18 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/4-print_rev.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_rev - prints a string, in reverse, followed by a new line. 5 | * @s: input string. 6 | * Return: no return. 7 | */ 8 | 9 | void print_rev(char *s) 10 | { 11 | int count = 0; 12 | 13 | while (count >= 0) 14 | { 15 | if (s[count] == '\0') 16 | break; 17 | count++; 18 | } 19 | 20 | for (count--; count >= 0; count--) 21 | _putchar(s[count]); 22 | _putchar('\n'); 23 | } 24 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/5-rev_string.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | *rev_string - update value. 6 | *@s: value to be evaluate. 7 | *Return: not. 8 | */ 9 | 10 | void rev_string(char *s) 11 | { 12 | char tmp; 13 | int i, len, len1; 14 | 15 | len = 0; 16 | len1 = 0; 17 | 18 | while (s[len] != '\0') 19 | { 20 | len++; 21 | } 22 | 23 | len1 = len - 1; 24 | 25 | for (i = 0; i < len / 2; i++) 26 | { 27 | tmp = s[i]; 28 | s[i] = s[len1]; 29 | s[len1--] = tmp; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/6-puts2.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * puts2 - prints one char out of 2 of a string 5 | * followed by a new line 6 | * @str: string to print the chars from 7 | */ 8 | 9 | void puts2(char *str) 10 | { 11 | int len, i; 12 | 13 | len = 0; 14 | 15 | while (str[len] != '\0') 16 | { 17 | len++; 18 | } 19 | 20 | for (i = 0; i < len; i += 2) 21 | { 22 | _putchar(str[i]); 23 | } 24 | _putchar('\n'); 25 | } 26 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/7-puts_half.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * puts_half - prints half of a string. 5 | * @str: input string. 6 | * Return: no return. 7 | */ 8 | 9 | void puts_half(char *str) 10 | { 11 | int count = 0, i; 12 | 13 | while (count >= 0) 14 | { 15 | if (str[count] == '\0') 16 | break; 17 | count++; 18 | } 19 | 20 | if (count % 2 == 1) 21 | i = count / 2; 22 | else 23 | i = (count - 1) / 2; 24 | 25 | for (i++; i < count; i++) 26 | _putchar(str[i]); 27 | _putchar('\n'); 28 | } 29 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/8-print_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * print_array - prints n elements of an array of integers. 6 | * @a: input array. 7 | * @n: input n elements 8 | * Return: no return. 9 | */ 10 | 11 | void print_array(int *a, int n) 12 | { 13 | int i; 14 | 15 | for (i = 0; i < n; i++) 16 | { 17 | printf("%d", *(a + i)); 18 | 19 | if (i != (n - 1)) 20 | printf(", "); 21 | } 22 | printf("\n"); 23 | } 24 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/9-strcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * *_strcpy - copies the string pointed to by src 5 | * including the terminating null byte (\0) 6 | * to the buffer pointed to by dest 7 | * @dest: pointer to the buffer in which we copy the string 8 | * @src: string to be copied 9 | * 10 | * Return: the pointer to dest 11 | */ 12 | 13 | char *_strcpy(char *dest, char *src) 14 | { 15 | int len, i; 16 | 17 | len = 0; 18 | 19 | while (src[len] != '\0') 20 | { 21 | len++; 22 | } 23 | 24 | for (i = 0; i < len; i++) 25 | { 26 | dest[i] = src[i]; 27 | } 28 | 29 | dest[i] = '\0'; 30 | 31 | return (dest); 32 | } 33 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | #ifndef _MAIN_H_ 2 | #define _MAIN_H_ 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 | int _putchar(char); 9 | void print_rev(char *s); 10 | void rev_string(char *s); 11 | void puts2(char *str); 12 | void puts_half(char *str); 13 | void print_array(int *a, int n); 14 | char *_strcpy(char *dest, char *src); 15 | int _atoi(char *s); 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/0-strcat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strcat - concatenates two strings, 4 | * @dest: destination. 5 | * @src: source. 6 | * Return: the pointer to dest. 7 | */ 8 | char *_strcat(char *dest, char *src) 9 | { 10 | int count = 0, count2 = 0; 11 | 12 | while (*(dest + count) != '\0') 13 | { 14 | count++; 15 | } 16 | 17 | while (count2 >= 0) 18 | { 19 | *(dest + count) = *(src + count2); 20 | if (*(src + count2) == '\0') 21 | break; 22 | count++; 23 | count2++; 24 | } 25 | return (dest); 26 | } 27 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/1-strncat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strncat - concatenates n bytes from a string to another 5 | * @dest: destination string 6 | * @src: source string 7 | * @n: number of bytes of str to concatenate 8 | * 9 | * Return: a pointer to the resulting string dest 10 | */ 11 | char *_strncat(char *dest, char *src, int n) 12 | { 13 | int i, j; 14 | 15 | i = 0; 16 | j = 0; 17 | 18 | while (dest[i] != '\0') 19 | i++; 20 | 21 | while (src[j] != '\0' && j < n) 22 | { 23 | dest[i] = src[j]; 24 | i++; 25 | j++; 26 | } 27 | 28 | dest[i] = '\0'; 29 | 30 | return (dest); 31 | } 32 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/100-rot13.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * rot13 - encodes a string in rot13 5 | * @s: string to be encoded 6 | * 7 | * Return: the resulting string 8 | */ 9 | char *rot13(char *s) 10 | { 11 | int i, j; 12 | 13 | char a[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; 14 | char b[] = "NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm"; 15 | 16 | for (i = 0; s[i] != '\0'; i++) 17 | { 18 | for (j = 0; a[j] != '\0'; j++) 19 | { 20 | if (s[i] == a[j]) 21 | { 22 | s[i] = b[j]; 23 | break; 24 | } 25 | } 26 | } 27 | 28 | return (s); 29 | } 30 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/101-print_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_number - prints an integer. 5 | * @n: input integer. 6 | * Return: no return. 7 | */ 8 | void print_number(int n) 9 | { 10 | unsigned int m, d, count; 11 | 12 | if (n < 0) 13 | { 14 | _putchar(45); 15 | m = n * -1; 16 | } 17 | else 18 | { 19 | m = n; 20 | } 21 | 22 | d = m; 23 | count = 1; 24 | 25 | while (d > 9) 26 | { 27 | d /= 10; 28 | count *= 10; 29 | } 30 | 31 | for (; count >= 1; count /= 10) 32 | { 33 | _putchar(((m / count) % 10) + 48); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/2-strncpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strncpy - copy a string 5 | * @dest: input value 6 | * @src: input value 7 | * @n: input value 8 | * 9 | * Return: dest 10 | */ 11 | char *_strncpy(char *dest, char *src, int n) 12 | { 13 | int j; 14 | 15 | j = 0; 16 | while (j < n && src[j] != '\0') 17 | { 18 | dest[j] = src[j]; 19 | j++; 20 | } 21 | while (j < n) 22 | { 23 | dest[j] = '\0'; 24 | j++; 25 | } 26 | 27 | return (dest); 28 | } 29 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/3-strcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strcmp - compare string values 5 | * @s1: input value 6 | * @s2: input value 7 | * 8 | * Return: s1[i] - s2[i] 9 | */ 10 | int _strcmp(char *s1, char *s2) 11 | { 12 | int i; 13 | 14 | i = 0; 15 | while (s1[i] != '\0' && s2[i] != '\0') 16 | { 17 | if (s1[i] != s2[i]) 18 | { 19 | return (s1[i] - s2[i]); 20 | } 21 | i++; 22 | } 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/4-rev_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * reverse_array - reverses an array of integers 5 | * @a: array to be reversed 6 | * @n: number of elements in the array 7 | */ 8 | void reverse_array(int *a, int n) 9 | { 10 | int i, j, tmp; 11 | 12 | j = n - 1; 13 | 14 | for (i = 0; i < n / 2; i++) 15 | { 16 | tmp = a[i]; 17 | a[i] = a[j]; 18 | a[j--] = tmp; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/5-string_toupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * string_toupper - change all lowercase to uppercase 5 | * @n: pointer 6 | * 7 | * Return: n 8 | */ 9 | 10 | char *string_toupper(char *n) 11 | { 12 | int i; 13 | 14 | i = 0; 15 | while (n[i] != '\0') 16 | { 17 | if (n[i] >= 'a' && n[i] <= 'z') 18 | n[i] = n[i] - 32; 19 | i++; 20 | } 21 | return (n); 22 | } 23 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/6-cap_string.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * cap_string - capitalizes everey word of a string 5 | * @s: string to modify 6 | * 7 | * Return: the resulting string 8 | */ 9 | char *cap_string(char *s) 10 | { 11 | int i, j; 12 | 13 | char spe[13] = {' ', '\t', '\n', ',', ';', '.', 14 | '!', '?', '"', '(', ')', '{', '}'}; 15 | 16 | for (i = 0; s[i] != '\0'; i++) 17 | { 18 | if (i == 0 && s[i] >= 'a' && s[i] <= 'z') 19 | s[i] -= 32; 20 | 21 | for (j = 0; j < 13; j++) 22 | { 23 | if (s[i] == spe[j]) 24 | { 25 | if (s[i + 1] >= 'a' && s[i + 1] <= 'z') 26 | { 27 | s[i + 1] -= 32; 28 | } 29 | } 30 | } 31 | } 32 | 33 | return (s); 34 | } 35 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/7-leet.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * leet - encode into 1337speak 4 | * @n: input value 5 | * 6 | * Return: n value 7 | */ 8 | char *leet(char *n) 9 | { 10 | int i, j; 11 | char s1[] = "aAeEoOtTlL"; 12 | char s2[] = "4433007711"; 13 | 14 | for (i = 0; n[i] != '\0'; i++) 15 | { 16 | for (j = 0; j < 10; j++) 17 | { 18 | if (n[i] == s1[j]) 19 | { 20 | n[i] = s2[j]; 21 | } 22 | } 23 | } 24 | return (n); 25 | } 26 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | #include 5 | 6 | int _putchar(char c); 7 | void *_memset(void *s, int c, size_t n); 8 | char *_memcpy(char *dest, char *src, unsigned int n); 9 | char *_strchr(char *s, char c); 10 | unsigned int _strspn(char *s, char *accept); 11 | char *_strpbrk(char *s, char *accept); 12 | char *_strstr(char *haystack, char *needle); 13 | void print_chessboard(char (*a)[8]); 14 | void print_diagsums(int *a, int size); 15 | void set_string(char **s, char *to); 16 | void print_number(int n); 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/0-memset.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _memset - fills memory with a constant byte, 4 | * @s: memory area. 5 | * @b: constant byte. 6 | * @n: bytes filled. 7 | * Return: the pointer to dest. 8 | * 9 | * PSEUDOCODE 10 | * Loop through "s" using "n" as limit 11 | * replace s[i] with "b" 12 | * return s 13 | */ 14 | char *_memset(char *s, char b, unsigned int n) 15 | { 16 | unsigned int i; 17 | 18 | for (i = 0; i < n; i++) 19 | *(s + i) = b; 20 | 21 | return (s); 22 | } 23 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/1-memcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _memcpy - copies memory area, 4 | * @dest: destination memory area. 5 | * @src: source memory area. 6 | * @n: bytes filled. 7 | * Return: the pointer to dest. 8 | * 9 | * PSEUDOCODE 10 | * Looping through the length of bytes n 11 | * dest[i] = src[i] 12 | */ 13 | char *_memcpy(char *dest, char *src, unsigned int n) 14 | { 15 | unsigned int i; 16 | 17 | for (i = 0; i < n; i++) 18 | *(dest + i) = *(src + i); 19 | 20 | return (dest); 21 | } 22 | 23 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/100-set_string.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * set_string - sets the value of a pointer to a char 5 | * @s: source adress. 6 | * @to: target adress. 7 | * Return: no return. 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-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char *s = "hello"; 12 | char *f; 13 | 14 | f = _strchr(s, 'l'); 15 | 16 | if (f != NULL) 17 | { 18 | printf("%s\n", f); 19 | } 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/2-strchr: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x07-pointers_arrays_strings/2-strchr -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/2-strchr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strchr - locates a character in a string 4 | * @s: string. 5 | * @c: character. 6 | * Return: the pointer to the first occurance of the character c 7 | */ 8 | char *_strchr(char *s, char c) 9 | { 10 | unsigned int i = 0; 11 | 12 | for (; *(s + i) != '\0'; i++) 13 | if (*(s + i) == c) 14 | return (s + i); 15 | if (*(s + i) == c) 16 | return (s + i); 17 | return ('\0'); 18 | } 19 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/3-strspn.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strspn - Gets the length of a prefix substring. 4 | * @s: The string to be searched. 5 | * @accept: The prefix to be measured. 6 | * 7 | * Return: The number of bytes in s which 8 | * consist only of bytes from accept. 9 | * 10 | * PSEUDOCODE 11 | * Loop through s and compare s[i] to values in accept 12 | * increase bytes counter when values in accept can be found 13 | * *in s 14 | * end loop and return bytes when accept is exhaused 15 | */ 16 | unsigned int _strspn(char *s, char *accept) 17 | { 18 | unsigned int bytes = 0; 19 | int index; 20 | 21 | while (*s) 22 | { 23 | for (index = 0; accept[index]; index++) 24 | { 25 | if (*s == accept[index]) 26 | { 27 | bytes++; 28 | break; 29 | } 30 | 31 | else if (accept[index + 1] == '\0') 32 | return (bytes); 33 | } 34 | 35 | s++; 36 | } 37 | 38 | return (bytes); 39 | } 40 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/4-strpbrk.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strpbrk - searches a string for any of a set of bytes. 5 | * @s: first string. 6 | * @accept: second string. 7 | * Return: a pointer to the byte in s that matches one of the 8 | * bytes in accept, or NULL if no such byte is found. 9 | * 10 | */ 11 | char *_strpbrk(char *s, char *accept) 12 | { 13 | unsigned int i, j; 14 | 15 | for (i = 0; *(s + i) != '\0'; i++) 16 | { 17 | for (j = 0; *(accept + j) != '\0'; j++) 18 | { 19 | if (*(s + i) == *(accept + j)) 20 | return (s + i); 21 | } 22 | } 23 | return ('\0'); 24 | } 25 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/5-strstr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strstr - a function that locates a substring 4 | * @haystack: locate a substring 5 | * @needle: substring to locate 6 | * 7 | * Return: pointer to the beginning of the located substring, 8 | * or NULL, if substring is not found 9 | */ 10 | char *_strstr(char *haystack, char *needle) 11 | { 12 | char *h = haystack; 13 | char *n = needle; 14 | 15 | while (*h) 16 | { 17 | n = needle; 18 | h = haystack; 19 | while (*n) 20 | { 21 | if (*h == *n) 22 | { 23 | n++; 24 | h++; 25 | } 26 | else 27 | break; 28 | } 29 | if (*n == '\0') 30 | return (haystack); 31 | haystack++; 32 | } 33 | return (0); 34 | } 35 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/7-print_chessboard.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * print_chessboard - a function that prints the chessboard 4 | * @a: 2d array of chars 5 | * 6 | * Return: nothing 7 | */ 8 | void print_chessboard(char (*a)[8]) 9 | { 10 | int i, j; 11 | 12 | for (i = 0; i < 8; i++) 13 | { 14 | for (j = 0; j < 8; j++) 15 | { 16 | _putchar(a[i][j]); 17 | } 18 | 19 | _putchar('\n'); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/8-print_diagsums.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * print_diagsums - a function that prints the sum of the two diagonals 4 | * of a square matrix of integers 5 | * @a: 2D array 6 | * @size: size x size of the square matrix 7 | * 8 | * Return: nothing 9 | */ 10 | void print_diagsums(int *a, int size) 11 | { 12 | int i; 13 | int tl = 0; 14 | int tr = 0; 15 | int s = size * size; 16 | 17 | for (i = 0; i < s; i += size + 1) 18 | { 19 | tl += a[i]; 20 | } 21 | 22 | for (i = size - 1; i < s - 1; i += size - 1) 23 | { 24 | tr += a[i]; 25 | } 26 | 27 | printf("%d, %d\n", tl, tr); 28 | } 29 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | #include 5 | 6 | char *_memset(char *s, char b, unsigned int n); 7 | char *_memcpy(char *dest, char *src, unsigned int n); 8 | char *_strchr(char *s, char c); 9 | unsigned int _strspn(char *s, char *accept); 10 | char *_strpbrk(char *s, char *accept); 11 | char *_strstr(char *haystack, char *needle); 12 | int _putchar(char); 13 | void print_chessboard(char (*a)[8]); 14 | void print_diagsums(int *a, int size); 15 | void set_string(char **s, char *to); 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/temp: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * _strchr - locates a character in a string 5 | * @s: the string 6 | * @c: the character 7 | * 8 | * Return: a pointer to a character aka the character found 9 | * 10 | * PSEUDOCODE 11 | * Loop through the string s 12 | * if s[i] == c 13 | * return &s[i] 14 | * else return NULL 15 | */ 16 | char *_strchr(char *s, char c) 17 | { 18 | unsigned int i; 19 | 20 | for (i = 0; s[i] != '\0' ; i++) 21 | { 22 | if (s[i] == c) 23 | return (&s[i]); 24 | } 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /0x08-recursion/0-puts_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _puts_recursion - prints a string, followed by a new line 5 | * @s: string to print 6 | */ 7 | void _puts_recursion(char *s) 8 | { 9 | if (*s == '\0') 10 | { 11 | _putchar('\n'); 12 | return; 13 | } 14 | _putchar(*s); 15 | _puts_recursion(s + 1); 16 | } 17 | -------------------------------------------------------------------------------- /0x08-recursion/1-print_rev_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _print_rev_recursion - prints a string, in reverse. 4 | * @s: pointer to string 5 | * Return: nothing 6 | **/ 7 | void _print_rev_recursion(char *s) 8 | { 9 | if (*s != '\0') 10 | { 11 | _print_rev_recursion(s + 1); 12 | _putchar(*s); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /0x08-recursion/101-wildcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * wildcmp - compares two string. 5 | * @s1: 1st string to be compared 6 | * @s2: second string containing '*' or not 7 | * Return: 1 if the strings can be considered as identical, and 0 8 | **/ 9 | int wildcmp(char *s1, char *s2) 10 | { 11 | if (*s2 == '*' && *(s2 + 1) != '\0' && *s1 == '\0') 12 | return (0); 13 | if (*s1 == '\0' && *s2 == '\0') 14 | return (1); 15 | if (*s1 == *s2) 16 | return (wildcmp(s1 + 1, s2 + 1)); 17 | if (*s2 == '*') 18 | return (wildcmp(s1, s2 + 1) || wildcmp(s1 + 1, s2)); 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /0x08-recursion/2-strlen_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strlen_recursion - returns the length of a string. 5 | * @s: string 6 | * Return: the length of a string. 7 | */ 8 | int _strlen_recursion(char *s) 9 | { 10 | if (*s == '\0') 11 | return (0); 12 | else 13 | return (1 + _strlen_recursion(s + 1)); 14 | } 15 | -------------------------------------------------------------------------------- /0x08-recursion/3-factorial.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * factorial - calculates the factorial of a given number. 4 | * @n: given number 5 | * Return: factorial 6 | **/ 7 | int factorial(int n) 8 | { 9 | if (n < 0) 10 | { 11 | return (-1); 12 | } 13 | if (n == 0 || n == 1) 14 | { 15 | return (1); 16 | } 17 | return (n * factorial(n - 1)); 18 | } 19 | -------------------------------------------------------------------------------- /0x08-recursion/4-pow_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * _pow_recursion - calculates value of x raised to the power of y 5 | * @x: given number 6 | * @y: exponent 7 | * Return: value of x raised to the power of y 8 | **/ 9 | int _pow_recursion(int x, int y) 10 | { 11 | if (y < 0) 12 | { 13 | return (-1); 14 | } 15 | if (y == 0) 16 | { 17 | return (1); 18 | } 19 | return (x * _pow_recursion(x, y - 1)); 20 | } 21 | -------------------------------------------------------------------------------- /0x08-recursion/6-is_prime_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * is_prime - detects if an input number is a prime number. 5 | * @n: input number. 6 | * @c: iterator. 7 | * Return: 1 if n is a prime number. 0 if n is not a prime number. 8 | */ 9 | int is_prime(unsigned int n, unsigned int c) 10 | { 11 | if (n % c == 0) 12 | { 13 | if (n == c) 14 | return (1); 15 | else 16 | return (0); 17 | } 18 | return (0 + is_prime(n, c + 1)); 19 | } 20 | /** 21 | * is_prime_number - detects if an input number is a prime number. 22 | * @n: input number. 23 | * Return: 1 if n is a prime number. 0 if n is not a prime number. 24 | */ 25 | int is_prime_number(int n) 26 | { 27 | if (n == 0) 28 | return (0); 29 | if (n < 0) 30 | return (0); 31 | if (n == 1) 32 | return (0); 33 | return (is_prime(n, 2)); 34 | } 35 | -------------------------------------------------------------------------------- /0x08-recursion/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | int _putchar(char c); 5 | void _puts_recursion(char *s); 6 | void _print_rev_recursion(char *s); 7 | int _strlen_recursion(char *s); 8 | int factorial(int n); 9 | int _pow_recursion(int x, int y); 10 | int _sqrt_recursion(int n); 11 | int is_prime_number(int n); 12 | int is_palindrome(char *s); 13 | int wildcmp(char *s1, char *s2); 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /0x09-static_libraries/README.md: -------------------------------------------------------------------------------- 1 | # 0x09.C - Static libraries 2 | 3 | Project involves working with static libraries in C 4 | 5 | ## Technologies used 6 | * C files are written according to the C90 standard 7 | * Tested on Ubuntu 20.04 LTS 8 | 9 | ## Files 10 | All of the following files are scripts and programs written in C: 11 | 12 | | Filename | Description | 13 | | -------- | ----------- | 14 | | [libmy.a](./libmy.a) | Static library that contains a lot of functions | 15 | | [create_static_lib.sh](./create_static_lib.sh) | Script that creates a static library called `liball.a` from all the `.c` files that are in the current directory | 16 | | [main.h](./main.h) | Header file that contains all the prototypes of the used functions | 17 | -------------------------------------------------------------------------------- /0x09-static_libraries/c-files/0-isupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _isupper - checks for uppercase character 6 | * @c: Variable text 7 | * Return: Always 0. 8 | */ 9 | 10 | int _isupper(int c) 11 | { 12 | if (c >= 'A' && c <= 'Z') 13 | { 14 | return (1); 15 | } 16 | else 17 | { 18 | return (0); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /0x09-static_libraries/c-files/0-memset.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _memset - fills memory with a constant byte, 4 | * @s: memory area. 5 | * @b: constant byte. 6 | * @n: bytes filled. 7 | * Return: the pointer to dest. 8 | * 9 | * PSEUDOCODE 10 | * Loop through "s" using "n" as limit 11 | * replace s[i] with "b" 12 | * return s 13 | */ 14 | char *_memset(char *s, char b, unsigned int n) 15 | { 16 | unsigned int i; 17 | 18 | for (i = 0; i < n; i++) 19 | *(s + i) = b; 20 | 21 | return (s); 22 | } 23 | -------------------------------------------------------------------------------- /0x09-static_libraries/c-files/0-strcat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strcat - concatenates two strings, 4 | * @dest: destination. 5 | * @src: source. 6 | * Return: the pointer to dest. 7 | */ 8 | char *_strcat(char *dest, char *src) 9 | { 10 | int count = 0, count2 = 0; 11 | 12 | while (*(dest + count) != '\0') 13 | { 14 | count++; 15 | } 16 | 17 | while (count2 >= 0) 18 | { 19 | *(dest + count) = *(src + count2); 20 | if (*(src + count2) == '\0') 21 | break; 22 | count++; 23 | count2++; 24 | } 25 | return (dest); 26 | } 27 | -------------------------------------------------------------------------------- /0x09-static_libraries/c-files/1-isdigit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isdigit - function that check if it's a digit 5 | * @c: takes in a character/digit 6 | * Return: 1 if digit and 0 if anything else 7 | */ 8 | 9 | int _isdigit(int c) 10 | { 11 | if (c >= '0' && c <= '9') 12 | return (1); 13 | return (0); 14 | } 15 | -------------------------------------------------------------------------------- /0x09-static_libraries/c-files/1-memcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _memcpy - copies memory area, 4 | * @dest: destination memory area. 5 | * @src: source memory area. 6 | * @n: bytes filled. 7 | * Return: the pointer to dest. 8 | * 9 | * PSEUDOCODE 10 | * Looping through the length of bytes n 11 | * dest[i] = src[i] 12 | */ 13 | char *_memcpy(char *dest, char *src, unsigned int n) 14 | { 15 | unsigned int i; 16 | 17 | for (i = 0; i < n; i++) 18 | *(dest + i) = *(src + i); 19 | 20 | return (dest); 21 | } 22 | 23 | -------------------------------------------------------------------------------- /0x09-static_libraries/c-files/1-strncat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strncat - concatenates n bytes from a string to another 5 | * @dest: destination string 6 | * @src: source string 7 | * @n: number of bytes of str to concatenate 8 | * 9 | * Return: a pointer to the resulting string dest 10 | */ 11 | char *_strncat(char *dest, char *src, int n) 12 | { 13 | int i, j; 14 | 15 | i = 0; 16 | j = 0; 17 | 18 | while (dest[i] != '\0') 19 | i++; 20 | 21 | while (src[j] != '\0' && j < n) 22 | { 23 | dest[i] = src[j]; 24 | i++; 25 | j++; 26 | } 27 | 28 | dest[i] = '\0'; 29 | 30 | return (dest); 31 | } 32 | -------------------------------------------------------------------------------- /0x09-static_libraries/c-files/100-atoi.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _atoi - converts a string to an integer 5 | * @s: string to be converted 6 | * 7 | * Return: the int converted from the string 8 | */ 9 | 10 | int _atoi(char *s) 11 | { 12 | int i, d, n, len, f, digit; 13 | 14 | i = 0; 15 | d = 0; 16 | n = 0; 17 | len = 0; 18 | f = 0; 19 | digit = 0; 20 | 21 | while (s[len] != '\0') 22 | len++; 23 | 24 | while (i < len && f == 0) 25 | { 26 | if (s[i] == '-') 27 | ++d; 28 | 29 | if (s[i] >= '0' && s[i] <= '9') 30 | { 31 | digit = s[i] - '0'; 32 | 33 | if (d % 2) 34 | digit = -digit; 35 | n = n * 10 + digit; 36 | f = 1; 37 | 38 | if (s[i + 1] < '0' || s[i + 1] > '9') 39 | break; 40 | f = 0; 41 | } 42 | i++; 43 | } 44 | 45 | if (f == 0) 46 | return (0); 47 | 48 | return (n); 49 | } 50 | -------------------------------------------------------------------------------- /0x09-static_libraries/c-files/2-strchr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strchr - locates a character in a string 4 | * @s: string. 5 | * @c: character. 6 | * Return: the pointer to the first occurance of the character c 7 | */ 8 | char *_strchr(char *s, char c) 9 | { 10 | unsigned int i = 0; 11 | 12 | for (; *(s + i) != '\0'; i++) 13 | if (*(s + i) == c) 14 | return (s + i); 15 | if (*(s + i) == c) 16 | return (s + i); 17 | return ('\0'); 18 | } 19 | -------------------------------------------------------------------------------- /0x09-static_libraries/c-files/2-strlen.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strlen - Returns the length of a string. 5 | * @s: The string to get the length of. 6 | * 7 | * Return: The length of @s. 8 | */ 9 | 10 | int _strlen(char *s) 11 | { 12 | int lenStr = 0; 13 | 14 | while (*s++) 15 | lenStr++; 16 | 17 | return (lenStr); 18 | } 19 | -------------------------------------------------------------------------------- /0x09-static_libraries/c-files/2-strncpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strncpy - copy a string 5 | * @dest: input value 6 | * @src: input value 7 | * @n: input value 8 | * 9 | * Return: dest 10 | */ 11 | char *_strncpy(char *dest, char *src, int n) 12 | { 13 | int j; 14 | 15 | j = 0; 16 | while (j < n && src[j] != '\0') 17 | { 18 | dest[j] = src[j]; 19 | j++; 20 | } 21 | while (j < n) 22 | { 23 | dest[j] = '\0'; 24 | j++; 25 | } 26 | 27 | return (dest); 28 | } 29 | -------------------------------------------------------------------------------- /0x09-static_libraries/c-files/3-islower.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _islower - Retuns 1 if char is lowercase character 5 | * else, return 0 6 | * 7 | * @c: The character in ASCII code 8 | * 9 | * Return: 1 for lowercase. 0 for the rest. 10 | */ 11 | 12 | int _islower(int c) 13 | { 14 | if (c >= 97 && c <= 122) 15 | { 16 | return (1); 17 | } 18 | else 19 | { 20 | return (0); 21 | } 22 | _putchar('\n'); 23 | } 24 | -------------------------------------------------------------------------------- /0x09-static_libraries/c-files/3-puts.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _puts - prints a string to stdout 5 | * @str: pointer to the string to print 6 | */ 7 | 8 | void _puts(char *str) 9 | { 10 | int i; 11 | 12 | for (i = 0; str[i] != '\0'; i++) 13 | { 14 | _putchar(str[i]); 15 | } 16 | _putchar('\n'); 17 | } 18 | -------------------------------------------------------------------------------- /0x09-static_libraries/c-files/3-strcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strcmp - compare string values 5 | * @s1: input value 6 | * @s2: input value 7 | * 8 | * Return: s1[i] - s2[i] 9 | */ 10 | int _strcmp(char *s1, char *s2) 11 | { 12 | int i; 13 | 14 | i = 0; 15 | while (s1[i] != '\0' && s2[i] != '\0') 16 | { 17 | if (s1[i] != s2[i]) 18 | { 19 | return (s1[i] - s2[i]); 20 | } 21 | i++; 22 | } 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x09-static_libraries/c-files/3-strspn.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strspn - Gets the length of a prefix substring. 4 | * @s: The string to be searched. 5 | * @accept: The prefix to be measured. 6 | * 7 | * Return: The number of bytes in s which 8 | * consist only of bytes from accept. 9 | * 10 | * PSEUDOCODE 11 | * Loop through s and compare s[i] to values in accept 12 | * increase bytes counter when values in accept can be found 13 | * *in s 14 | * end loop and return bytes when accept is exhaused 15 | */ 16 | unsigned int _strspn(char *s, char *accept) 17 | { 18 | unsigned int bytes = 0; 19 | int index; 20 | 21 | while (*s) 22 | { 23 | for (index = 0; accept[index]; index++) 24 | { 25 | if (*s == accept[index]) 26 | { 27 | bytes++; 28 | break; 29 | } 30 | 31 | else if (accept[index + 1] == '\0') 32 | return (bytes); 33 | } 34 | 35 | s++; 36 | } 37 | 38 | return (bytes); 39 | } 40 | -------------------------------------------------------------------------------- /0x09-static_libraries/c-files/4-isalpha.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isalpha - Retuns 1 if char c is a letter, 5 | * and a lowercase or uppercase 6 | * 7 | * @c: The character in ASCII code 8 | * 9 | * Return: 1 for lowercase. 0 for the rest. 10 | */ 11 | 12 | int _isalpha(int c) 13 | { 14 | if ((c >= 97 && c <= 122) || (c >= 65 && c <= 90)) 15 | { 16 | return (1); 17 | } 18 | else 19 | { 20 | return (0); 21 | } 22 | _putchar('\n'); 23 | } 24 | -------------------------------------------------------------------------------- /0x09-static_libraries/c-files/4-strpbrk.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strpbrk - searches a string for any of a set of bytes. 5 | * @s: first string. 6 | * @accept: second string. 7 | * Return: a pointer to the byte in s that matches one of the 8 | * bytes in accept, or NULL if no such byte is found. 9 | * 10 | */ 11 | char *_strpbrk(char *s, char *accept) 12 | { 13 | unsigned int i, j; 14 | 15 | for (i = 0; *(s + i) != '\0'; i++) 16 | { 17 | for (j = 0; *(accept + j) != '\0'; j++) 18 | { 19 | if (*(s + i) == *(accept + j)) 20 | return (s + i); 21 | } 22 | } 23 | return ('\0'); 24 | } 25 | -------------------------------------------------------------------------------- /0x09-static_libraries/c-files/5-strstr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strstr - a function that locates a substring 4 | * @haystack: locate a substring 5 | * @needle: substring to locate 6 | * 7 | * Return: pointer to the beginning of the located substring, 8 | * or NULL, if substring is not found 9 | */ 10 | char *_strstr(char *haystack, char *needle) 11 | { 12 | char *h = haystack; 13 | char *n = needle; 14 | 15 | while (*h) 16 | { 17 | n = needle; 18 | h = haystack; 19 | while (*n) 20 | { 21 | if (*h == *n) 22 | { 23 | n++; 24 | h++; 25 | } 26 | else 27 | break; 28 | } 29 | if (*n == '\0') 30 | return (haystack); 31 | haystack++; 32 | } 33 | return (0); 34 | } 35 | -------------------------------------------------------------------------------- /0x09-static_libraries/c-files/6-abs.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _abs - gives the absolute value 5 | * of an integer. 6 | * 7 | * @n: input number as an integer. 8 | * 9 | * Return: the absolute value of int n 10 | */ 11 | 12 | int _abs(int n) 13 | { 14 | if (n < 0) 15 | { 16 | return (n * -1); 17 | } 18 | return (n); 19 | } 20 | -------------------------------------------------------------------------------- /0x09-static_libraries/c-files/9-strcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * *_strcpy - copies the string pointed to by src 5 | * including the terminating null byte (\0) 6 | * to the buffer pointed to by dest 7 | * @dest: pointer to the buffer in which we copy the string 8 | * @src: string to be copied 9 | * 10 | * Return: the pointer to dest 11 | */ 12 | 13 | char *_strcpy(char *dest, char *src) 14 | { 15 | int len, i; 16 | 17 | len = 0; 18 | 19 | while (src[len] != '\0') 20 | { 21 | len++; 22 | } 23 | 24 | for (i = 0; i < len; i++) 25 | { 26 | dest[i] = src[i]; 27 | } 28 | 29 | dest[i] = '\0'; 30 | 31 | return (dest); 32 | } 33 | -------------------------------------------------------------------------------- /0x09-static_libraries/c-files/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | 5 | /** 6 | 7 | * _putchar - writes the character c to stdout 8 | 9 | * @c: The character to print 10 | 11 | * 12 | 13 | * Return: On success 1. 14 | 15 | * On error, -1 is returned, and errno is set appropriately. 16 | 17 | */ 18 | 19 | int _putchar(char c) 20 | 21 | { 22 | 23 | return (write(1, &c, 1)); 24 | 25 | } 26 | -------------------------------------------------------------------------------- /0x09-static_libraries/create_static_lib.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -Wall -pedantic -Werror -Wextra -c *.c 3 | ar rc liball.a *.o 4 | ranlib liball.a 5 | -------------------------------------------------------------------------------- /0x09-static_libraries/libmy.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x09-static_libraries/libmy.a -------------------------------------------------------------------------------- /0x09-static_libraries/main.h: -------------------------------------------------------------------------------- 1 | #ifndef FILE_MAIN 2 | #define FILE_MAIN 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 | -------------------------------------------------------------------------------- /0x09-static_libraries/object-files/0-isupper.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x09-static_libraries/object-files/0-isupper.o -------------------------------------------------------------------------------- /0x09-static_libraries/object-files/0-memset.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x09-static_libraries/object-files/0-memset.o -------------------------------------------------------------------------------- /0x09-static_libraries/object-files/0-strcat.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x09-static_libraries/object-files/0-strcat.o -------------------------------------------------------------------------------- /0x09-static_libraries/object-files/1-isdigit.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x09-static_libraries/object-files/1-isdigit.o -------------------------------------------------------------------------------- /0x09-static_libraries/object-files/1-memcpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x09-static_libraries/object-files/1-memcpy.o -------------------------------------------------------------------------------- /0x09-static_libraries/object-files/1-strncat.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x09-static_libraries/object-files/1-strncat.o -------------------------------------------------------------------------------- /0x09-static_libraries/object-files/100-atoi.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x09-static_libraries/object-files/100-atoi.o -------------------------------------------------------------------------------- /0x09-static_libraries/object-files/2-strchr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x09-static_libraries/object-files/2-strchr.o -------------------------------------------------------------------------------- /0x09-static_libraries/object-files/2-strlen.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x09-static_libraries/object-files/2-strlen.o -------------------------------------------------------------------------------- /0x09-static_libraries/object-files/2-strncpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x09-static_libraries/object-files/2-strncpy.o -------------------------------------------------------------------------------- /0x09-static_libraries/object-files/3-islower.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x09-static_libraries/object-files/3-islower.o -------------------------------------------------------------------------------- /0x09-static_libraries/object-files/3-puts.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x09-static_libraries/object-files/3-puts.o -------------------------------------------------------------------------------- /0x09-static_libraries/object-files/3-strcmp.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x09-static_libraries/object-files/3-strcmp.o -------------------------------------------------------------------------------- /0x09-static_libraries/object-files/3-strspn.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x09-static_libraries/object-files/3-strspn.o -------------------------------------------------------------------------------- /0x09-static_libraries/object-files/4-isalpha.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x09-static_libraries/object-files/4-isalpha.o -------------------------------------------------------------------------------- /0x09-static_libraries/object-files/4-strpbrk.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x09-static_libraries/object-files/4-strpbrk.o -------------------------------------------------------------------------------- /0x09-static_libraries/object-files/5-strstr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x09-static_libraries/object-files/5-strstr.o -------------------------------------------------------------------------------- /0x09-static_libraries/object-files/6-abs.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x09-static_libraries/object-files/6-abs.o -------------------------------------------------------------------------------- /0x09-static_libraries/object-files/9-strcpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x09-static_libraries/object-files/9-strcpy.o -------------------------------------------------------------------------------- /0x09-static_libraries/object-files/_putchar.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x09-static_libraries/object-files/_putchar.o -------------------------------------------------------------------------------- /0x0A-argc_argv/0-whatsmyname.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - prints its name, followed by a new line. 4 | * @argc: number of command line arguments. 5 | * @argv: array that contains the program command line arguments. 6 | * Return: 0 - success. 7 | */ 8 | int main(int argc __attribute__((unused)), char *argv[]) 9 | { 10 | printf("%s\n", argv[0]); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x0A-argc_argv/1-args.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - prints the number of arguments passed into it. 4 | * @argc: number of command line arguments. 5 | * @argv: array that contains the program command line arguments. 6 | * Return: 0 - success. 7 | */ 8 | int main(int argc, char *argv[] __attribute__((unused))) 9 | { 10 | printf("%d\n", argc - 1); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x0A-argc_argv/2-args.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - prints all arguments it receives. 4 | * @argc: number of command line arguments. 5 | * @argv: array that contains the program command line arguments. 6 | * Return: 0 - success. 7 | */ 8 | int main(int argc, char *argv[]) 9 | { 10 | int i; 11 | 12 | for (i = 0; i < argc; i++) 13 | printf("%s\n", argv[i]); 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x0A-argc_argv/3-mul.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main - multiplies two numbers. 5 | * @argc: number of command line arguments. 6 | * @argv: array that contains the program command line arguments. 7 | * Return: 0 - success. 8 | */ 9 | int main(int argc, char *argv[]) 10 | { 11 | if (argc != 3) 12 | { 13 | printf("Error\n"); 14 | return (1); 15 | } 16 | printf("%d\n", atoi(argv[1]) * atoi(argv[2])); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x0A-argc_argv/4-add.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * main - Prints the addition of positive numbers, 6 | * followed by a new line. 7 | * @argc: The number of arguments passed to the program. 8 | * @argv: An array of pointers to the arguments. 9 | * 10 | * Return: If one of the numbers contains symbols that are non-digits - 1. 11 | * Otherwise - 0. 12 | */ 13 | int main(int argc, char *argv[]) 14 | { 15 | int num, digit, sum = 0; 16 | 17 | for (num = 1; num < argc; num++) 18 | { 19 | for (digit = 0; argv[num][digit]; digit++) 20 | { 21 | if (argv[num][digit] < '0' || argv[num][digit] > '9') 22 | { 23 | printf("Error\n"); 24 | return (1); 25 | } 26 | } 27 | 28 | sum += atoi(argv[num]); 29 | } 30 | 31 | printf("%d\n", sum); 32 | 33 | return (0); 34 | } 35 | -------------------------------------------------------------------------------- /0x0A-argc_argv/README.md: -------------------------------------------------------------------------------- 1 | # 0x0A. C - argc, argv 2 | 3 | >This project aims at using arguments passed to the program 4 | 5 | ## Technologies 6 | * C files are written according to the C90 standard 7 | * Tested on Ubuntu 20.04 LTS 8 | 9 | ## Files 10 | All of the following files are programs written in C: 11 | 12 | | Filename | Description | 13 | | -------- | ----------- | 14 | | [0-whatsmyname.c](./0-whatsmyname.c) | Prints its name | 15 | | [1-args.c](./1-args.c) | Prints the number of arguments passed into it | 16 | | [2-args.c](./2-args.c) | Prints all arguments it receives | 17 | | [3-mul.c](./3-mul.c) | Multiplies two numbers | 18 | | [4-add.c](./4-add.c) | Adds positive numbers | 19 | | [100-change.c](./100-change.c) | Prints the minimum number of coins to make change for an amount of money | 20 | -------------------------------------------------------------------------------- /0x0B-malloc_free/0-create_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * create_array - creates an array of chars. 5 | * @size: size of the array. 6 | * @c: storaged char 7 | * 8 | * Return: pointer of an array of chars 9 | */ 10 | char *create_array(unsigned int size, char c) 11 | { 12 | char *array; 13 | unsigned int i = 0; 14 | 15 | if (size == 0) 16 | return (NULL); 17 | 18 | array = malloc(size * sizeof(char)); 19 | 20 | if (array == NULL) 21 | return (NULL); 22 | 23 | while (i < size) 24 | { 25 | array[i] = c; 26 | i++; 27 | } 28 | 29 | return (array); 30 | } 31 | -------------------------------------------------------------------------------- /0x0B-malloc_free/1-strdup.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _strdup - Returns a pointer to a newly-allocated space in memory 6 | * containing a copy of the string given as parameter. 7 | * @str: The string to be copied. 8 | * 9 | * Return: If str == NULL or insufficient memory is available - NULL. 10 | * Otherwise - a pointer to the duplicated string. 11 | */ 12 | char *_strdup(char *str) 13 | { 14 | unsigned int i; 15 | unsigned int strlen; 16 | char *cpy_str; 17 | 18 | if (str == NULL) 19 | return (NULL); 20 | 21 | for (strlen = 0; str[strlen] != '\0'; strlen++) 22 | ; 23 | 24 | cpy_str = malloc((strlen + 1) * sizeof(char)); 25 | 26 | if (cpy_str == NULL) 27 | return (NULL); 28 | 29 | for (i = 0; i < strlen; i++) 30 | { 31 | cpy_str[i] = str[i]; 32 | } 33 | 34 | cpy_str[strlen] = '\0'; 35 | 36 | return (cpy_str); 37 | } 38 | -------------------------------------------------------------------------------- /0x0B-malloc_free/100-argstostr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * argstostr - concatenates all the arguments of a program. 6 | * @ac: argument count. 7 | * @av: argument vector. 8 | * 9 | * Return: pointer of an array of char 10 | */ 11 | char *argstostr(int ac, char **av) 12 | { 13 | char *aout; 14 | int c, i, j, ia; 15 | 16 | if (ac == 0) 17 | return (NULL); 18 | 19 | for (c = i = 0; i < ac; i++) 20 | { 21 | if (av[i] == NULL) 22 | return (NULL); 23 | 24 | for (j = 0; av[i][j] != '\0'; j++) 25 | c++; 26 | c++; 27 | } 28 | 29 | aout = malloc((c + 1) * sizeof(char)); 30 | 31 | if (aout == NULL) 32 | { 33 | free(aout); 34 | return (NULL); 35 | } 36 | 37 | for (i = j = ia = 0; ia < c; j++, ia++) 38 | { 39 | if (av[i][j] == '\0') 40 | { 41 | aout[ia] = '\n'; 42 | i++; 43 | ia++; 44 | j = 0; 45 | } 46 | if (ia < c - 1) 47 | aout[ia] = av[i][j]; 48 | } 49 | aout[ia] = '\0'; 50 | 51 | return (aout); 52 | } 53 | -------------------------------------------------------------------------------- /0x0B-malloc_free/4-free_grid.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * free_grid - frees a 2 dimensional grid. 6 | * @grid: multidimensional array of integers. 7 | * @height: height of the grid. 8 | * 9 | * Return: no return 10 | */ 11 | void free_grid(int **grid, int height) 12 | { 13 | if (grid != NULL && height != 0) 14 | { 15 | for (; height >= 0; height--) 16 | free(grid[height]); 17 | free(grid); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x0B-malloc_free/main.h: -------------------------------------------------------------------------------- 1 | #ifndef _MAIN_H_ 2 | #define _MAIN_H_ 3 | 4 | char *create_array(unsigned int size, char c); 5 | char *_strdup(char *str); 6 | char *str_concat(char *s1, char *s2); 7 | int **alloc_grid(int width, int height); 8 | void free_grid(int **grid, int height); 9 | char *argstostr(int ac, char **av); 10 | 11 | #endif 12 | -------------------------------------------------------------------------------- /0x0B-malloc_free/test_files/1-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * main - check the code for ALX School students. 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(void) 11 | { 12 | char *s; 13 | 14 | s = _strdup("Holberton"); 15 | if (s == NULL) 16 | { 17 | printf("failed to allocate memory\n"); 18 | return (1); 19 | } 20 | printf("%s\n", s); 21 | free(s); 22 | return (0); 23 | } -------------------------------------------------------------------------------- /0x0B-malloc_free/test_files/2-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * main - check the code for ALX School students. 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(void) 11 | { 12 | char *s; 13 | 14 | s = str_concat("Betty ", "Holberton"); 15 | if (s == NULL) 16 | { 17 | printf("failed\n"); 18 | return (1); 19 | } 20 | printf("%s\n", s); 21 | free(s); 22 | return (0); 23 | } -------------------------------------------------------------------------------- /0x0C-more_malloc_free/0-malloc_checked.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * malloc_checked - allocates memory. 5 | * @b: amount of bytes. 6 | * 7 | * Return: pointer to the allocated memory. 8 | * if malloc fails, status value is equal to 98. 9 | */ 10 | void *malloc_checked(unsigned int b) 11 | { 12 | int *pointer; 13 | 14 | pointer = malloc(b); 15 | 16 | if (pointer == NULL) 17 | exit(98); 18 | 19 | return (pointer); 20 | } 21 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/100-realloc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x0C-more_malloc_free/100-realloc -------------------------------------------------------------------------------- /0x0C-more_malloc_free/101-mul.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | /** 5 | * _strlen - Finds the length of a string. 6 | * @str: The string to be measured. 7 | * 8 | * Return: The length of the string. 9 | */ 10 | int _strlen(char *str) 11 | { 12 | int i; 13 | 14 | for(i = 0; str[i]; i++) 15 | ; 16 | 17 | return (i); 18 | } 19 | /** 20 | * main - multiplies two positive numbers 21 | * @argc: number of arguments 22 | * @argv: array of arguments 23 | * 24 | * Return: always 0 (Success) 25 | */ 26 | int main(int argc, char *argv[]) 27 | { 28 | unsigned long int a, b, res; 29 | 30 | int len_a, len_b; 31 | 32 | len_a = _strlen(argv[1]); 33 | len_b = _strlen(argv[2]); 34 | 35 | for (i = 0; i < ) 36 | 37 | if (argc != 3) 38 | { 39 | printf("Error\n"); 40 | return (98); 41 | } 42 | a = atoi(argv[1]); 43 | b = atoi(argv[2]); 44 | 45 | res = mul(a, b); 46 | printf("%d", res); 47 | } 48 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/2-calloc.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _calloc - Allocates memory for an array of a certain number 6 | * of elements each of an inputted byte size. 7 | * @nmemb: The number of elements. 8 | * @size: The byte size of each array element. 9 | * 10 | * Return: If nmemb = 0, size = 0, or the function fails - NULL. 11 | * Otherwise - a pointer to the allocated memory. 12 | */ 13 | void *_calloc(unsigned int nmemb, unsigned int size) 14 | { 15 | unsigned int i; 16 | char *array; 17 | 18 | if (nmemb == 0 || size == 0) 19 | return (NULL); 20 | 21 | array = malloc(nmemb * size); 22 | 23 | if (array == NULL) 24 | return (NULL); 25 | 26 | for (i = 0; i < nmemb * size; i++) 27 | array[i] = 0; 28 | 29 | return (array); 30 | } 31 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/3-array_range.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * array_range - creates an array of integers. 5 | * @min: minimum value. 6 | * @max: maximum value. 7 | * 8 | * Return: pointer to the newly created array. 9 | * if min > max, returns NULL. 10 | * if malloc fails, returns NULL. 11 | */ 12 | int *array_range(int min, int max) 13 | { 14 | int *array; 15 | int i, j, size; 16 | 17 | if (min > max) 18 | return (NULL); 19 | 20 | size = (max - min) + 1; 21 | 22 | array = malloc(sizeof(int) * size); 23 | 24 | if (array == NULL) 25 | return (NULL); 26 | 27 | for (i = 0, j = min; j <= max; i++, j++) 28 | array[i] = j; 29 | 30 | return (array); 31 | } 32 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/main.h: -------------------------------------------------------------------------------- 1 | #ifndef _MAIN_H 2 | #define _MAIN_H 3 | 4 | void *malloc_checked(unsigned int b); 5 | char *string_nconcat(char *s1, char *s2, unsigned int n); 6 | void *_calloc(unsigned int nmemb, unsigned int size); 7 | int *array_range(int min, int max); 8 | void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size); 9 | 10 | #endif -------------------------------------------------------------------------------- /0x0C-more_malloc_free/test/0-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | #include 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | char *c; 14 | int *i; 15 | float *f; 16 | double *d; 17 | 18 | c = malloc_checked(sizeof(char) * 1024); 19 | printf("%p\n", (void *)c); 20 | i = malloc_checked(sizeof(int) * 402); 21 | printf("%p\n", (void *)i); 22 | f = malloc_checked(sizeof(float) * 100000000); 23 | printf("%p\n", (void *)f); 24 | d = malloc_checked(INT_MAX); 25 | printf("%p\n", (void *)d); 26 | free(c); 27 | free(i); 28 | free(f); 29 | free(d); 30 | return (0); 31 | } -------------------------------------------------------------------------------- /0x0C-more_malloc_free/test/1-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * main - check the code 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(void) 11 | { 12 | char *concat; 13 | 14 | concat = string_nconcat("Best ", "School !!!", 6); 15 | printf("%s\n", concat); 16 | free(concat); 17 | return (0); 18 | } -------------------------------------------------------------------------------- /0x0C-more_malloc_free/test/100-realloc.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x0C-more_malloc_free/test/100-realloc.exe -------------------------------------------------------------------------------- /0x0C-more_malloc_free/test/2-calloc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x0C-more_malloc_free/test/2-calloc -------------------------------------------------------------------------------- /0x0C-more_malloc_free/test/2-calloc.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x0C-more_malloc_free/test/2-calloc.exe -------------------------------------------------------------------------------- /0x0C-more_malloc_free/test/3-array_range.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x0C-more_malloc_free/test/3-array_range.exe -------------------------------------------------------------------------------- /0x0C-more_malloc_free/test/3-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | #include 5 | 6 | /** 7 | * simple_print_buffer - prints buffer in hexa 8 | * @buffer: the address of memory to print 9 | * @size: the size of the memory to print 10 | * 11 | * Return: Nothing. 12 | */ 13 | void simple_print_buffer(int *buffer, unsigned int size) 14 | { 15 | unsigned int i; 16 | 17 | i = 0; 18 | while (i < size) 19 | { 20 | if (i % 10) 21 | { 22 | printf(" "); 23 | } 24 | if (!(i % 10) && i) 25 | { 26 | printf("\n"); 27 | } 28 | printf("0x%02x", buffer[i]); 29 | i++; 30 | } 31 | printf("\n"); 32 | } 33 | 34 | /** 35 | * main - check the code 36 | * 37 | * Return: Always 0. 38 | */ 39 | int main(void) 40 | { 41 | int *a; 42 | 43 | a = array_range(0, 10); 44 | simple_print_buffer(a, 11); 45 | free(a); 46 | return (0); 47 | } -------------------------------------------------------------------------------- /0x0C-more_malloc_free/test/result: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x0C-more_malloc_free/test/result -------------------------------------------------------------------------------- /0x0C-more_malloc_free/test/result.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x0C-more_malloc_free/test/result.exe -------------------------------------------------------------------------------- /0x0D-preprocessor/0-object_like_macro.h: -------------------------------------------------------------------------------- 1 | #ifndef _OBJECT_LIKE_MACRO_H_ 2 | #define _OBJECT_LIKE_MACRO_H_ 3 | 4 | #define SIZE 1024 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /0x0D-preprocessor/1-pi.h: -------------------------------------------------------------------------------- 1 | #ifndef _1_PI_H 2 | #define _1_PI_H 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 of the program 5 | * was compiled from, followed by a new line. 6 | * 7 | * Return: Always 0. 8 | */ 9 | 10 | int main(void) 11 | { 12 | printf("%s\n", __FILE__); 13 | 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x0D-preprocessor/3-function_like_macro.h: -------------------------------------------------------------------------------- 1 | #ifndef FUNCTION_LIKE_MACRO_H 2 | #define FUNCTION_LIKE_MACRO_H 3 | 4 | #define ABS(x) ((x) < 0 ? -(x) : (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 | /** 6 | * main - check the code 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(void) 11 | { 12 | int i; 13 | int j; 14 | 15 | i = ABS(-98) * 10; 16 | j = ABS(98) * 10; 17 | printf("%d, %d\n", i, j); 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /0x0D-preprocessor/3-main.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x0D-preprocessor/3-main.exe -------------------------------------------------------------------------------- /0x0D-preprocessor/4-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "4-sum.h" 3 | #include "4-sum.h" 4 | 5 | /** 6 | * main - check the code 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(void) 11 | { 12 | int s; 13 | 14 | s = SUM(98, 1024); 15 | printf("%d\n", s); 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x0D-preprocessor/4-main.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x0D-preprocessor/4-main.exe -------------------------------------------------------------------------------- /0x0D-preprocessor/4-sum.h: -------------------------------------------------------------------------------- 1 | #ifndef _SUM_H 2 | #define _SUM_H 3 | 4 | #define SUM(x, y) ((x) + (y)) 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /0x0D-preprocessor/Test/0-main.c: -------------------------------------------------------------------------------- 1 | #include "0-object_like_macro.h" 2 | #include "0-object_like_macro.h" 3 | #include 4 | 5 | /** 6 | * main - check the code 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(void) 11 | { 12 | int s; 13 | 14 | s = 98 + SIZE; 15 | printf("%d\n", s); 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x0D-preprocessor/Test/1-main.c: -------------------------------------------------------------------------------- 1 | #include "1-pi.h" 2 | #include "1-pi.h" 3 | #include 4 | 5 | /** 6 | * main - check the code 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(void) 11 | { 12 | float a; 13 | float r; 14 | 15 | r = 98; 16 | a = PI * r * r; 17 | printf("%.3f\n", a); 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /0x0D-preprocessor/d.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x0D-preprocessor/d.exe -------------------------------------------------------------------------------- /0x0E-structures_typedef/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | struct dog my_dog; 12 | 13 | my_dog.name = "Poppy"; 14 | my_dog.age = 3.5; 15 | my_dog.owner = "Bob"; 16 | printf("My name is %s, and I am %.1f :) - Woof!\n", my_dog.name, my_dog.age); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/1-init_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | 4 | /** 5 | * init_dog - Initializes a variable of type struct dog. 6 | * @d: The dog to be initialized. 7 | * @name: The name of the dog. 8 | * @age: The age of the dog. 9 | * @owner: The owner of the dog. 10 | */ 11 | void init_dog(struct dog *d, char *name, float age, char *owner) 12 | { 13 | if (d != NULL) 14 | { 15 | d->name = name; 16 | d->age = age; 17 | d->owner = owner; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/2-print_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * print_dog - Prints a struct dog. 7 | * @d: The struct dog to be printed. 8 | */ 9 | void print_dog(struct dog *d) 10 | { 11 | if (d) 12 | { 13 | if (d->name) 14 | printf("Name: %s\n", d->name); 15 | else 16 | printf("Name: (nil)\n"); 17 | 18 | if (d->age >= 0) 19 | printf("Age: %.06f\n", d->age); 20 | else 21 | printf("Age: (nil)\n"); 22 | 23 | if (d->owner) 24 | printf("Owner: %s\n", d->owner); 25 | else 26 | printf("Owner: (nil)\n"); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/5-free_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | 4 | /** 5 | * free_dog - Frees dogs. 6 | * @d: The dog to be freed. 7 | */ 8 | void free_dog(dog_t *d) 9 | { 10 | if (d) 11 | { 12 | if (d->name) 13 | free(d->name); 14 | if (d->owner) 15 | free(d->owner); 16 | free(d); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/dog.h: -------------------------------------------------------------------------------- 1 | #ifndef _DOG_H_ 2 | #define _DOG_H_ 3 | 4 | /** 5 | * struct dog - A new type describing a dog. 6 | * @name: The name of the dog. 7 | * @age: The age of the dog. 8 | * @owner: The owner of the dog. 9 | */ 10 | struct dog 11 | { 12 | char *name; 13 | float age; 14 | char *owner; 15 | }; 16 | 17 | typedef struct dog dog_t; 18 | 19 | void init_dog(struct dog *d, char *name, float age, char *owner); 20 | void print_dog(struct dog *d); 21 | dog_t *new_dog(char *name, float age, char *owner); 22 | void free_dog(dog_t *d); 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/test/0-main.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x0E-structures_typedef/test/0-main.exe -------------------------------------------------------------------------------- /0x0E-structures_typedef/test/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | struct dog my_dog; 12 | 13 | init_dog(&my_dog, "Poppy", 3.5, "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/test/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | struct dog my_dog; 12 | 13 | my_dog.name = "Poppy"; 14 | my_dog.age = 3.5; 15 | my_dog.owner = "Bob"; 16 | print_dog(&my_dog); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/test/4-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | dog_t *my_dog; 12 | 13 | my_dog = new_dog("Poppy", 3.5, "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/test/5-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | dog_t *my_dog; 12 | 13 | my_dog = new_dog("Poppy", 3.5, "Bob"); 14 | printf("My name is %s, and I am %.1f :) - Woof!\n", my_dog->name, my_dog->age); 15 | free_dog(my_dog); 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/test/b.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x0E-structures_typedef/test/b.exe -------------------------------------------------------------------------------- /0x0E-structures_typedef/test/c.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x0E-structures_typedef/test/c.exe -------------------------------------------------------------------------------- /0x0E-structures_typedef/test/e.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x0E-structures_typedef/test/e.exe -------------------------------------------------------------------------------- /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 | 11 | void print_name(char *name, void (*f)(char *)) 12 | { 13 | if (f && name) 14 | (*f)(name); 15 | } 16 | -------------------------------------------------------------------------------- /0x0F-function_pointers/1-array_iterator.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "function_pointers.h" 3 | 4 | /** 5 | * array_iterator - executes a function given as a 6 | * parameter on each element of an array. 7 | * @array: input integer array. 8 | * @size: size of the array. 9 | * @action: pointer to the function. 10 | * 11 | * Return: no return. 12 | */ 13 | 14 | void array_iterator(int *array, size_t size, void (*action)(int)) 15 | { 16 | size_t i; 17 | 18 | if (!array || !size || !action) 19 | return; 20 | 21 | for (i = 0; i < size; i++) 22 | { 23 | action(array[i]); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /0x0F-function_pointers/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "function_pointers.h" 3 | 4 | /** 5 | * print_elem - prints an integer 6 | * @elem: the integer to print 7 | * 8 | * Return: Nothing. 9 | */ 10 | void print_elem(int elem) 11 | { 12 | printf("%d\n", elem); 13 | } 14 | 15 | /** 16 | * print_elem_hex - prints an integer, in hexadecimal 17 | * @elem: the integer to print 18 | * 19 | * Return: Nothing. 20 | */ 21 | void print_elem_hex(int elem) 22 | { 23 | printf("0x%x\n", elem); 24 | } 25 | 26 | /** 27 | * main - check the code 28 | * 29 | * Return: Always 0. 30 | */ 31 | int main(void) 32 | { 33 | int array[5] = {0, 98, 402, 1024, 4096}; 34 | 35 | array_iterator(array, 5, &print_elem); 36 | array_iterator(array, 5, &print_elem_hex); 37 | return (0); 38 | } 39 | -------------------------------------------------------------------------------- /0x0F-function_pointers/2-int_index.c: -------------------------------------------------------------------------------- 1 | #include "function_pointers.h" 2 | 3 | /** 4 | * int_index - Searches for an integer in an array of integers. 5 | * @array: The array of integers. 6 | * @size: The size of the array. 7 | * @cmp: A pointer to the function to be used to compare values. 8 | * 9 | * Return: If no element matches or size <= 0 - -1. 10 | * Otherwise - The index of the first element for which 11 | * the cmp function does not return 0. 12 | */ 13 | int int_index(int *array, int size, int (*cmp)(int)) 14 | { 15 | int i; 16 | 17 | if (size <= 0) 18 | return (-1); 19 | 20 | if (!array || !size || !cmp) 21 | return (-1); 22 | 23 | for (i = 0; i < size; i++) 24 | { 25 | if (cmp(array[i])) 26 | return (i); 27 | } 28 | 29 | return (-1); 30 | } 31 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-calc.h: -------------------------------------------------------------------------------- 1 | #ifndef _CALC_H 2 | #define _CALC_H 3 | 4 | /** 5 | * struct op - Struct op 6 | * 7 | * @op: The operator 8 | * @f: The function associated 9 | */ 10 | typedef struct op 11 | { 12 | char *op; 13 | int (*f)(int a, int b); 14 | } op_t; 15 | 16 | int op_add(int a, int b); 17 | int op_sub(int a, int b); 18 | int op_mul(int a, int b); 19 | int op_div(int a, int b); 20 | int op_mod(int a, int b); 21 | int (*get_op_func(char *s))(int, int); 22 | 23 | #endif 24 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-get_op_func.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "3-calc.h" 3 | 4 | /** 5 | * get_op_func - selects the correct function to perform 6 | * the operation asked by the user. 7 | * @s: char operator. 8 | * 9 | * Return: pointer to the function that corresponds to the operator. 10 | */ 11 | int (*get_op_func(char *s))(int, int) 12 | { 13 | op_t ops[] = { 14 | {"+", op_add}, 15 | {"-", op_sub}, 16 | {"*", op_mul}, 17 | {"/", op_div}, 18 | {"%", op_mod}, 19 | {NULL, NULL} 20 | }; 21 | 22 | int i = 0; 23 | 24 | while (ops[i].op) 25 | { 26 | if (*s == *(ops[i].op)) 27 | return (ops[i].f); 28 | i++; 29 | } 30 | 31 | return (NULL); 32 | } 33 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-main.c: -------------------------------------------------------------------------------- 1 | #include "3-calc.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * main - check the code for Holberton School students. 7 | * @argc: argument count. 8 | * @argv: argument vector. 9 | * 10 | * Return: Always 0. 11 | */ 12 | int main(int argc, char *argv[]) 13 | { 14 | int a, b, res; 15 | 16 | if (argc != 4) 17 | { 18 | printf("Error\n"); 19 | exit(98); 20 | } 21 | 22 | if (argv[2][1] || get_op_func(argv[2]) == NULL) 23 | { 24 | printf("Error\n"); 25 | exit(99); 26 | } 27 | 28 | a = atoi(argv[1]); 29 | b = atoi(argv[3]); 30 | 31 | if ((argv[2][0] == '/' || argv[2][0] == '%') && (b == 0)) 32 | { 33 | printf("Error\n"); 34 | exit(100); 35 | } 36 | 37 | res = (*get_op_func(argv[2]))(a, b); 38 | 39 | printf("%d\n", res); 40 | return (0); 41 | } 42 | -------------------------------------------------------------------------------- /0x0F-function_pointers/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x0F-function_pointers/a.exe -------------------------------------------------------------------------------- /0x0F-function_pointers/b.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x0F-function_pointers/b.exe -------------------------------------------------------------------------------- /0x0F-function_pointers/c.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x0F-function_pointers/c.exe -------------------------------------------------------------------------------- /0x0F-function_pointers/calc.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x0F-function_pointers/calc.exe -------------------------------------------------------------------------------- /0x0F-function_pointers/function_pointers.h: -------------------------------------------------------------------------------- 1 | #ifndef _FUNCTION_POINTERS_H_ 2 | #define _FUNCTION_POINTERS_H_ 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 11 | -------------------------------------------------------------------------------- /0x10-variadic_functions/0-sum_them_all.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * sum_them_all - returns the sum of all its parameters. 7 | * @n: amount of the arguments. 8 | * 9 | * Return: sum of its parameters. 10 | */ 11 | int sum_them_all(const unsigned int n, ...) 12 | { 13 | va_list list; 14 | unsigned int i, sum = 0; 15 | 16 | if (n == 0) 17 | return (0); 18 | 19 | va_start(list, n); 20 | 21 | for (i = 0; i < n; i++) 22 | sum += va_arg(list, int); 23 | 24 | va_end(list); 25 | 26 | return (sum); 27 | } 28 | -------------------------------------------------------------------------------- /0x10-variadic_functions/1-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * print_numbers - Prints numbers, followed by a new line. 7 | * @separator: The string to be printed between numbers. 8 | * @n: The number of integers passed to the function. 9 | * @...: A variable number of numbers to be printed. 10 | */ 11 | void print_numbers(const char *separator, const unsigned int n, ...) 12 | { 13 | va_list list; 14 | unsigned int i; 15 | unsigned int num; 16 | 17 | va_start(list, n); 18 | 19 | for (i = 0; i < n; i++) 20 | { 21 | num = va_arg(list, int); 22 | printf("%d", num); 23 | 24 | if (separator && i < n - 1) 25 | printf("%s", separator); 26 | } 27 | 28 | va_end(list); 29 | printf("\n"); 30 | } 31 | -------------------------------------------------------------------------------- /0x10-variadic_functions/2-print_strings.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * print_strings - Prints strings, followed by a new line. 7 | * @separator: The string to be printed between strings. 8 | * @n: The number of strings passed to the function. 9 | * @...: A variable number of strings to be printed. 10 | * 11 | * Description: If separator is NULL, it is not printed. 12 | * If one of the strings if NULL, (nil) is printed instead. 13 | */ 14 | void print_strings(const char *separator, const unsigned int n, ...) 15 | { 16 | va_list list; 17 | unsigned int i; 18 | char *str; 19 | 20 | va_start(list, n); 21 | 22 | for (i = 0; i < n; i++) 23 | { 24 | str = va_arg(list, char *); 25 | 26 | if (str) 27 | printf("%s", str); 28 | else 29 | printf("(nil)"); 30 | 31 | if ((i < n - 1) && separator) 32 | printf("%s", separator); 33 | } 34 | 35 | printf("\n"); 36 | va_end(list); 37 | } 38 | -------------------------------------------------------------------------------- /0x10-variadic_functions/3-main.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | 3 | /** 4 | * main - check the code 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | print_all("ceis", 'B', 3, "stSchool"); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x10-variadic_functions/d.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x10-variadic_functions/d.exe -------------------------------------------------------------------------------- /0x10-variadic_functions/variadic_functions.h: -------------------------------------------------------------------------------- 1 | #ifndef _VARIADIC_FUNCTIONS_ 2 | #define _VARIADIC_FUNCTIONS_ 3 | 4 | int sum_them_all(const unsigned int n, ...); 5 | void print_numbers(const char *separator, const unsigned int n, ...); 6 | void print_strings(const char *separator, const unsigned int n, ...); 7 | void print_all(const char * const format, ...); 8 | 9 | #endif 10 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/0-print_list.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * print_list - Prints all the elements of a list_t list. 5 | * @h: list. 6 | * 7 | * Return: number of nodes in the list. 8 | */ 9 | size_t print_list(const list_t *h) 10 | { 11 | size_t i = 0; 12 | 13 | while (h) 14 | { 15 | if (h->str == NULL) 16 | printf("[0] (nil)\n"); 17 | else 18 | printf("[%d] %s\n", h->len, h->str); 19 | h = h->next; 20 | i++; 21 | } 22 | return (i); 23 | } 24 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/1-list_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * list_len - returns then number of elements in a list. 5 | * @h: list. 6 | * Return: number of elements in list. 7 | */ 8 | size_t list_len(const list_t *h) 9 | { 10 | size_t i = 0; 11 | 12 | while (h) 13 | { 14 | h = h->next; 15 | i++; 16 | } 17 | 18 | return (i); 19 | } 20 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/100-first.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * constructorRun - runs 5 | * before main function is executed. 6 | */ 7 | void constructorRun(void) 8 | { 9 | printf("You're beat! and yet, you must allow,\n" 10 | "I bore my house upon my back!\n"); 11 | } 12 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/101-hello_holberton.asm: -------------------------------------------------------------------------------- 1 | global main 2 | 3 | section .text 4 | 5 | main: 6 | mov rax, 1 7 | mov rdi, 1 8 | mov rsi, msg 9 | mov rdx, msglen 10 | syscall 11 | mov rax, 60 12 | mov rdi, 0 13 | syscall 14 | 15 | section .rodata 16 | msg: db "Hello, Holberton", 10 17 | msglen: equ $ - msg 18 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/101-hello_holberton.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x12-singly_linked_lists/101-hello_holberton.o -------------------------------------------------------------------------------- /0x12-singly_linked_lists/2-add_node.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_node - Adds a new node at the beginning 5 | * of a list_t list. 6 | * @head: The head of the list_t list. 7 | * @str: The string to be added to the list_t list. 8 | * 9 | * Return: NULL on failure and the address 10 | * of the new element on success. 11 | */ 12 | list_t *add_node(list_t **head, const char *str) 13 | { 14 | list_t *new = NULL; 15 | size_t i = 0; 16 | 17 | new = malloc(sizeof(list_t)); 18 | if (!new) 19 | { 20 | free(new); 21 | return (NULL); 22 | } 23 | 24 | for (i = 0; str[i]; i++) 25 | ; 26 | 27 | new->str = strdup(str) ? strdup(str) : (NULL); 28 | new->len = i; 29 | new->next = *head; 30 | 31 | *head = new; 32 | 33 | return (new); 34 | } 35 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/3-add_node_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_node_end - add new node to end list. 5 | * @head: The head of the list_t list. 6 | * @str: The string to be added to the list_t list. 7 | * Return: NULL on failure and the address 8 | * of the new element on success. 9 | */ 10 | list_t *add_node_end(list_t **head, const char *str) 11 | { 12 | list_t *new, *temp; 13 | size_t i = 0; 14 | 15 | new = malloc(sizeof(list_t)); 16 | if (!new) 17 | { 18 | free(new); 19 | return (NULL); 20 | } 21 | 22 | for (i = 0; str[i]; i++) 23 | ; 24 | 25 | new->str = strdup(str) ? strdup(str) : (NULL); 26 | new->len = i; 27 | new->next = NULL; 28 | 29 | if (!(*head)) 30 | *head = new; 31 | else 32 | { 33 | temp = *head; 34 | /*Use temp->next instead of just temp*/ 35 | while (temp->next != NULL) 36 | temp = temp->next; 37 | temp->next = new; 38 | } 39 | 40 | return (new); 41 | } 42 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/4-free_list.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_list - Frees list. 5 | * @head: The list. 6 | */ 7 | void free_list(list_t *head) 8 | { 9 | list_t *temp; 10 | 11 | while (head) 12 | { 13 | temp = head; 14 | free(head->str); 15 | free(head); 16 | head = temp->next; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/Tests/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | list_t *head; 14 | list_t *new; 15 | list_t hello = {"World", 5, NULL}; 16 | size_t n; 17 | 18 | head = &hello; 19 | new = malloc(sizeof(list_t)); 20 | if (new == NULL) 21 | { 22 | printf("Error\n"); 23 | return (1); 24 | } 25 | new->str = strdup("Hello"); 26 | new->len = 5; 27 | new->next = head; 28 | head = new; 29 | n = print_list(head); 30 | printf("-> %llu elements\n", n); 31 | 32 | printf("\n"); 33 | free(new->str); 34 | new->str = NULL; 35 | n = print_list(head); 36 | printf("-> %llu elements\n", n); 37 | 38 | free(new); 39 | return (0); 40 | } 41 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/Tests/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | list_t *head; 14 | list_t *new; 15 | list_t hello = {"World", 5, NULL}; 16 | size_t n; 17 | 18 | head = &hello; 19 | new = malloc(sizeof(list_t)); 20 | if (new == NULL) 21 | { 22 | printf("Error\n"); 23 | return (1); 24 | } 25 | new->str = strdup("Hello"); 26 | new->len = 5; 27 | new->next = head; 28 | head = new; 29 | n = list_len(head); 30 | printf("-> %lu elements\n", n); 31 | free(new->str); 32 | free(new); 33 | return (0); 34 | } 35 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/Tests/100-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - check the code 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | printf("(A tortoise, having pretty good sense of a hare's nature, challenges one to a race.)\n"); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/Tests/101-hello_holberton.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x12-singly_linked_lists/Tests/101-hello_holberton.o -------------------------------------------------------------------------------- /0x12-singly_linked_lists/Tests/4-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | list_t *head; 14 | 15 | head = NULL; 16 | add_node_end(&head, "Bob"); 17 | add_node_end(&head, "&"); 18 | add_node_end(&head, "Kris"); 19 | add_node_end(&head, "love"); 20 | add_node_end(&head, "asm"); 21 | print_list(head); 22 | free_list(head); 23 | head = NULL; 24 | return (0); 25 | } 26 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/Tests/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x12-singly_linked_lists/Tests/a.out -------------------------------------------------------------------------------- /0x12-singly_linked_lists/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x12-singly_linked_lists/a.out -------------------------------------------------------------------------------- /0x12-singly_linked_lists/hello: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x12-singly_linked_lists/hello -------------------------------------------------------------------------------- /0x12-singly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef LISTS_H 2 | #define LISTS_H 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 | */ 16 | typedef struct list_s 17 | { 18 | char *str; 19 | unsigned int len; 20 | struct list_s *next; 21 | } list_t; 22 | 23 | size_t print_list(const list_t *h); 24 | size_t list_len(const list_t *h); 25 | list_t *add_node(list_t **head, const char *str); 26 | list_t *add_node_end(list_t **head, const char *str); 27 | void free_list(list_t *head); 28 | void constructorRun (void) __attribute__ ((constructor)); 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/0-print_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * print_listint - Prints all the elements of a list. 5 | * @h: pointer to the head of the list. 6 | * 7 | * Return: The number of nodes in the list. 8 | */ 9 | size_t print_listint(const listint_t *h) 10 | { 11 | size_t i = 0; 12 | 13 | while (h) 14 | { 15 | printf("%d\n", h->n); 16 | h = h->next; 17 | i++; 18 | } 19 | 20 | return (i); 21 | } 22 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/1-listint_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * listint_len - returns the number of elements in 5 | * a linked list. 6 | * @h: head of list. 7 | * 8 | * Return: numbers of elements in list. 9 | */ 10 | size_t listint_len(const listint_t *h) 11 | { 12 | size_t num = 0; 13 | 14 | while (h) 15 | { 16 | h = h->next; 17 | num++; 18 | } 19 | 20 | return (num); 21 | } 22 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/10-delete_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * delete_nodeint_at_index - deletes the node at index 5 | * of a linked list. 6 | * @head: head of a list. 7 | * @index: index of the list where the node is 8 | * deleted. 9 | * 10 | * Return: 1 if it succeeded, -1 if it failed. 11 | */ 12 | int delete_nodeint_at_index(listint_t **head, unsigned int index) 13 | { 14 | listint_t *curr, *prev, *temp; 15 | unsigned int i = 0; 16 | 17 | curr = *head; 18 | prev = curr; 19 | 20 | if ((*head) && index == 0) 21 | { 22 | *head = (*head)->next; 23 | free(prev); 24 | return (1); 25 | } 26 | 27 | while (curr) 28 | { 29 | temp = curr; 30 | 31 | if (i == index) 32 | { 33 | prev->next = curr->next; 34 | free(curr); 35 | return (1); 36 | } 37 | curr = curr->next; 38 | prev = temp; 39 | i++; 40 | } 41 | 42 | return (-1); 43 | } 44 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/2-add_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_nodeint - Adds a new node at the beginning 5 | * of a listint_t list. 6 | * @head: A pointer to the address of the 7 | * head of the list. 8 | * @n: The integer for the new node to contain. 9 | * 10 | * Return: the address of the new element, 11 | * or NULL if it failed 12 | */ 13 | listint_t *add_nodeint(listint_t **head, const int n) 14 | { 15 | listint_t *new = NULL; 16 | 17 | new = (listint_t *)malloc(sizeof(listint_t)); 18 | if (!new) 19 | return (NULL); 20 | 21 | new->n = n; 22 | new->next = *head; 23 | 24 | *head = new; 25 | 26 | return (new); 27 | } 28 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/3-add_nodeint_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_nodeint_end - Adds a new node at the end 5 | * of a listint_t list. 6 | * @head: A pointer to the address of the 7 | * head of the list. 8 | * @n: The integer for the new node to contain. 9 | * 10 | * Return: the address of the new element, 11 | * or NULL if it failed 12 | */ 13 | listint_t *add_nodeint_end(listint_t **head, const int n) 14 | { 15 | listint_t *temp; 16 | listint_t *new = NULL; 17 | 18 | new = (listint_t *)malloc(sizeof(listint_t)); 19 | if (!new) 20 | return (NULL); 21 | 22 | new->n = n; 23 | new->next = NULL; 24 | 25 | if (!(*head)) 26 | *head = new; 27 | else 28 | { 29 | temp = *head; 30 | while (temp->next != NULL) 31 | temp = temp->next; 32 | 33 | temp->next = new; 34 | } 35 | 36 | return (new); 37 | } 38 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/4-free_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_listint - Frees a listint_t list. 5 | * @head: A pointer to the head of the list. 6 | */ 7 | void free_listint(listint_t *head) 8 | { 9 | listint_t *temp = head; 10 | 11 | while (head) 12 | { 13 | temp = temp->next; 14 | free(head); 15 | head = temp; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/5-free_listint2.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_listint2 - Frees a listint_t list 5 | * and sets the head to NULL. 6 | * @head: A pointer to the address of the 7 | * head of the listint_t list. * 8 | */ 9 | void free_listint2(listint_t **head) 10 | { 11 | listint_t *temp; 12 | 13 | if (head == NULL) 14 | return; 15 | 16 | while (*head) 17 | { 18 | temp = (*head)->next; 19 | free(*head); 20 | *head = temp; 21 | } 22 | 23 | *head = NULL; 24 | } 25 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/6-pop_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * pop_listint - Delete the head node of a list. 5 | * @head: A pointer to the address of the 6 | * head of the list. 7 | * 8 | * Return: If the linked list is empty - 0. 9 | * Otherwise - The head node's data (n). 10 | */ 11 | int pop_listint(listint_t **head) 12 | { 13 | listint_t *temp; 14 | int data; 15 | 16 | if (!(*head)) 17 | return (0); 18 | 19 | temp = *head; 20 | data = temp->n; 21 | temp = temp->next; 22 | 23 | free(*head); 24 | 25 | *head = temp; 26 | 27 | return (data); 28 | } 29 | -------------------------------------------------------------------------------- /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, starting at 0. 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 = 0; 13 | 14 | while (head != NULL) 15 | { 16 | if (i == index) 17 | return (head); 18 | i++, head = head->next; 19 | } 20 | 21 | return (NULL); 22 | } 23 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/8-sum_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * sum_listint - returns the sum of all the data (n) of 5 | * a linked list. 6 | * @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 = 0; 13 | 14 | if (!head) 15 | return (0); 16 | 17 | while (head) 18 | { 19 | sum += head->n; 20 | head = head->next; 21 | } 22 | 23 | return (sum); 24 | } 25 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/Tests/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("-> %llu elements\n", n); 30 | free(new); 31 | return (0); 32 | } 33 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/Tests/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("-> %llu elements\n", n); 30 | free(new); 31 | return (0); 32 | } 33 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/Tests/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/Tests/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/Tests/6-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 n; 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 | n = pop_listint(&head); 27 | printf("- %d\n", n); 28 | print_listint(head); 29 | n = pop_listint(&head); 30 | printf("- %d\n", n); 31 | print_listint(head); 32 | free_listint2(&head); 33 | printf("%p\n", (void *)head); 34 | return (0); 35 | } 36 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/Tests/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/Tests/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/Tests/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/Tests/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x13-more_singly_linked_lists/Tests/a.exe -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/Tests/b.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x13-more_singly_linked_lists/Tests/b.exe -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/Tests/c.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x13-more_singly_linked_lists/Tests/c.exe -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/Tests/d.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x13-more_singly_linked_lists/Tests/d.exe -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/Tests/g.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x13-more_singly_linked_lists/Tests/g.exe -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/Tests/h.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x13-more_singly_linked_lists/Tests/h.exe -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/Tests/i.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x13-more_singly_linked_lists/Tests/i.exe -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/Tests/j.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x13-more_singly_linked_lists/Tests/j.exe -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/Tests/k.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x13-more_singly_linked_lists/Tests/k.exe -------------------------------------------------------------------------------- /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(8); 16 | printf("\n"); 17 | print_binary(98); 18 | printf("\n"); 19 | print_binary(1024); 20 | printf("\n"); 21 | print_binary((1 << 10) + 1); 22 | printf("\n"); 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/1-print_binary.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_binary - prints the binary representation 5 | * of a number. 6 | * @n: unsigned long int. 7 | * 8 | * Return: no return. 9 | */ 10 | void print_binary(unsigned long int n) 11 | { 12 | unsigned long int i = 1, a; 13 | 14 | if (n == 0) 15 | _putchar('0'); 16 | 17 | while (i <= n) 18 | i *= 2; 19 | i >>= 1; 20 | 21 | while (n > 0) 22 | { 23 | if (n > i) 24 | { 25 | _putchar('1'); 26 | n -= i; 27 | i >>= 1; 28 | } 29 | else if (n == i) 30 | { 31 | _putchar('1'); 32 | a = (i >> 1); 33 | if (a > 0) 34 | { 35 | for (i = a; i > 0; i >>= 1) 36 | _putchar('0'); 37 | } 38 | break; 39 | } 40 | else 41 | { 42 | _putchar('0'); 43 | i >>= 1; 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/2-get_bit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include "stdlib.h" 3 | 4 | /** 5 | * get_bit - returns the value of a bit at an index 6 | * @n: number 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 | if (index > 63) 14 | return (-1); 15 | 16 | return ((n >> index) & 1); 17 | } 18 | -------------------------------------------------------------------------------- /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 | n = get_bit(8, 0); 20 | printf("%d\n", n); 21 | n = get_bit(5, 1); 22 | printf("%d\n", n); 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /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 | * set_bit - sets bit at index to 1 4 | * @n: pointer to number 5 | * @index: index to change 6 | * Return: 1 on success, -1 on failure 7 | */ 8 | int set_bit(unsigned long int *n, unsigned int index) 9 | { 10 | if (index > 63) 11 | return (-1); 12 | 13 | *n = *n | (1 << index); 14 | return (1); 15 | } 16 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/4-clear_bit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * clear_bit - sets the value of a bit to 0 at a given index 4 | * @n: pointer to number 5 | * @index: index to change 6 | * Return: 1 if it worked or -1 if an error occurred 7 | */ 8 | int clear_bit(unsigned long int *n, unsigned int index) 9 | { 10 | if (index > 63) 11 | return (-1); 12 | 13 | *n = *n & ~(1 << index); 14 | return (1); 15 | } 16 | -------------------------------------------------------------------------------- /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 - counts the number of bits to change 5 | * to get from one number to another 6 | * @n: first number 7 | * @m: second number to transform 8 | * 9 | * Return: number of bits to transform 10 | */ 11 | unsigned int flip_bits(unsigned long int n, unsigned long int m) 12 | { 13 | int i, num = 0; 14 | 15 | for (i = 63; i >= 0; i--) 16 | { 17 | if (((n ^ m) >> i) & 1) 18 | num++; 19 | } 20 | 21 | return (num); 22 | } 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 "main.h" 2 | #include 3 | 4 | /** 5 | * _putchar - writes the character c to stdout 6 | * @c: The character to print 7 | * 8 | * Return: On success 1. 9 | * On error, -1 is returned, and errno is set appropriately. 10 | */ 11 | int _putchar(char c) 12 | { 13 | return (write(1, &c, 1)); 14 | } 15 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x14-bit_manipulation/a.exe -------------------------------------------------------------------------------- /0x14-bit_manipulation/b.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x14-bit_manipulation/b.exe -------------------------------------------------------------------------------- /0x14-bit_manipulation/c.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x14-bit_manipulation/c.exe -------------------------------------------------------------------------------- /0x14-bit_manipulation/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | int _putchar(char c); 5 | unsigned int _strlen(char const *str); 6 | unsigned int _pow(unsigned int a, unsigned int b); 7 | unsigned int binary_to_uint(const char *b); 8 | void print_binary(unsigned long int n); 9 | int get_bit(unsigned long int n, unsigned int index); 10 | int set_bit(unsigned long int *n, unsigned int index); 11 | int clear_bit(unsigned long int *n, unsigned int index); 12 | unsigned int flip_bits(unsigned long int n, unsigned long int m); 13 | int get_endianness(void); 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /0x15-file_io/0-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 | } 25 | -------------------------------------------------------------------------------- /0x15-file_io/0-read_textfile.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * read_textfile - Reads a text file and prints it to POSIX stdout. 5 | * @filename: A pointer to the name of the file. 6 | * @letters: The number of letters the 7 | * function should read and print. 8 | * 9 | * Return: If the function fails or filename is NULL - 0. 10 | * O/w - the actual number of bytes the function can read and print. 11 | */ 12 | ssize_t read_textfile(const char *filename, size_t letters) 13 | { 14 | ssize_t fd, r, w; 15 | char *buffer; 16 | 17 | if (!filename) 18 | return (0); 19 | 20 | buffer = malloc(letters * sizeof(char)); 21 | if (!buffer) 22 | return (0); 23 | 24 | fd = open(filename, O_RDONLY); 25 | r = read(fd, buffer, letters); 26 | w = write(STDOUT_FILENO, buffer, r); 27 | 28 | if (fd == -1 || r == -1 || w == -1 || w != r) 29 | { 30 | free(buffer); 31 | return (0); 32 | } 33 | 34 | free(buffer); 35 | close(fd); 36 | 37 | return (w); 38 | } 39 | -------------------------------------------------------------------------------- /0x15-file_io/1-create_file.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * create_file - Creates a file. 5 | * @filename: A pointer to the name of the file to create. 6 | * @text_content: A pointer to a string to write to the file. 7 | * 8 | * Return: If the function fails - -1. 9 | * Otherwise - 1. 10 | */ 11 | int create_file(const char *filename, char *text_content) 12 | { 13 | int fd, w, len; 14 | 15 | if (filename == NULL) 16 | return (-1); 17 | 18 | if (!text_content) 19 | text_content = ""; 20 | 21 | for (len = 0; *(text_content + len); len++) 22 | ; 23 | 24 | fd = open(filename, O_CREAT | O_RDWR | O_TRUNC, 0600); 25 | w = write(fd, text_content, len); 26 | 27 | if (fd == -1 || w == -1) 28 | return (-1); 29 | 30 | close(fd); 31 | 32 | return (1); 33 | } 34 | -------------------------------------------------------------------------------- /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 - Appends text at the end of a file. 5 | * @filename: A pointer to the name of the file. 6 | * @text_content: The string to add to the end of the file. 7 | * 8 | * Return: If the function fails or filename is NULL - -1. 9 | * If the file does not exist the user lacks write permissions - -1. 10 | * Otherwise - 1. 11 | */ 12 | int append_text_to_file(const char *filename, char *text_content) 13 | { 14 | int fd, w, len = 0; 15 | 16 | if (filename == NULL) 17 | return (-1); 18 | 19 | if (text_content) 20 | { 21 | for (len = 0; text_content[len]; len++) 22 | ; 23 | fd = open(filename, O_WRONLY | O_APPEND); 24 | w = write(fd, text_content, len); 25 | 26 | if (fd == -1 || w == -1) 27 | return (-1); 28 | } 29 | 30 | close(fd); 31 | 32 | return (1); 33 | } 34 | -------------------------------------------------------------------------------- /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/Requiescat: -------------------------------------------------------------------------------- 1 | Requiescat 2 | by Oscar Wilde 3 | 4 | Tread lightly, she is near 5 | Under the snow, 6 | Speak gently, she can hear 7 | The daisies grow. 8 | 9 | All her bright golden hair 10 | Tarnished with rust, 11 | She that was young and fair 12 | Fallen to dust. 13 | 14 | Lily-like, white as snow, 15 | She hardly knew 16 | She was a woman, so 17 | Sweetly she grew. 18 | 19 | Coffin-board, heavy stone, 20 | Lie on her breast, 21 | I vex my heart alone, 22 | She is at rest. 23 | 24 | Peace, Peace, she cannot hear 25 | Lyre or sonnet, 26 | All my life's buried here, 27 | Heap earth upon it. 28 | -------------------------------------------------------------------------------- /0x15-file_io/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | int _putchar(char c); 12 | ssize_t read_textfile(const char *filename, size_t letters); 13 | int create_file(const char *filename, char *text_content); 14 | int append_text_to_file(const char *filename, char *text_content); 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /0x17-doubly_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 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 | } 35 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/0-print_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * print_dlistint - prints all elements of 5 | * doubly linked list 6 | * @h: head pointer to the list 7 | * 8 | * Return: number of nodes 9 | */ 10 | size_t print_dlistint(const dlistint_t *h) 11 | { 12 | size_t i = 0; 13 | 14 | while (h) 15 | { 16 | printf("%d\n", h->n); 17 | h = h->next; 18 | i++; 19 | } 20 | return (i); 21 | } 22 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/1-dlistint_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * dlistint_len - returns the number of elements 5 | * in a doubly linked list 6 | * @h: head pointer to the list 7 | * 8 | * Return: number of elements 9 | */ 10 | size_t dlistint_len(const dlistint_t *h) 11 | { 12 | size_t i = 0; 13 | 14 | while (h) 15 | { 16 | i++; 17 | h = h->next; 18 | } 19 | 20 | return (i); 21 | } 22 | -------------------------------------------------------------------------------- /0x17-doubly_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 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/100-password: -------------------------------------------------------------------------------- 1 | en C Pyfo neZ 2 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/102-result: -------------------------------------------------------------------------------- 1 | 906609 -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/2-add_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_dnodeint - adds a new node at the 5 | * beginning of a doubly linked list 6 | * @head: head double pointer to the list 7 | * @n: int data to insert in the new node 8 | * 9 | * Return: the address of the new element, or NULL if it failed 10 | */ 11 | dlistint_t *add_dnodeint(dlistint_t **head, const int n) 12 | { 13 | dlistint_t *new = malloc(sizeof(dlistint_t)); 14 | 15 | if (!new) 16 | return (NULL); 17 | 18 | new->n = n; 19 | 20 | if (!(*head)) 21 | { 22 | *head = new; 23 | new->next = NULL; 24 | new->prev = NULL; 25 | return (new); 26 | } 27 | 28 | new->next = *head; 29 | new->prev = NULL; 30 | (*head)->prev = new; 31 | *head = new; 32 | 33 | return (new); 34 | } 35 | -------------------------------------------------------------------------------- /0x17-doubly_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 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/3-add_dnodeint_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_dnodeint_end - adds a new node at the end 5 | * of a doubly linked list 6 | * @head: head double pointer to the list 7 | * @n: int data to insert in the new node 8 | * 9 | * Return: the address of the new element, or NULL if it failed 10 | */ 11 | dlistint_t *add_dnodeint_end(dlistint_t **head, const int n) 12 | { 13 | dlistint_t *last = *head; 14 | dlistint_t *new = malloc(sizeof(dlistint_t)); 15 | 16 | if (!new) 17 | return (NULL); 18 | 19 | new->n = n; 20 | new->next = NULL; 21 | 22 | if (!(*head)) 23 | { 24 | *head = new; 25 | new->prev = NULL; 26 | return (new); 27 | } 28 | 29 | while (last->next) 30 | last = last->next; 31 | 32 | last->next = new; 33 | new->prev = last; 34 | 35 | return (new); 36 | } 37 | -------------------------------------------------------------------------------- /0x17-doubly_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 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/4-free_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * free_dlistint - frees a doubly linked list 4 | * @head: head pointer to the list 5 | */ 6 | void free_dlistint(dlistint_t *head) 7 | { 8 | dlistint_t *temp; 9 | 10 | while (head) 11 | { 12 | temp = head->next; 13 | free(head); 14 | head = temp; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/4-m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x17-doubly_linked_lists/4-m -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/4-main.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * main - check the code 5 | * 6 | * Return: Always EXIT_SUCCESS. 7 | */ 8 | int main(void) 9 | { 10 | dlistint_t *head; 11 | 12 | head = NULL; 13 | add_dnodeint_end(&head, 0); 14 | add_dnodeint_end(&head, 1); 15 | add_dnodeint_end(&head, 2); 16 | add_dnodeint_end(&head, 3); 17 | add_dnodeint_end(&head, 4); 18 | add_dnodeint_end(&head, 98); 19 | add_dnodeint_end(&head, 402); 20 | add_dnodeint_end(&head, 1024); 21 | print_dlistint(head); 22 | free_dlistint(head); 23 | head = NULL; 24 | return (EXIT_SUCCESS); 25 | } 26 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/5-get_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * get_dnodeint_at_index - gets the nth node of a doubly linked list 5 | * @head: head pointer to the list 6 | * @index: index of the node to return 7 | * 8 | * Return: address of the node, or if it does not exist, NULL 9 | */ 10 | dlistint_t *get_dnodeint_at_index(dlistint_t *head, unsigned int index) 11 | { 12 | unsigned int i = 0; 13 | 14 | if (!head) 15 | return (NULL); 16 | 17 | while (head) 18 | { 19 | if (index == i) 20 | return (head); 21 | head = head->next; 22 | i++; 23 | } 24 | 25 | return (NULL); 26 | } 27 | -------------------------------------------------------------------------------- /0x17-doubly_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 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/6-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code 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/6-sum_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * sum_dlistint - returns the sum of all the data 5 | * of a doubly linked list 6 | * @head: head pointer to the list 7 | * 8 | * Return: sum of the nodes or 0 on empty 9 | */ 10 | int sum_dlistint(dlistint_t *head) 11 | { 12 | int sum = 0; 13 | 14 | while (head) 15 | { 16 | sum += head->n; 17 | head = head->next; 18 | } 19 | 20 | return (sum); 21 | } 22 | -------------------------------------------------------------------------------- /0x17-doubly_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 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 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/8-delete_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * delete_dnodeint_at_index - deletes a node in a doubly linked list 5 | * at a given index 6 | * @head: head double pointer to the list 7 | * @index: index of the node to delete 8 | * 9 | * Return: 1 on success, -1 on failure 10 | */ 11 | int delete_dnodeint_at_index(dlistint_t **head, unsigned int index) 12 | { 13 | dlistint_t *temp = *head; 14 | unsigned int i = 0; 15 | 16 | if (!(*head)) 17 | return (-1); 18 | 19 | while (temp && i != index) 20 | { 21 | i++; 22 | temp = temp->next; 23 | } 24 | 25 | if (!temp) 26 | return (-1); 27 | else if (temp == *head) 28 | { 29 | *head = (*head)->next; 30 | (*head)->prev = NULL; 31 | free(temp); 32 | } 33 | else if (temp->next == NULL) 34 | { 35 | temp->prev->next = NULL; 36 | free(temp); 37 | } 38 | else 39 | { 40 | temp->prev->next = temp->next; 41 | temp->next->prev = temp->prev; 42 | free(temp); 43 | } 44 | 45 | return (1); 46 | } 47 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/README.md: -------------------------------------------------------------------------------- 1 | # C - Doubly linked lists 2 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/e: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x17-doubly_linked_lists/e -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x17-doubly_linked_lists/h -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/j: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x17-doubly_linked_lists/j -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/k: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x17-doubly_linked_lists/k -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef LISTS_H 2 | #define LISTS_H 3 | 4 | #include 5 | #include 6 | 7 | /** 8 | * struct dlistint_s - doubly linked list 9 | * @n: integer 10 | * @prev: points to the previous node 11 | * @next: points to the next node 12 | * 13 | * Description: doubly linked list node structure 14 | * for Holberton project 15 | */ 16 | typedef struct dlistint_s 17 | { 18 | int n; 19 | struct dlistint_s *prev; 20 | struct dlistint_s *next; 21 | } dlistint_t; 22 | 23 | size_t print_dlistint(const dlistint_t *h); 24 | size_t dlistint_len(const dlistint_t *h); 25 | dlistint_t *add_dnodeint(dlistint_t **head, const int n); 26 | dlistint_t *add_dnodeint_end(dlistint_t **head, const int n); 27 | void free_dlistint(dlistint_t *head); 28 | dlistint_t *get_dnodeint_at_index(dlistint_t *head, unsigned int index); 29 | int sum_dlistint(dlistint_t *head); 30 | dlistint_t *insert_dnodeint_at_index(dlistint_t **h, unsigned int idx, int n); 31 | int delete_dnodeint_at_index(dlistint_t **head, unsigned int index); 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/1-create_dynamic_lib.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc *.c -c -fPIC 3 | gcc *.o -shared -o liball.so 4 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/100-operations.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 | return (a / b); 19 | } 20 | 21 | int mod(int a, int b) 22 | { 23 | return (a % b); 24 | } 25 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/100-operations.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x18-dynamic_libraries/100-operations.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/100-operations.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x18-dynamic_libraries/100-operations.so -------------------------------------------------------------------------------- /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/Adebayo-S/alx-low_level_programming/raw/master/0x18-dynamic_libraries/libmask.so 3 | export LD_PRELOAD=/tmp/libmask.so 4 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/README.md: -------------------------------------------------------------------------------- 1 | # C - Dynamic libraries 2 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/c-files/0-isupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _isupper - checks for uppercase character 6 | * @c: Variable text 7 | * Return: Always 0. 8 | */ 9 | 10 | int _isupper(int c) 11 | { 12 | if (c >= 'A' && c <= 'Z') 13 | { 14 | return (1); 15 | } 16 | else 17 | { 18 | return (0); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/c-files/0-memset.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _memset - fills memory with a constant byte, 4 | * @s: memory area. 5 | * @b: constant byte. 6 | * @n: bytes filled. 7 | * Return: the pointer to dest. 8 | * 9 | * PSEUDOCODE 10 | * Loop through "s" using "n" as limit 11 | * replace s[i] with "b" 12 | * return s 13 | */ 14 | char *_memset(char *s, char b, unsigned int n) 15 | { 16 | unsigned int i; 17 | 18 | for (i = 0; i < n; i++) 19 | *(s + i) = b; 20 | 21 | return (s); 22 | } 23 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/c-files/0-strcat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strcat - concatenates two strings, 4 | * @dest: destination. 5 | * @src: source. 6 | * Return: the pointer to dest. 7 | */ 8 | char *_strcat(char *dest, char *src) 9 | { 10 | int count = 0, count2 = 0; 11 | 12 | while (*(dest + count) != '\0') 13 | { 14 | count++; 15 | } 16 | 17 | while (count2 >= 0) 18 | { 19 | *(dest + count) = *(src + count2); 20 | if (*(src + count2) == '\0') 21 | break; 22 | count++; 23 | count2++; 24 | } 25 | return (dest); 26 | } 27 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/c-files/1-isdigit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isdigit - function that check if it's a digit 5 | * @c: takes in a character/digit 6 | * Return: 1 if digit and 0 if anything else 7 | */ 8 | 9 | int _isdigit(int c) 10 | { 11 | if (c >= '0' && c <= '9') 12 | return (1); 13 | return (0); 14 | } 15 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/c-files/1-memcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _memcpy - copies memory area, 4 | * @dest: destination memory area. 5 | * @src: source memory area. 6 | * @n: bytes filled. 7 | * Return: the pointer to dest. 8 | * 9 | * PSEUDOCODE 10 | * Looping through the length of bytes n 11 | * dest[i] = src[i] 12 | */ 13 | char *_memcpy(char *dest, char *src, unsigned int n) 14 | { 15 | unsigned int i; 16 | 17 | for (i = 0; i < n; i++) 18 | *(dest + i) = *(src + i); 19 | 20 | return (dest); 21 | } 22 | 23 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/c-files/1-strncat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strncat - concatenates n bytes from a string to another 5 | * @dest: destination string 6 | * @src: source string 7 | * @n: number of bytes of str to concatenate 8 | * 9 | * Return: a pointer to the resulting string dest 10 | */ 11 | char *_strncat(char *dest, char *src, int n) 12 | { 13 | int i, j; 14 | 15 | i = 0; 16 | j = 0; 17 | 18 | while (dest[i] != '\0') 19 | i++; 20 | 21 | while (src[j] != '\0' && j < n) 22 | { 23 | dest[i] = src[j]; 24 | i++; 25 | j++; 26 | } 27 | 28 | dest[i] = '\0'; 29 | 30 | return (dest); 31 | } 32 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/c-files/100-atoi.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _atoi - converts a string to an integer 5 | * @s: string to be converted 6 | * 7 | * Return: the int converted from the string 8 | */ 9 | 10 | int _atoi(char *s) 11 | { 12 | int i, d, n, len, f, digit; 13 | 14 | i = 0; 15 | d = 0; 16 | n = 0; 17 | len = 0; 18 | f = 0; 19 | digit = 0; 20 | 21 | while (s[len] != '\0') 22 | len++; 23 | 24 | while (i < len && f == 0) 25 | { 26 | if (s[i] == '-') 27 | ++d; 28 | 29 | if (s[i] >= '0' && s[i] <= '9') 30 | { 31 | digit = s[i] - '0'; 32 | 33 | if (d % 2) 34 | digit = -digit; 35 | n = n * 10 + digit; 36 | f = 1; 37 | 38 | if (s[i + 1] < '0' || s[i + 1] > '9') 39 | break; 40 | f = 0; 41 | } 42 | i++; 43 | } 44 | 45 | if (f == 0) 46 | return (0); 47 | 48 | return (n); 49 | } 50 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/c-files/2-strchr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strchr - locates a character in a string 4 | * @s: string. 5 | * @c: character. 6 | * Return: the pointer to the first occurance of the character c 7 | */ 8 | char *_strchr(char *s, char c) 9 | { 10 | unsigned int i = 0; 11 | 12 | for (; *(s + i) != '\0'; i++) 13 | if (*(s + i) == c) 14 | return (s + i); 15 | if (*(s + i) == c) 16 | return (s + i); 17 | return ('\0'); 18 | } 19 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/c-files/2-strlen.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strlen - Returns the length of a string. 5 | * @s: The string to get the length of. 6 | * 7 | * Return: The length of @s. 8 | */ 9 | 10 | int _strlen(char *s) 11 | { 12 | int lenStr = 0; 13 | 14 | while (*s++) 15 | lenStr++; 16 | 17 | return (lenStr); 18 | } 19 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/c-files/2-strncpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strncpy - copy a string 5 | * @dest: input value 6 | * @src: input value 7 | * @n: input value 8 | * 9 | * Return: dest 10 | */ 11 | char *_strncpy(char *dest, char *src, int n) 12 | { 13 | int j; 14 | 15 | j = 0; 16 | while (j < n && src[j] != '\0') 17 | { 18 | dest[j] = src[j]; 19 | j++; 20 | } 21 | while (j < n) 22 | { 23 | dest[j] = '\0'; 24 | j++; 25 | } 26 | 27 | return (dest); 28 | } 29 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/c-files/3-islower.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _islower - Retuns 1 if char is lowercase character 5 | * else, return 0 6 | * 7 | * @c: The character in ASCII code 8 | * 9 | * Return: 1 for lowercase. 0 for the rest. 10 | */ 11 | 12 | int _islower(int c) 13 | { 14 | if (c >= 97 && c <= 122) 15 | { 16 | return (1); 17 | } 18 | else 19 | { 20 | return (0); 21 | } 22 | _putchar('\n'); 23 | } 24 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/c-files/3-puts.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _puts - prints a string to stdout 5 | * @str: pointer to the string to print 6 | */ 7 | 8 | void _puts(char *str) 9 | { 10 | int i; 11 | 12 | for (i = 0; str[i] != '\0'; i++) 13 | { 14 | _putchar(str[i]); 15 | } 16 | _putchar('\n'); 17 | } 18 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/c-files/3-strcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strcmp - compare string values 5 | * @s1: input value 6 | * @s2: input value 7 | * 8 | * Return: s1[i] - s2[i] 9 | */ 10 | int _strcmp(char *s1, char *s2) 11 | { 12 | int i; 13 | 14 | i = 0; 15 | while (s1[i] != '\0' && s2[i] != '\0') 16 | { 17 | if (s1[i] != s2[i]) 18 | { 19 | return (s1[i] - s2[i]); 20 | } 21 | i++; 22 | } 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/c-files/3-strspn.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strspn - Gets the length of a prefix substring. 4 | * @s: The string to be searched. 5 | * @accept: The prefix to be measured. 6 | * 7 | * Return: The number of bytes in s which 8 | * consist only of bytes from accept. 9 | * 10 | * PSEUDOCODE 11 | * Loop through s and compare s[i] to values in accept 12 | * increase bytes counter when values in accept can be found 13 | * *in s 14 | * end loop and return bytes when accept is exhaused 15 | */ 16 | unsigned int _strspn(char *s, char *accept) 17 | { 18 | unsigned int bytes = 0; 19 | int index; 20 | 21 | while (*s) 22 | { 23 | for (index = 0; accept[index]; index++) 24 | { 25 | if (*s == accept[index]) 26 | { 27 | bytes++; 28 | break; 29 | } 30 | 31 | else if (accept[index + 1] == '\0') 32 | return (bytes); 33 | } 34 | 35 | s++; 36 | } 37 | 38 | return (bytes); 39 | } 40 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/c-files/4-isalpha.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isalpha - Retuns 1 if char c is a letter, 5 | * and a lowercase or uppercase 6 | * 7 | * @c: The character in ASCII code 8 | * 9 | * Return: 1 for lowercase. 0 for the rest. 10 | */ 11 | 12 | int _isalpha(int c) 13 | { 14 | if ((c >= 97 && c <= 122) || (c >= 65 && c <= 90)) 15 | { 16 | return (1); 17 | } 18 | else 19 | { 20 | return (0); 21 | } 22 | _putchar('\n'); 23 | } 24 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/c-files/4-strpbrk.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strpbrk - searches a string for any of a set of bytes. 5 | * @s: first string. 6 | * @accept: second string. 7 | * Return: a pointer to the byte in s that matches one of the 8 | * bytes in accept, or NULL if no such byte is found. 9 | * 10 | */ 11 | char *_strpbrk(char *s, char *accept) 12 | { 13 | unsigned int i, j; 14 | 15 | for (i = 0; *(s + i) != '\0'; i++) 16 | { 17 | for (j = 0; *(accept + j) != '\0'; j++) 18 | { 19 | if (*(s + i) == *(accept + j)) 20 | return (s + i); 21 | } 22 | } 23 | return ('\0'); 24 | } 25 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/c-files/5-strstr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strstr - a function that locates a substring 4 | * @haystack: locate a substring 5 | * @needle: substring to locate 6 | * 7 | * Return: pointer to the beginning of the located substring, 8 | * or NULL, if substring is not found 9 | */ 10 | char *_strstr(char *haystack, char *needle) 11 | { 12 | char *h = haystack; 13 | char *n = needle; 14 | 15 | while (*h) 16 | { 17 | n = needle; 18 | h = haystack; 19 | while (*n) 20 | { 21 | if (*h == *n) 22 | { 23 | n++; 24 | h++; 25 | } 26 | else 27 | break; 28 | } 29 | if (*n == '\0') 30 | return (haystack); 31 | haystack++; 32 | } 33 | return (0); 34 | } 35 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/c-files/6-abs.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _abs - gives the absolute value 5 | * of an integer. 6 | * 7 | * @n: input number as an integer. 8 | * 9 | * Return: the absolute value of int n 10 | */ 11 | 12 | int _abs(int n) 13 | { 14 | if (n < 0) 15 | { 16 | return (n * -1); 17 | } 18 | return (n); 19 | } 20 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/c-files/9-strcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * *_strcpy - copies the string pointed to by src 5 | * including the terminating null byte (\0) 6 | * to the buffer pointed to by dest 7 | * @dest: pointer to the buffer in which we copy the string 8 | * @src: string to be copied 9 | * 10 | * Return: the pointer to dest 11 | */ 12 | 13 | char *_strcpy(char *dest, char *src) 14 | { 15 | int len, i; 16 | 17 | len = 0; 18 | 19 | while (src[len] != '\0') 20 | { 21 | len++; 22 | } 23 | 24 | for (i = 0; i < len; i++) 25 | { 26 | dest[i] = src[i]; 27 | } 28 | 29 | dest[i] = '\0'; 30 | 31 | return (dest); 32 | } 33 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/c-files/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | 5 | /** 6 | 7 | * _putchar - writes the character c to stdout 8 | 9 | * @c: The character to print 10 | 11 | * 12 | 13 | * Return: On success 1. 14 | 15 | * On error, -1 is returned, and errno is set appropriately. 16 | 17 | */ 18 | 19 | int _putchar(char c) 20 | 21 | { 22 | 23 | return (write(1, &c, 1)); 24 | 25 | } 26 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/libdynamic.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x18-dynamic_libraries/libdynamic.so -------------------------------------------------------------------------------- /0x18-dynamic_libraries/libmask.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x18-dynamic_libraries/libmask.so -------------------------------------------------------------------------------- /0x18-dynamic_libraries/main.h: -------------------------------------------------------------------------------- 1 | #ifndef _MAIN_H 2 | #define _MAIN_H 3 | int _putchar(char c); 4 | int _islower(int c); 5 | int _isalpha(int c); 6 | int _abs(int n); 7 | int _isupper(int c); 8 | int _isdigit(int c); 9 | int _strlen(char *s); 10 | void _puts(char *s); 11 | char *_strcpy(char *dest, char *src); 12 | int _atoi(char *s); 13 | char *_strcat(char *dest, char *src); 14 | char *_strncat(char *dest, char *src, int n); 15 | char *_strncpy(char *dest, char *src, int n); 16 | int _strcmp(char *s1, char *s2); 17 | char *_memset(char *s, char b, unsigned int n); 18 | char *_memcpy(char *dest, char *src, unsigned int n); 19 | char *_strchr(char *s, char c); 20 | unsigned int _strspn(char *s, char *accept); 21 | char *_strpbrk(char *s, char *accept); 22 | char *_strstr(char *haystack, char *needle); 23 | #endif /*_MAIN_H*/ 24 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/object-files/0-isupper.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x18-dynamic_libraries/object-files/0-isupper.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/object-files/0-memset.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x18-dynamic_libraries/object-files/0-memset.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/object-files/0-strcat.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x18-dynamic_libraries/object-files/0-strcat.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/object-files/1-isdigit.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x18-dynamic_libraries/object-files/1-isdigit.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/object-files/1-memcpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x18-dynamic_libraries/object-files/1-memcpy.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/object-files/1-strncat.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x18-dynamic_libraries/object-files/1-strncat.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/object-files/100-atoi.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x18-dynamic_libraries/object-files/100-atoi.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/object-files/2-strchr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x18-dynamic_libraries/object-files/2-strchr.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/object-files/2-strlen.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x18-dynamic_libraries/object-files/2-strlen.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/object-files/2-strncpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x18-dynamic_libraries/object-files/2-strncpy.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/object-files/3-islower.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x18-dynamic_libraries/object-files/3-islower.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/object-files/3-puts.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x18-dynamic_libraries/object-files/3-puts.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/object-files/3-strcmp.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x18-dynamic_libraries/object-files/3-strcmp.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/object-files/3-strspn.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x18-dynamic_libraries/object-files/3-strspn.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/object-files/4-isalpha.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x18-dynamic_libraries/object-files/4-isalpha.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/object-files/4-strpbrk.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x18-dynamic_libraries/object-files/4-strpbrk.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/object-files/5-strstr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x18-dynamic_libraries/object-files/5-strstr.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/object-files/6-abs.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x18-dynamic_libraries/object-files/6-abs.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/object-files/9-strcpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x18-dynamic_libraries/object-files/9-strcpy.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/object-files/_putchar.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x18-dynamic_libraries/object-files/_putchar.o -------------------------------------------------------------------------------- /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: pointer to new hash table or NULL on error. 8 | */ 9 | hash_table_t *hash_table_create(unsigned long int size) 10 | { 11 | hash_table_t *hashtable = NULL; 12 | 13 | hashtable = malloc(sizeof(hash_table_t)); 14 | if (!hashtable || !size) 15 | return (NULL); 16 | 17 | hashtable->size = size; 18 | hashtable->array = malloc(sizeof(hash_node_t *) * size); 19 | 20 | return (hashtable); 21 | } 22 | -------------------------------------------------------------------------------- /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 - implementation of the djb2 algorithm 5 | * @str: string used to generate hash value 6 | * 7 | * Return: hash value 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 | { 17 | hash = ((hash << 5) + hash) + c; /* hash * 33 + c */ 18 | } 19 | return (hash); 20 | } 21 | -------------------------------------------------------------------------------- /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 | #include 3 | 4 | /** 5 | * key_index -gives the index of the key 6 | * @key: the key (string) 7 | * @size: the size of the array 8 | * 9 | * Return: the index at which the key/value pair should 10 | * be stored in the array of the hash table. 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/2-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 | unsigned long int hash_table_array_size; 15 | 16 | hash_table_array_size = 1024; 17 | s = "cisfun"; 18 | printf("%lu\n", hash_djb2((unsigned char *)s)); 19 | printf("%lu\n", key_index((unsigned char *)s, hash_table_array_size)); 20 | s = "Don't forget to tweet today"; 21 | printf("%lu\n", hash_djb2((unsigned char *)s)); 22 | printf("%lu\n", key_index((unsigned char *)s, hash_table_array_size)); 23 | s = "98"; 24 | printf("%lu\n", hash_djb2((unsigned char *)s)); 25 | printf("%lu\n", key_index((unsigned char *)s, hash_table_array_size)); 26 | return (EXIT_SUCCESS); 27 | } 28 | -------------------------------------------------------------------------------- /0x1A-hash_tables/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/4-hash_table_get.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_get - retrieves a value associated with a key. 5 | * @ht: pointer to the hash table. 6 | * @key: is the key I am looking for. 7 | * 8 | * Return: the value associated with the key or NULL if not found. 9 | */ 10 | char *hash_table_get(const hash_table_t *ht, const char *key) 11 | { 12 | unsigned long int idx; 13 | hash_node_t *temp; 14 | 15 | if (!ht || !key) 16 | return (NULL); 17 | 18 | idx = key_index((const unsigned char *) key, ht->size); 19 | 20 | temp = ht->array[idx]; 21 | 22 | while (temp) 23 | { 24 | if (strcmp(temp->key, key) == 0) 25 | return (temp->value); 26 | temp = temp->next; 27 | } 28 | 29 | return (NULL); 30 | } 31 | -------------------------------------------------------------------------------- /0x1A-hash_tables/5-hash_table_print.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_print - prints the values in a hash table. 5 | * @ht: pointer to the hash table. 6 | * 7 | * Return: the value associated with the key or NULL if not found. 8 | */ 9 | void hash_table_print(const hash_table_t *ht) 10 | { 11 | unsigned long int idx = 0; 12 | hash_node_t *temp = NULL; 13 | 14 | if (!ht) 15 | return; 16 | 17 | printf("{"); 18 | 19 | for (idx = 0; idx < ht->size; idx++) 20 | { 21 | if (ht->array[idx]) 22 | { 23 | printf("'%s': '%s'", ht->array[idx]->key, ht->array[idx]->value); 24 | idx++; 25 | break; 26 | } 27 | } 28 | 29 | while (idx < ht->size) 30 | { 31 | if (ht->array[idx]) 32 | { 33 | temp = ht->array[idx]; 34 | while (temp) 35 | { 36 | printf(", '%s': '%s'", temp->key, temp->value); 37 | temp = temp->next; 38 | } 39 | } 40 | idx++; 41 | } 42 | 43 | printf("}\n"); 44 | } 45 | -------------------------------------------------------------------------------- /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: pointer to the hash table. 6 | * 7 | * Return: nothing. 8 | */ 9 | void hash_table_delete(hash_table_t *ht) 10 | { 11 | unsigned long int idx = 0; 12 | hash_node_t *temp = NULL; 13 | hash_node_t *trail = NULL; 14 | 15 | if (!ht) 16 | return; 17 | 18 | while (idx < ht->size) 19 | { 20 | temp = ht->array[idx]; 21 | while (temp) 22 | { 23 | trail = temp; 24 | temp = temp->next; 25 | free(trail->key); 26 | free(trail->value); 27 | free(trail); 28 | } 29 | idx++; 30 | } 31 | 32 | free(ht->array); 33 | free(ht); 34 | } 35 | -------------------------------------------------------------------------------- /0x1A-hash_tables/README.md: -------------------------------------------------------------------------------- 1 | # 0x1A. C - Hash tables 2 | -------------------------------------------------------------------------------- /0x1A-hash_tables/a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x1A-hash_tables/a -------------------------------------------------------------------------------- /0x1A-hash_tables/b: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x1A-hash_tables/b -------------------------------------------------------------------------------- /0x1A-hash_tables/c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x1A-hash_tables/c -------------------------------------------------------------------------------- /0x1A-hash_tables/d: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x1A-hash_tables/d -------------------------------------------------------------------------------- /0x1A-hash_tables/e: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x1A-hash_tables/e -------------------------------------------------------------------------------- /0x1A-hash_tables/f: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x1A-hash_tables/f -------------------------------------------------------------------------------- /0x1A-hash_tables/g: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x1A-hash_tables/g -------------------------------------------------------------------------------- /0x1C-makefiles/0-Makefile: -------------------------------------------------------------------------------- 1 | # -*- MakeFile -*- 2 | 3 | #target: dependencies 4 | # action 5 | 6 | all: school 7 | school: school.c main.c 8 | gcc main.c school.c -o school 9 | -------------------------------------------------------------------------------- /0x1C-makefiles/1-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | 4 | all: $(SRC) 5 | $(CC) $(SRC) -o school 6 | -------------------------------------------------------------------------------- /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) $(CLFAGS) $(OBJ) -o $(NAME) 10 | 11 | clean: 12 | $(RM) *~ $(NAME) 13 | 14 | oclean: 15 | $(RM) $(OBJ) 16 | 17 | fclean: clean oclean 18 | 19 | re: fclean all 20 | -------------------------------------------------------------------------------- /0x1C-makefiles/5-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 | [1, 0, 0, 0, 0, 0], 10 | [1, 1, 0, 0, 0, 0], 11 | [0, 1, 0, 0, 0, 0], 12 | [0, 1, 1, 1, 1, 1], 13 | [0, 0, 0, 0, 0, 0] 14 | ] 15 | print(island_perimeter(grid)) 16 | -------------------------------------------------------------------------------- /0x1C-makefiles/main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - Entry point 5 | * 6 | * Return: Always 7 | */ 8 | int main(void) 9 | { 10 | print_school(); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x1C-makefiles/main.h: -------------------------------------------------------------------------------- 1 | #ifndef _MAIN_H_ 2 | #define _MAIN_H_ 3 | 4 | #include 5 | #include 6 | 7 | void print_school(void); 8 | 9 | #endif 10 | -------------------------------------------------------------------------------- /0x1C-makefiles/main.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x1C-makefiles/main.o -------------------------------------------------------------------------------- /0x1C-makefiles/school: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x1C-makefiles/school -------------------------------------------------------------------------------- /0x1C-makefiles/school.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adebayo-S/alx-low_level_programming/6ba8de050cc54cafb939454e272151877d6a030a/0x1C-makefiles/school.o -------------------------------------------------------------------------------- /0x1E-search_algorithms/0-linear.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * linear_search - searches for a value in an array of integers 5 | * using Linear search 6 | * @array: pointer to first element of the array 7 | * @size: number of elements in array 8 | * @value: value to search 9 | * 10 | * Return: 1st index where value is located or -1 on NULL 11 | * array input or if value is absent 12 | */ 13 | int linear_search(int *array, size_t size, int value) 14 | { 15 | size_t i; 16 | 17 | if (!array || size <= 0) 18 | return (-1); 19 | 20 | i = 0; 21 | while (i < size) 22 | { 23 | printf("Value checked array[%ld] = [%d]\n", i, array[i]); 24 | if (array[i] == value) 25 | return (i); 26 | i++; 27 | } 28 | return (-1); 29 | } 30 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "search_algos.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always EXIT_SUCCESS 9 | */ 10 | int main(void) 11 | { 12 | int array[] = { 13 | 10, 1, 42, 3, 4, 42, 6, 7, -1, 9 14 | }; 15 | size_t size = sizeof(array) / sizeof(array[0]); 16 | 17 | printf("Found %d at index: %d\n\n", 3, linear_search(array, size, 3)); 18 | printf("Found %d at index: %d\n\n", 42, linear_search(array, size, 42)); 19 | printf("Found %d at index: %d\n", 999, linear_search(array, size, 999)); 20 | return (EXIT_SUCCESS); 21 | } 22 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "search_algos.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always EXIT_SUCCESS 9 | */ 10 | int main(void) 11 | { 12 | int array[] = { 13 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 14 | }; 15 | size_t size = sizeof(array) / sizeof(array[0]); 16 | 17 | printf("Found %d at index: %d\n\n", 2, binary_search(array, size, 2)); 18 | printf("Found %d at index: %d\n\n", 5, binary_search(array, 5, 5)); 19 | printf("Found %d at index: %d\n", 999, binary_search(array, size, 999)); 20 | return (EXIT_SUCCESS); 21 | } 22 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/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 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/README.md: -------------------------------------------------------------------------------- 1 | # 0x1E-search_algorithms 2 | --------------------------------------------------------------------------------