├── 0x00-hello_world ├── .101-quote.c.swk ├── .101-quote.c.swl ├── .101-quote.c.swm ├── .101-quote.c.swn ├── .101-quote.c.swo ├── .101-quote.c.swp ├── .101-quote.c101-quote.c.swp ├── .6-size.c.swp ├── 0-preprocessor ├── 1-compiler ├── 100-intel ├── 101-quote.c ├── 2-assembler ├── 3-name ├── 4-puts.c ├── 5-printf.c ├── 6-size.c ├── @! └── README.md ├── 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 ├── README.mee ├── READMEmd ├── _putchar.c ├── before ├── main.h └── vi ├── 0x03-debugging ├── 0-main.c ├── 1-go.c ├── 1-main.c ├── 2-largest_number.c ├── 3-print_remaining_days.c ├── README.md └── main.h ├── 0x04-more_functions_nested_loops ├── 0-isupper.c ├── 1-isdigit.c ├── 10-print_triangle.c ├── 100-prime_factor.c ├── 101-print_number.c ├── 2-mul.c ├── 3-print_numbers.c ├── 4-print_most_numbers.c ├── 5-more_numbers.c ├── 6-print_line.c ├── 7-print_diagonal.c ├── 8-print_square.c ├── 9-fizz_buzz.c ├── README.md └── main.h ├── 0x05-pointers_arrays_strings ├── 0-reset_to_98.c ├── 1-swap.c ├── 100-atoi.c ├── 101-keygen.c ├── 2-strlen.c ├── 3-puts.c ├── 4-print_rev.c ├── 5-rev_string.c ├── 6-puts2.c ├── 7-puts_half.c ├── 8-print_array.c ├── 9-strcpy.c ├── README.md └── main.h ├── 0x06-pointers_arrays_strings ├── 0-strcat.c ├── 1-strncat.c ├── 100-rot13.c ├── 101-print_number.c ├── 102-magic.c ├── 103-infinite_add.c ├── 104-print_buffer.c ├── 2-strncpy.c ├── 3-strcmp.c ├── 4-rev_array.c ├── 5-string_toupper.c ├── 6-cap_string.c ├── 7-leet.c ├── README.md └── main.h ├── 0x07-pointers_arrays_strings ├── 0-memset.c ├── 1-memcpy.c ├── 100-set_string.c ├── 101-crackme_password ├── 2-strchr.c ├── 3-strspn.c ├── 4-strpbrk.c ├── 5-strstr.c ├── 7-print_chessboard.c ├── 8-print_diagsums.c ├── README.md └── main.h ├── 0x08-recursion ├── 0-puts_recursion.c ├── 1-print_rev_recursion.c ├── 100-is_palindrome.c ├── 101-wildcmp.c ├── 2-strlen_recursion.c ├── 3-factorial.c ├── 4-pow_recursion.c ├── 5-sqrt_recursion.c ├── 6-is_prime_number.c ├── README.md └── main.h ├── 0x09-static_libraries ├── 0-isupper.c ├── 0-isupper.o ├── 0-memset.c ├── 0-memset.o ├── 0-strcat.c ├── 0-strcat.o ├── 1-isdigit.c ├── 1-isdigit.o ├── 1-memcpy.c ├── 1-memcpy.o ├── 1-strncat.c ├── 1-strncat.o ├── 100-atoi.c ├── 100-atoi.o ├── 2-strchr.c ├── 2-strchr.o ├── 2-strlen.c ├── 2-strlen.o ├── 2-strncpy.c ├── 2-strncpy.o ├── 3-islower.c ├── 3-islower.o ├── 3-puts.c ├── 3-puts.o ├── 3-strcmp.c ├── 3-strcmp.o ├── 3-strspn.c ├── 3-strspn.o ├── 4-isalpha.c ├── 4-isalpha.o ├── 4-strpbrk.c ├── 4-strpbrk.o ├── 5-strstr.c ├── 5-strstr.o ├── 6-abs.c ├── 6-abs.o ├── 9-strcpy.c ├── 9-strcpy.o ├── README.md ├── _putchar.c ├── _putchar.o ├── create_static_lib.sh ├── liball.a ├── libmy ├── libmy.a └── main.h ├── 0x0A-argc_argv ├── 0-whatsmyname.c ├── 1-args.c ├── 100-change.c ├── 2-args.c ├── 3-mul.c ├── 4-add.c └── README.md ├── 0x0B-malloc_free ├── 0-create_array.c ├── 1-strdup.c ├── 100-argstostr.c ├── 101-strtow.c ├── 2-str_concat.c ├── 3-alloc_grid.c ├── 4-free_grid.c ├── README.md └── main.h ├── 0x0C-more_malloc_free ├── 0-malloc_checked.c ├── 1-string_nconcat.c ├── 100-realloc.c ├── 101-mul ├── 101-mul.c ├── 2-calloc.c ├── 3-array_range.c ├── README.md └── main.h ├── 0x0D-preprocessor ├── 0-object_like_macro.h ├── 1-pi.h ├── 2-main.c ├── 3-function_like_macro.h ├── 4-sum.h └── README.md ├── 0x0E-structures_typedef ├── 1-init_dog.c ├── 2-print_dog.c ├── 4-new_dog.c ├── 5-free_dog.c ├── README.md └── dog.h ├── 0x0F-function_pointers ├── 0-print_name.c ├── 1-array_iterator.c ├── 100-main_opcodes.c ├── 2-int_index.c ├── 3-calc.h ├── 3-get_op_func.c ├── 3-main.c ├── 3-op_functions.c ├── README.md └── function_pointers.h ├── 0x10-variadic_functions ├── 0-sum_them_all.c ├── 1-print_numbers.c ├── 2-print_strings.c ├── 3-print_all.c ├── README.md └── variadic_functions.h ├── 0x12-singly_linked_lists ├── 0-print_list.c ├── 1-list_len.c ├── 100-first.c ├── 101-hello_holberton.asm ├── 2-add_node.c ├── 3-add_node_end.c ├── 4-free_list.c ├── README.md └── lists.h ├── 0x13-more_singly_linked_lists ├── 0-print_listint.c ├── 1-listint_len.c ├── 10-delete_nodeint.c ├── 100-reverse_listint.c ├── 101-print_listint_safe.c ├── 102-free_listint_safe.c ├── 103-find_loop.c ├── 2-add_nodeint.c ├── 3-add_nodeint_end.c ├── 4-free_listint.c ├── 5-free_listint2.c ├── 6-pop_listint.c ├── 7-get_nodeint.c ├── 8-sum_listint.c ├── 9-insert_nodeint.c ├── README.md └── lists.h ├── 0x14-bit_manipulation ├── .101-password.swo ├── .101-password.swp ├── 0-binary_to_uint.c ├── 1-print_binary.c ├── 100-get_endianness.c ├── 101-password ├── 2-get_bit.c ├── 3-set_bit.c ├── 4-clear_bit.c ├── 5-flip_bits.c ├── README.md └── main.h ├── 0x15-file_io ├── 0-read_textfile.c ├── 1-create_file.c ├── 100-elf_header.c ├── 2-append_text_to_file.c ├── 3-cp.c ├── README.md └── main.h ├── 0x17-doubly_linked_lists ├── 0-print_dlistint.c ├── 1-dlistint_len.c ├── 100-password ├── 102-result ├── 103-keygen.c ├── 2-add_dnodeint.c ├── 3-add_dnodeint_end.c ├── 4-free_dlistint.c ├── 5-get_dnodeint.c ├── 6-sum_dlistint.c ├── 7-insert_dnodeint.c ├── 8-delete_dnodeint.c ├── README.md ├── keygen5 └── lists.h ├── 0x18-dynamic_libraries ├── 1-create_dynamic_lib.sh ├── 100-operations.so ├── 101-make_me_win.sh ├── README.md ├── libdynamic.so ├── main.h └── preload.so ├── 0x1A-hash_tables ├── 0-hash_table_create.c ├── 1-djb2.c ├── 100-sorted_hash_table.c ├── 2-key_index.c ├── 3-hash_table_set.c ├── 4-hash_table_get.c ├── 5-hash_table_print.c ├── 6-hash_table_delete.c ├── README.md └── hash_tables.h ├── 0x1C-makefiles ├── 0-Makefile ├── 1-Makefile ├── 100-Makefile ├── 2-Makefile ├── 3-Makefile ├── 4-Makefile ├── 5-island_perimeter.py ├── README.md ├── m.h ├── main.c └── school.c ├── 0x1E-search_algorithms ├── 0-linear.c ├── 1-binary.c ├── 100-jump.c ├── 101-O ├── 102-interpolation.c ├── 103-exponential.c ├── 104-advanced_binary.c ├── 105-jump_list.c ├── 106-linear_skip.c ├── 107-O ├── 108-O ├── 2-O ├── 3-O ├── 4-O ├── 5-O ├── 6-O ├── README.md └── search_algos.h └── README.md /0x00-hello_world/.101-quote.c.swk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tecnophille/alx-low_level_programming/0e22613fb01cad5635f187c402011ff726386b2e/0x00-hello_world/.101-quote.c.swk -------------------------------------------------------------------------------- /0x00-hello_world/.101-quote.c.swl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tecnophille/alx-low_level_programming/0e22613fb01cad5635f187c402011ff726386b2e/0x00-hello_world/.101-quote.c.swl -------------------------------------------------------------------------------- /0x00-hello_world/.101-quote.c.swm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tecnophille/alx-low_level_programming/0e22613fb01cad5635f187c402011ff726386b2e/0x00-hello_world/.101-quote.c.swm -------------------------------------------------------------------------------- /0x00-hello_world/.101-quote.c.swn: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tecnophille/alx-low_level_programming/0e22613fb01cad5635f187c402011ff726386b2e/0x00-hello_world/.101-quote.c.swn -------------------------------------------------------------------------------- /0x00-hello_world/.101-quote.c.swo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tecnophille/alx-low_level_programming/0e22613fb01cad5635f187c402011ff726386b2e/0x00-hello_world/.101-quote.c.swo -------------------------------------------------------------------------------- /0x00-hello_world/.101-quote.c.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tecnophille/alx-low_level_programming/0e22613fb01cad5635f187c402011ff726386b2e/0x00-hello_world/.101-quote.c.swp -------------------------------------------------------------------------------- /0x00-hello_world/.101-quote.c101-quote.c.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tecnophille/alx-low_level_programming/0e22613fb01cad5635f187c402011ff726386b2e/0x00-hello_world/.101-quote.c101-quote.c.swp -------------------------------------------------------------------------------- /0x00-hello_world/.6-size.c.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tecnophille/alx-low_level_programming/0e22613fb01cad5635f187c402011ff726386b2e/0x00-hello_world/.6-size.c.swp -------------------------------------------------------------------------------- /0x00-hello_world/0-preprocessor: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -E $CFILE -o c 3 | -------------------------------------------------------------------------------- /0x00-hello_world/1-compiler: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -c $CFILE 3 | -------------------------------------------------------------------------------- /0x00-hello_world/100-intel: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -S -masm=intel $CFILE 3 | -------------------------------------------------------------------------------- /0x00-hello_world/101-quote.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main - Entry point 5 | * 6 | * Return: Always 1 (Success) 7 | */ 8 | int main(void) 9 | { 10 | <<<<<<< HEAD 11 | write(2, "and that piece of art is useful\" - Dora Korpar, 2015-10-19\n", 59); 12 | ======= 13 | write(2,"and that piece of art is useful\" -Dora Korper, 2015-10-19\n", 59); 14 | >>>>>>> 5a4ea96da9f2d6ab1c0bf39dbb15cf26ca9a2699 15 | return (1); 16 | } 17 | -------------------------------------------------------------------------------- /0x00-hello_world/2-assembler: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -S $CFILE 3 | -------------------------------------------------------------------------------- /0x00-hello_world/3-name: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc $CFILE -o cisfun 3 | -------------------------------------------------------------------------------- /0x00-hello_world/4-puts.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * * main - Entry point 4 | * * 5 | * * Return: Always 0 (Success) 6 | * * 7 | */ 8 | int main(void) 9 | { 10 | puts("\"Programming is like building a multilingual puzzle"); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x00-hello_world/5-printf.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * * main - Entry point 4 | * * 5 | * * Return: Always 0 (Success) 6 | * * 7 | * 8 | */ 9 | int main(void) 10 | { 11 | printf("with proper grammar, but the outcome is a piece of art,\n"); 12 | return (0) 13 | ; 14 | } 15 | -------------------------------------------------------------------------------- /0x00-hello_world/6-size.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * * main - Entry point 4 | * * 5 | * * Return: Always 0 (Success) 6 | */ 7 | int main(void) 8 | { 9 | printf("Size of a char: %d byte(s)\n", sizeof(char)); 10 | printf("Size of an int: %d byte(s)\n", sizeof(int)); 11 | printf("Size of a long int: %d byte(s)\n", sizeof(long int)); 12 | printf("Size of a long long int: %d byte(s)\n", sizeof(long long int)); 13 | printf("Size of a float: %d byte(s)\n", sizeof(float)); 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x00-hello_world/@!: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -S -masm=intel $CFILE 3 | -------------------------------------------------------------------------------- /0x00-hello_world/README.md: -------------------------------------------------------------------------------- 1 | 0x00-hello_world 2 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/0-positive_or_negative.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /** 6 | * main - Determines if a number is positive, negative or zero. 7 | * 8 | * Return: Always 0 (Success) 9 | */ 10 | int main(void) 11 | { 12 | int n; 13 | 14 | srand(time(0)); 15 | n = rand() - RAND_MAX / 2; 16 | if (n > 0) 17 | { 18 | printf("%d is positive\n", n); 19 | } 20 | else if (n == 0) 21 | { 22 | printf("%d is zero\n", n); 23 | } 24 | else 25 | { 26 | printf("%d is negative\n", n); 27 | } 28 | return (0); 29 | } 30 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/1-last_digit.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /** 6 | * main - Determines either greater than 5, is less than 6, or is 0 7 | * 8 | * Return: Always 0 (Success) 9 | */ 10 | int main(void) 11 | { 12 | int n, l; 13 | 14 | srand(time(0)); 15 | n = rand() - RAND_MAX / 2; 16 | l = n % 10; 17 | if (l > 5) 18 | { 19 | printf("Last digit of %d is %d and is greater than 5\n", n, l); 20 | } 21 | else if (l == 0) 22 | { 23 | printf("Last digit of %d is %d and is 0\n", n, l); 24 | } 25 | else 26 | { 27 | printf("Last digit of %d is %d and is less than 6 and not 0\n", n, l); 28 | } 29 | return (0); 30 | } 31 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/100-print_comb3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Prints numbers between 00 to 89. 5 | * 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | int i, e; 11 | 12 | i = 48; 13 | e = 48; 14 | 15 | while (e < 58) 16 | { 17 | i = 48; 18 | while (i < 58) 19 | { 20 | if (e != i && e < i) 21 | { 22 | putchar(e); 23 | putchar(i); 24 | if (i == 57 && e == 56) 25 | { 26 | break; 27 | } 28 | putchar(','); 29 | putchar(' '); 30 | } 31 | i++; 32 | } 33 | e++; 34 | } 35 | putchar('\n'); 36 | return (0); 37 | } 38 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/101-print_comb4.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Prints numbers between 012 to 789. 5 | * 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | int i, e, g; 11 | 12 | i = 48; 13 | e = 48; 14 | g = 48; 15 | 16 | while (e < 58) 17 | { 18 | i = 48; 19 | while (i < 58) 20 | { 21 | g = 48; 22 | while (g < 58) 23 | { 24 | if (e != i && e != g && i != g && e < i && i < g) 25 | { 26 | putchar(e); 27 | putchar(i); 28 | putchar(g); 29 | if (i == 56 && e == 55 && g == 57) 30 | { 31 | break; 32 | } 33 | putchar(','); 34 | putchar(' '); 35 | } 36 | g++; 37 | } 38 | i++; 39 | } 40 | e++; 41 | } 42 | putchar('\n'); 43 | return (0); 44 | } 45 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/102-print_comb5.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Prints numbers between 00 to 99. 5 | * 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | int i, e, g, h, op1, op2; 11 | 12 | i = e = g = h = 48; 13 | while (h < 58) 14 | { 15 | g = 48; 16 | while (g < 58) 17 | { 18 | e = 48; 19 | while (e < 58) 20 | { 21 | i = 48; 22 | while (i < 58) 23 | { 24 | op1 = (h * 10) + g; 25 | op2 = (e * 10) + i; 26 | if (op1 < op2) 27 | { 28 | putchar(h); 29 | putchar(g); 30 | putchar(' '); 31 | putchar(e); 32 | putchar(i); 33 | if (h == 57 && g == 56 && e == 57 && i == 57) 34 | break; 35 | putchar(','); 36 | putchar(' '); 37 | } 38 | i++; 39 | } 40 | e++; 41 | } 42 | g++; 43 | } 44 | h++; 45 | } 46 | putchar('\n'); 47 | return (0); 48 | } 49 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/2-print_alphabet.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * * main - Prints the alphabet. 5 | * * 6 | * * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | char alp[26] = "abcdefghijklmnopqrstuvwxyz"; 11 | int i; 12 | 13 | for (i = 0; i < 26; i++) 14 | 15 | { 16 | putchar(alp[i]); 17 | } 18 | putchar('\n'); 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/3-print_alphabets.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Prints the alphabet. 5 | * 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | char alp[52] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; 11 | int i; 12 | 13 | for (i = 0; i < 52; i++) 14 | { 15 | putchar(alp[i]); 16 | } 17 | putchar('\n'); 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/4-print_alphabt.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /** 6 | * main - Prints the alphabet without q and e. 7 | * 8 | * Return: Always 0 (Success) 9 | */ 10 | int main(void) 11 | { 12 | int i; 13 | 14 | for (i = 97; i < 123; i++) 15 | { 16 | if (i != 101 && i != 113) 17 | { 18 | putchar(i); 19 | } 20 | } 21 | putchar('\n'); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/5-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Prints numbers from 0 to 9. 5 | * 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | int i; 11 | 12 | for (i = 0; i < 10; i++) 13 | { 14 | printf("%d", i); 15 | } 16 | putchar('\n'); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/6-print_numberz.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Prints numbers between 0 to 9. 5 | * 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | int i; 11 | 12 | for (i = 48; i < 58; i++) 13 | { 14 | putchar(i); 15 | } 16 | putchar('\n'); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/7-print_tebahpla.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Prints reversed alphabet. 5 | * 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | int i; 11 | 12 | for (i = 122; i > 96; i--) 13 | { 14 | putchar(i); 15 | } 16 | putchar('\n'); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/8-print_base16.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Prints numbers between 0 to 9 and letters between a to f. 5 | * 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | int i; 11 | 12 | for (i = 48; i < 58; i++) 13 | { 14 | putchar(i); 15 | } 16 | for (i = 97; i < 103; i++) 17 | { 18 | putchar(i); 19 | } 20 | putchar('\n'); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/9-print_comb.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Prints numbers between 0 to 9 with commas and spaces. 5 | * 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | int i; 11 | 12 | for (i = 48; i < 58; i++) 13 | { 14 | putchar(i); 15 | if (i != 57) 16 | { 17 | putchar(','); 18 | putchar(' '); 19 | } 20 | } 21 | putchar('\n'); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/README.md: -------------------------------------------------------------------------------- 1 | # Variables, if, else and while 2 | 3 | Project done during **ALX Software Engineering Programme** at **ALX**. It aims to learn about arithmetic operators, relational operators, boolean operators, comments, declaring variables and loops in **C language**. 4 | 5 | ## Technologies 6 | * C files are compiled using `gcc` 7 | * C files are written according to the BETTY CODING standard 8 | * Tested on Ubuntu 20.04 LTS 9 | 10 | ## Files 11 | All of the following files are programs written in C: 12 | 13 | | Filename | Description | 14 | | -------- | ----------- | 15 | | `0-positive_or_negative.c` | Prints `is positive`, `is zero` or `is negative` when a randomly generated number is any of these cases | 16 | | `1-last_digit.c` | Prints the last digit of a randomly generated number | 17 | | `2-print_alphabet.c` | Prints the alphabet in lowercase | 18 | | `3-print_alphabets.c` | Prints the alphabet in lowercase, and then in uppercase | 19 | | `4-print_alphabet.c` | Prints the alphabet in lowercase, except `q` and `e` letters | 20 | | `5-print_numbers.c` | Prints all single digit numbers of base 10 starting from `0` | 21 | | `6-print_numberz.c` | Prints all single digit numbers of base 10 starting from `0`, without using `char` variables | 22 | | `7-print_tebahpla.c` | Prints the lowercase alphabet in reverse | 23 | | `8-print_base16.c` | Prints all the numbers of base 16 in lowercase | 24 | | `9-print_comb.c` | Prints all possible combinations of single-digit numbers | 25 | | `10-print_comb2.c` | Prints the numbers from 00 to 99 | 26 | | `100-print_comb3.c` | Prints all possible different combinations of two digits, separated by `,` | 27 | | `101-print_comb4.c` | Prints all possible different combinations of three digits, separated by `,` | 28 | | `102-print_comb5.c` | Prints all possible combinations of two two-digit numbers | 29 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/0-putchar.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - Prints _putchar as a message. 5 | * 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | _putchar('_'); 11 | _putchar('p'); 12 | _putchar('u'); 13 | _putchar('t'); 14 | _putchar('c'); 15 | _putchar('h'); 16 | _putchar('a'); 17 | _putchar('r'); 18 | _putchar('\n'); 19 | 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/1-alphabet.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_alphabet - Prints the alphabet in lowercase. 5 | * 6 | * Return: Always 0 (Success) 7 | */ 8 | void print_alphabet(void) 9 | { 10 | int i; 11 | 12 | for (i = 97; i <= 122; i++) 13 | { 14 | _putchar(i); 15 | } 16 | _putchar('\n'); 17 | } 18 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/10-add.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * add - Adds two integers and returns 5 | * the result. 6 | * 7 | * @a: number one. 8 | * @b: number two. 9 | * 10 | * Return: Add of number one and number two. 11 | */ 12 | int add(int a, int b) 13 | { 14 | return (a + b); 15 | } 16 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/100-times_table.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_times_table - Prints the n times table 5 | * 6 | * @n: number times table (0 < n <= 15) 7 | * 8 | * Return: no return 9 | */ 10 | void print_times_table(int n) 11 | { 12 | int a, b, op; 13 | 14 | if (n >= 0 && n <= 15) 15 | { 16 | for (a = 0; a <= n; a++) 17 | { 18 | _putchar(48); 19 | for (b = 1; b <= n; b++) 20 | { 21 | op = a * b; 22 | _putchar(44); 23 | _putchar(32); 24 | if (op <= 9) 25 | { 26 | _putchar(32); 27 | _putchar(32); 28 | _putchar(op + 48); 29 | } 30 | else if (op <= 99) 31 | { 32 | _putchar(32); 33 | _putchar((op / 10) + 48); 34 | _putchar((op % 10) + 48); 35 | } 36 | else 37 | { 38 | _putchar(((op / 100) % 10) + 48); 39 | _putchar(((op / 10) % 10) + 48); 40 | _putchar((op % 10) + 48); 41 | } 42 | } 43 | _putchar('\n'); 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/101-natural.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - computes and prints the sum of all the multiples 5 | * of 3 or 5 below 1024 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | unsigned long int sum3, sum5, sum; 11 | int i; 12 | 13 | sum3 = 0; 14 | sum5 = 0; 15 | sum = 0; 16 | 17 | for (i = 0; i < 1024; ++i) 18 | { 19 | if ((i % 3) == 0) 20 | { 21 | sum3 = sum3 + i; 22 | } else if ((i % 5) == 0) 23 | { 24 | sum5 = sum5 + i; 25 | } 26 | } 27 | sum = sum3 + sum5; 28 | printf("%lu\n", sum); 29 | return (0); 30 | } 31 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/102-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Prints the add of the Fibonacci numbers 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | int c; 11 | long int n1, n2, fn; 12 | 13 | n1 = 1; 14 | n2 = 2; 15 | printf("%ld, %ld", n1, n2); 16 | for (c = 0; c < 48; c++) 17 | { 18 | fn = n1 + n2; 19 | printf(", %ld", fn); 20 | n1 = n2; 21 | n2 = fn; 22 | } 23 | printf("\n"); 24 | return (0); 25 | } 26 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/103-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Prints the add of the even-valued 5 | * fibonacci numbers. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | long int n1, n2, fn, afn; 12 | 13 | n1 = 1; 14 | n2 = 2; 15 | fn = afn = 0; 16 | while (fn <= 4000000) 17 | { 18 | fn = n1 + n2; 19 | n1 = n2; 20 | n2 = fn; 21 | if ((n1 % 2) == 0) 22 | { 23 | afn += n1; 24 | } 25 | } 26 | printf("%ld\n", afn); 27 | return (0); 28 | } 29 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/104-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Prints the first 98 Fibonacci numbers 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | int c, boolean, boolean2; 11 | long int n1, n2, fn, fn2, n11, n22; 12 | 13 | n1 = 1; 14 | n2 = 2; 15 | boolean = boolean2 = 1; 16 | printf("%ld, %ld", n1, n2); 17 | for (c = 0; c < 96; c++) 18 | { 19 | if (boolean) 20 | { 21 | fn = n1 + n2; 22 | printf(", %ld", fn); 23 | n1 = n2; 24 | n2 = fn; 25 | } 26 | else 27 | { 28 | if (boolean2) 29 | { 30 | n11 = n1 % 1000000000; 31 | n22 = n2 % 1000000000; 32 | n1 = n1 / 1000000000; 33 | n2 = n2 / 1000000000; 34 | boolean2 = 0; 35 | } 36 | fn2 = (n11 + n22); 37 | fn = n1 + n2 + (fn2 / 1000000000); 38 | printf(", %ld", fn); 39 | printf("%ld", fn2 % 1000000000); 40 | n1 = n2; 41 | n11 = n22; 42 | n2 = fn; 43 | n22 = (fn2 % 1000000000); 44 | } 45 | if (((n1 + n2) < 0) && boolean == 1) 46 | boolean = 0; 47 | } 48 | printf("\n"); 49 | return (0); 50 | } 51 | -------------------------------------------------------------------------------- /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. 6 | * 7 | * @n: input number. 8 | * 9 | * Return: no return. 10 | */ 11 | void print_to_98(int n) 12 | { 13 | if (n > 98) 14 | { 15 | for (; n > 98; n--) 16 | { 17 | printf("%d, ", n); 18 | } 19 | } 20 | else if (n < 98) 21 | { 22 | for (; n < 98; n++) 23 | { 24 | printf("%d, ", n); 25 | } 26 | } 27 | printf("%d\n", n); 28 | } 29 | -------------------------------------------------------------------------------- /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 (Success) 7 | */ 8 | void print_alphabet_x10(void) 9 | { 10 | int a, b; 11 | 12 | for (a = 0; a <= 9; a++) 13 | { 14 | for (b = 97; b <= 122; b++) 15 | { 16 | _putchar(b); 17 | } 18 | _putchar('\n'); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/3-islower.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _islower - Shows 1 if the input is a 5 | * lowercase character. Another cases, shows 6 | * 0 7 | * 8 | * @c: The character in ASCII code 9 | * 10 | * Return: 1 for lowercase character. 0 for the rest. 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 - Shows 1 if the input is a 5 | * letter Another cases, shows 0 6 | * 7 | * @c: The character in ASCII code 8 | * 9 | * Return: 1 for letters. 0 for the rest. 10 | */ 11 | int _isalpha(int c) 12 | { 13 | if ((c >= 97 && c <= 122) || (c >= 65 && c <= 90)) 14 | { 15 | return (1); 16 | } 17 | else 18 | { 19 | return (0); 20 | } 21 | _putchar('\n'); 22 | } 23 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/5-sign.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_sign - Determines if the input number 5 | * greater, iqual or less than zero. 6 | * 7 | * @n: The input number as an integer. 8 | * 9 | * Return: 1 is greater than zero. 0 is zero. 10 | * -1 is less than zero. 11 | */ 12 | int print_sign(int n) 13 | { 14 | if (n > 0) 15 | { 16 | _putchar(43); 17 | return (1); 18 | } 19 | else if (n < 0) 20 | { 21 | _putchar(45); 22 | return (-1); 23 | } 24 | else 25 | { 26 | _putchar(48); 27 | return (0); 28 | } 29 | _putchar('\n'); 30 | } 31 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/6-abs.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _abs - Computes the absolute value 5 | * of an integer. 6 | * 7 | * @i: input number as an integer. 8 | * 9 | * Return: absolute value 10 | */ 11 | int _abs(int i) 12 | { 13 | if (i >= 0) 14 | { 15 | return (i); 16 | } 17 | else 18 | { 19 | return (i * -1); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /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 | int print_last_digit(int n) 12 | { 13 | int l; 14 | 15 | l = n % 10; 16 | if (l < 0) 17 | { 18 | _putchar(-l + 48); 19 | return (-l); 20 | } 21 | else 22 | { 23 | _putchar(l + 48); 24 | return (l); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/8-24_hours.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * jack_bauer - Prints the minutes of a day 5 | * 6 | * Return: no return 7 | */ 8 | void jack_bauer(void) 9 | { 10 | int a, b, c, d; 11 | 12 | for (a = 48; a <= 50; a++) 13 | { 14 | for (b = 48; b <= 57; b++) 15 | { 16 | for (c = 48; c <= 53; c++) 17 | { 18 | for (d = 48; d <= 57; d++) 19 | { 20 | if (a >= 50 && b >= 52) 21 | break; 22 | _putchar(a); 23 | _putchar(b); 24 | _putchar(58); 25 | _putchar(c); 26 | _putchar(d); 27 | _putchar('\n'); 28 | } 29 | } 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/9-times_table.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * times_table - Prints the 9 times table 5 | * 6 | * Return: no return 7 | */ 8 | void times_table(void) 9 | { 10 | int a, b, op; 11 | 12 | for (a = 0; a <= 9; a++) 13 | { 14 | _putchar(48); 15 | for (b = 1; b <= 9; b++) 16 | { 17 | op = a * b; 18 | _putchar(44); 19 | _putchar(32); 20 | if (op <= 9) 21 | { 22 | _putchar(32); 23 | _putchar(op + 48); 24 | } 25 | else 26 | { 27 | _putchar((op / 10) + 48); 28 | _putchar((op % 10) + 48); 29 | } 30 | } 31 | _putchar('\n'); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/README.md: -------------------------------------------------------------------------------- 1 | Ujjh 2 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/README.mee: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tecnophille/alx-low_level_programming/0e22613fb01cad5635f187c402011ff726386b2e/0x02-functions_nested_loops/README.mee -------------------------------------------------------------------------------- /0x02-functions_nested_loops/READMEmd: -------------------------------------------------------------------------------- 1 | 0x02-functions_nested_loops 2 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/_putchar.c: -------------------------------------------------------------------------------- 1 | Task 0 2 | 3 | #include "main.h" 4 | /** 5 | *main - Entry point 6 | * 7 | *Return: nothing 8 | */ 9 | 10 | 11 | int main(void) 12 | { 13 | _putchar('_'); 14 | _putchar('p'); 15 | _putchar('u'); 16 | _putchar('t'); 17 | _putchar('c'); 18 | _putchar('h'); 19 | _putchar('a'); 20 | _putchar('r'); 21 | 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/before: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tecnophille/alx-low_level_programming/0e22613fb01cad5635f187c402011ff726386b2e/0x02-functions_nested_loops/before -------------------------------------------------------------------------------- /0x02-functions_nested_loops/main.h: -------------------------------------------------------------------------------- 1 | #ifndef _main_h_ 2 | #define _main_h_ 3 | 4 | int _putchar(char); 5 | void print_alphabet(void); 6 | void print_alphabet_x10(void); 7 | int _islower(int c); 8 | int _isalpha(int c); 9 | int print_sign(int n); 10 | int _abs(int); 11 | int print_last_digit(int); 12 | void jack_bauer(void); 13 | void times_table(void); 14 | int add(int, int); 15 | void print_to_98(int n); 16 | void print_times_table(int n); 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/vi: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Prints natural numbers below 1024 that are 5 | * multiplies of 3 or 5 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int a, b; 12 | 13 | for (a = 1; a < 1024; a++) 14 | { 15 | if ((a % 3) == 0 || (a % 5) == 0) 16 | b += a; 17 | } 18 | printf("%d\n", b); 19 | return (0); 20 | } 21 | 22 | -------------------------------------------------------------------------------- /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-go.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/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 % 100 == 0) && (year % 400 == 0))) 16 | { 17 | if (month > 2 && day >= 60) 18 | { 19 | day++; 20 | } 21 | 22 | printf("Day of the year: %d\n", day); 23 | printf("Remaining days: %d\n", 366 - day); 24 | } 25 | else 26 | { 27 | if (month == 2 && day == 60) 28 | { 29 | printf("Invalid date: %02d/%02d/%04d\n", month, day - 31, year); 30 | } 31 | else 32 | { 33 | printf("Day of the year: %d\n", day); 34 | printf("Remaining days: %d\n", 365 - day); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /0x03-debugging/README.md: -------------------------------------------------------------------------------- 1 | # Debugging 2 | 3 | Project done during **Full Stack Software Engineering studies** at **ALX SE**. Aims at learning how to debug in **C language**. 4 | 5 | ## Technologies 6 | * C files are compiled using `gcc` 7 | * C files are written according to the C90 standard 8 | * Tested on Ubuntu 20.04 LTS 9 | 10 | ## Files 11 | All of the following files are programs written in C: 12 | 13 | | Filename | Description | 14 | | -------- | ----------- | 15 | | `0-main.c` | Test that function `positive_or_negative()` gives the correct output when given a case of `0` | 16 | | `1-main.c` | Fixed program to the infinite loop | 17 | | `2-largest_number.c` | Prints the largest of three integers | 18 | | `3-print_remaining_days.c` | Converts a date to the day of year and determines how many days are left in the year | 19 | | `main.h` | Header file | 20 | -------------------------------------------------------------------------------- /0x03-debugging/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | #include 5 | 6 | void positive_or_negative(int i); 7 | int largest_number(int a, int b, int c); 8 | int convert_day(int month, int day); 9 | void print_remaining_days(int month, int day, int year); 10 | 11 | #endif /* MAIN_H */ 12 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/0-isupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isupper - checks if parameter is an uppercase character. 5 | * @c: input character. 6 | * Return: 1 if is an uppercase character, 0 in other case. 7 | */ 8 | int _isupper(int c) 9 | { 10 | if (c >= 65 && c <= 90) 11 | { 12 | return (1); 13 | } 14 | else 15 | { 16 | return (0); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/1-isdigit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isdigit - checks if parameter is a number between 0 to 9. 5 | * @c: input number. 6 | * Return: 1 if is a number (0 to 9), 0 in other case. 7 | */ 8 | int _isdigit(int c) 9 | { 10 | if (c >= 48 && c <= 57) 11 | { 12 | return (1); 13 | } 14 | else 15 | { 16 | return (0); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/10-print_triangle.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_triangle - prints a triangle. 5 | * @size: numbers of lines. 6 | * Return: no return. 7 | */ 8 | void print_triangle(int size) 9 | { 10 | int i, j; 11 | 12 | for (i = 0; i < size; i++) 13 | { 14 | for (j = 1; j < (size - i); j++) 15 | _putchar(' '); 16 | for (j--; j < size; j++) 17 | _putchar(35); 18 | if (i < (size - 1)) 19 | _putchar('\n'); 20 | } 21 | _putchar('\n'); 22 | } 23 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/100-prime_factor.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints largest prime factor. 5 | * Return: Always 0. 6 | */ 7 | 8 | int main(void) 9 | { 10 | long int n, fp; 11 | 12 | n = 612852475143; 13 | for (fp = 2; fp <= n; fp++) 14 | { 15 | if (n % fp == 0) 16 | { 17 | n /= fp; 18 | fp--; 19 | } 20 | } 21 | printf("%ld\n", fp); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/101-print_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_number - prints an integer. 5 | * @n: input integer. 6 | * Return: no return. 7 | */ 8 | void print_number(int n) 9 | { 10 | unsigned int m, d, count; 11 | 12 | if (n < 0) 13 | { 14 | _putchar(45); 15 | m = n * -1; 16 | } 17 | else 18 | { 19 | m = n; 20 | } 21 | 22 | d = m; 23 | count = 1; 24 | 25 | while (d > 9) 26 | { 27 | d /= 10; 28 | count *= 10; 29 | } 30 | 31 | for (; count >= 1; count /= 10) 32 | { 33 | _putchar(((m / count) % 10) + 48); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/2-mul.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * mul - multiplies two integers. 5 | * @a: first number. 6 | * @b: second number. 7 | * Return: multiplication. 8 | */ 9 | int mul(int a, int b) 10 | { 11 | return (a * b); 12 | } 13 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/3-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_numbers - prints numbers between 0 to 9. 5 | * Return: no return. 6 | */ 7 | void print_numbers(void) 8 | { 9 | int ch; 10 | 11 | for (ch = 48; ch < 58; ch++) 12 | { 13 | _putchar(ch); 14 | } 15 | _putchar('\n'); 16 | } 17 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/4-print_most_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_most_numbers - prints numbers between 0 to 9 5 | * (neither 2 nor 4). 6 | * Return: no return. 7 | */ 8 | void print_most_numbers(void) 9 | { 10 | int ch; 11 | 12 | for (ch = 48; ch < 58; ch++) 13 | { 14 | if (ch != 50 && ch != 52) 15 | _putchar(ch); 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 numbers between 0 to 14 5 | * 10 times. 6 | * Return: no return. 7 | */ 8 | void more_numbers(void) 9 | { 10 | int i, ch; 11 | 12 | for (i = 0; i < 10; i++) 13 | { 14 | for (ch = 0; ch < 15; ch++) 15 | { 16 | if (ch >= 10) 17 | _putchar((ch / 10) + 48); 18 | _putchar((ch % 10) + 48); 19 | } 20 | _putchar('\n'); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/6-print_line.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_line - prints straight line n times. 5 | * @n: times straight line is printed. 6 | * Return: no return. 7 | */ 8 | void print_line(int n) 9 | { 10 | int i; 11 | 12 | for (i = 0; i < n; i++) 13 | { 14 | _putchar(95); 15 | } 16 | _putchar('\n'); 17 | } 18 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/7-print_diagonal.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_diagonal - prints diagonal line n times. 5 | * @n: times diagonal line is printed. 6 | * Return: no return. 7 | */ 8 | void print_diagonal(int n) 9 | { 10 | int i, j; 11 | 12 | for (i = 0; i < n; i++) 13 | { 14 | for (j = 0; j < i; j++) 15 | { 16 | _putchar(' '); 17 | } 18 | _putchar(92); 19 | if (i < (n - 1)) 20 | _putchar('\n'); 21 | } 22 | _putchar('\n'); 23 | } 24 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/8-print_square.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_square - prints hashes squares. 5 | * @size: size of the square. 6 | * Return: no return. 7 | */ 8 | void print_square(int size) 9 | { 10 | int i, j; 11 | 12 | for (i = 0; i < size; i++) 13 | { 14 | for (j = 0; j < size; j++) 15 | { 16 | _putchar(35); 17 | } 18 | if (i != size - 1) 19 | _putchar('\n'); 20 | } 21 | _putchar('\n'); 22 | } 23 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/9-fizz_buzz.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints Buzz each numbers of 3 and 5. 5 | * Return: Always 0. 6 | */ 7 | int main(void) 8 | { 9 | int n; 10 | 11 | n = 1; 12 | printf("%d", n); 13 | for (n = 2; n <= 100; n++) 14 | { 15 | if ((n % 3 == 0) && (n % 5 == 0)) 16 | { 17 | printf(" FizzBuzz"); 18 | } 19 | else if (n % 3 == 0) 20 | { 21 | printf(" Fizz"); 22 | } 23 | else if (n % 5 == 0) 24 | { 25 | printf(" Buzz"); 26 | } 27 | else 28 | { 29 | printf(" %d", n); 30 | } 31 | } 32 | printf("\n"); 33 | return (0); 34 | } 35 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/README.md: -------------------------------------------------------------------------------- 1 | # More functions, more nested loops 2 | 3 | Project done during **Full Stack Software Engineering** at **ALX SE**. Teaches about nested loops in **C language**. 4 | 5 | ## Technologies 6 | * C files are compiled using `gcc` 7 | * C files are written according to the Betty coding standard 8 | * Tested on Ubuntu 20.04 LTS 9 | 10 | ## Files 11 | All of the following files are programs written in C: 12 | 13 | | Filename | Description | 14 | | -------- | ----------- | 15 | | `0-isupper.c` | Checks for uppercase character | 16 | | `1-isdigit.c` | Checks for a digit (`0` through `9`) | 17 | | `2-mul.c` | Multiplies two integers | 18 | | `3-print_numbers.c` | Prints the numbers, from `0` to `9` | 19 | | `4-print_most_numbers.c` | Prints the numbers, from `0` to `9`, except `2` and `4` | 20 | | `5-more_numbers.c` | Prints 10 times the numbers, from `0` to `14` | 21 | | `6-print_line.c` | Draws a straight line in the terminal | 22 | | `7-print_diagonal.c` | Draws a diagonal line in the terminal | 23 | | `8-print_square.c` | Prints a sqaure | 24 | | `9-fizz_buzz.c` | Prints Fizz-Buzz test | 25 | | `10-print_triangle.c` | Prints a triangle | 26 | | `100-prime_factor.c` | Finds and prints the largest prime factor of the number `612852475143` | 27 | | `101-print_number.c` | Prints an integer | 28 | -------------------------------------------------------------------------------- /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); 9 | void print_most_numbers(void); 10 | void more_numbers(void); 11 | void print_line(int n); 12 | void print_diagonal(int n); 13 | void print_square(int size); 14 | void print_triangle(int size); 15 | void print_number(int n); 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/0-reset_to_98.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * reset_to_98 - takes a pointer to a parameter and updates its value. 5 | * @n: input integer. 6 | * Return: no return. 7 | */ 8 | void reset_to_98(int *n) 9 | { 10 | *n = 98; 11 | } 12 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/1-swap.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * swap_int - swaps the values of two integers. 5 | * @a: first integer. 6 | * @b: second integer. 7 | * Return: no return. 8 | */ 9 | void swap_int(int *a, int *b) 10 | { 11 | int c = *a; 12 | *a = *b; 13 | *b = c; 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: input string. 6 | * Return: integer. 7 | */ 8 | int _atoi(char *s) 9 | { 10 | unsigned int count = 0, size = 0, oi = 0, pn = 1, m = 1, i; 11 | 12 | while (*(s + count) != '\0') 13 | { 14 | if (size > 0 && (*(s + count) < '0' || *(s + count) > '9')) 15 | break; 16 | 17 | if (*(s + count) == '-') 18 | pn *= -1; 19 | 20 | if ((*(s + count) >= '0') && (*(s + count) <= '9')) 21 | { 22 | if (size > 0) 23 | m *= 10; 24 | size++; 25 | } 26 | count++; 27 | } 28 | 29 | for (i = count - size; i < count; i++) 30 | { 31 | oi = oi + ((*(s + i) - 48) * m); 32 | m /= 10; 33 | } 34 | return (oi * pn); 35 | } 36 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/101-keygen.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | /** 5 | * main - generates keygen. 6 | * Return: 0 Always. 7 | */ 8 | int main(void) 9 | { 10 | int r = 0, c = 0; 11 | time_t t; 12 | 13 | srand((unsigned int) time(&t)); 14 | while (c < 2772) 15 | { 16 | r = rand() % 128; 17 | if ((c + r) > 2772) 18 | break; 19 | c = c + r; 20 | printf("%c", r); 21 | } 22 | printf("%c\n", (2772 - c)); 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/2-strlen.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strlen - returns the length of a string. 5 | * @s: input string. 6 | * Return: length of a string. 7 | */ 8 | int _strlen(char *s) 9 | { 10 | int count = 0; 11 | 12 | while (*(s + count) != '\0') 13 | count++; 14 | return (count); 15 | } 16 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/3-puts.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _puts - prints a string, followed by a new line. 5 | * @str: input string. 6 | * Return: no return. 7 | */ 8 | void _puts(char *str) 9 | { 10 | int count = 0; 11 | 12 | while (count >= 0) 13 | { 14 | if (str[count] == '\0') 15 | { 16 | _putchar('\n'); 17 | break; 18 | } 19 | _putchar(str[count]); 20 | count++; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /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 | void print_rev(char *s) 9 | { 10 | int count = 0; 11 | 12 | while (count >= 0) 13 | { 14 | if (s[count] == '\0') 15 | break; 16 | count++; 17 | } 18 | 19 | for (count--; count >= 0; count--) 20 | _putchar(s[count]); 21 | _putchar('\n'); 22 | } 23 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/5-rev_string.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * rev_string - reverses a string. 5 | * @s: input string. 6 | * Return: no return. 7 | */ 8 | void rev_string(char *s) 9 | { 10 | int count = 0, i, j; 11 | char *str, temp; 12 | 13 | while (count >= 0) 14 | { 15 | if (s[count] == '\0') 16 | break; 17 | count++; 18 | } 19 | str = s; 20 | 21 | for (i = 0; i < (count - 1); i++) 22 | { 23 | for (j = i + 1; j > 0; j--) 24 | { 25 | temp = *(str + j); 26 | *(str + j) = *(str + (j - 1)); 27 | *(str + (j - 1)) = temp; 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /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 | * @str: input string. 6 | * Return: no return. 7 | */ 8 | void puts2(char *str) 9 | { 10 | int count = 0; 11 | 12 | while (count >= 0) 13 | { 14 | if (str[count] == '\0') 15 | { 16 | _putchar('\n'); 17 | break; 18 | } 19 | if (count % 2 == 0) 20 | _putchar(str[count]); 21 | count++; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /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 | void puts_half(char *str) 9 | { 10 | int count = 0, i; 11 | 12 | while (count >= 0) 13 | { 14 | if (str[count] == '\0') 15 | break; 16 | count++; 17 | } 18 | 19 | if (count % 2 == 1) 20 | i = count / 2; 21 | else 22 | i = (count - 1) / 2; 23 | 24 | for (i++; i < count; i++) 25 | _putchar(str[i]); 26 | _putchar('\n'); 27 | } 28 | -------------------------------------------------------------------------------- /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 | void print_array(int *a, int n) 11 | { 12 | int i = 0; 13 | 14 | for (; i < n; i++) 15 | { 16 | printf("%d", *(a + i)); 17 | if (i != (n - 1)) 18 | printf(", "); 19 | } 20 | printf("\n"); 21 | } 22 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/9-strcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * _strcpy - copies the string pointed to by src, 5 | * including the terminating null byte, to the 6 | * buffer pointed to by dest. 7 | * @dest: destination. 8 | * @src: source. 9 | * Return: the pointer to dest. 10 | */ 11 | char *_strcpy(char *dest, char *src) 12 | { 13 | int count = 0; 14 | 15 | while (count >= 0) 16 | { 17 | *(dest + count) = *(src + count); 18 | if (*(src + count) == '\0') 19 | break; 20 | count++; 21 | } 22 | return (dest); 23 | } 24 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | # Pointers, arrays and strings 2 | 3 | Project done during **Full Stack Software Engineering** at **ALX SE**. About pointers and arrays in **C language**. 4 | 5 | ## Technologies 6 | * C files are compiled using `gcc` 7 | * C files are written according to the Betty coding standard 8 | * Tested on Ubuntu 20.04 LTS 9 | 10 | ## Files 11 | All of the following files are programs written in C: 12 | 13 | | Filename | Description | 14 | | -------- | ----------- | 15 | | `0-reset_to_98.c` | Takes a pointer to an `int` as parameter and updates the value it points to `98` | 16 | | `1-swap.c` | Swaps the values of two integers | 17 | | `2-strlen.c` | Returns the length of a string | 18 | | `3-puts.c` | Prints a string to `stdout` | 19 | | `4-print_rev.c` | Prints a string, in reverse | 20 | | `5-rev_string.c` | Reverses a string | 21 | | `6-puts2.c` | Prints every other character of a string, starting with the first character | 22 | | `7-puts_half.c` | Prints half of a string | 23 | | `8-print_array.c` | Prints `n` elements of an array of integers | 24 | | `9-strcpy.c` | Copies the string pointed to by `src`, including the terminating null byte, to the buffer pointed to by `dest` | 25 | | `100-atoi.c` | Converts a string to an integer | 26 | | `101-keygen.c` | Generates random valid passwords for the program `101-crackme` | 27 | -------------------------------------------------------------------------------- /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 | * _strncat - concatenates two strings, 4 | * @dest: destination. 5 | * @src: source. 6 | * @n: print amount of bytes used from src. 7 | * Return: the pointer to destination 8 | */ 9 | char *_strncat(char *dest, char *src, int n) 10 | { 11 | int count = 0, count2 = 0; 12 | 13 | while (*(dest + count) != '\0') 14 | { 15 | count++; 16 | } 17 | 18 | while (count2 < n) 19 | { 20 | *(dest + count) = *(src + count2); 21 | if (*(src + count2) == '\0') 22 | break; 23 | count++; 24 | count2++; 25 | } 26 | return (dest); 27 | } 28 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/100-rot13.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * rot13 - encodes a string using rot13 4 | * @s: input string. 5 | * Return: the pointer to dest. 6 | */ 7 | 8 | char *rot13(char *s) 9 | { 10 | int count = 0, i; 11 | char alphabet[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; 12 | char rot13[] = "nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM"; 13 | 14 | while (*(s + count) != '\0') 15 | { 16 | for (i = 0; i < 52; i++) 17 | { 18 | if (*(s + count) == alphabet[i]) 19 | { 20 | *(s + count) = rot13[i]; 21 | break; 22 | } 23 | } 24 | count++; 25 | } 26 | 27 | return (s); 28 | } 29 | -------------------------------------------------------------------------------- /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/102-magic.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | int n; 6 | int a[5]; 7 | int *p; 8 | 9 | a[2] = 1024; 10 | p = &n; 11 | /* 12 | * write your line of code here... 13 | * Remember: 14 | * - you are not allowed to use a 15 | * - you are not allowed to modify p 16 | * - only one statement 17 | * - you are not allowed to code anything else than this line of code 18 | */ 19 | p[5] = 98; 20 | /* ...so that this prints 98\n */ 21 | printf("a[2] = %d\n", a[2]); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/103-infinite_add.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * infinite_add - adds two numbers 5 | * @n1: number one. 6 | * @n2: number two. 7 | * @r: buffer that the function will use to store the result. 8 | * @size_r: buffer size: 9 | * Return: the pointer to dest. 10 | */ 11 | 12 | char *infinite_add(char *n1, char *n2, char *r, int size_r) 13 | { 14 | int c1 = 0, c2 = 0, op, bg, dr1, dr2, add = 0; 15 | 16 | while (*(n1 + c1) != '\0') 17 | c1++; 18 | while (*(n2 + c2) != '\0') 19 | c2++; 20 | if (c1 >= c2) 21 | bg = c1; 22 | else 23 | bg = c2; 24 | if (size_r <= bg + 1) 25 | return (0); 26 | r[bg + 1] = '\0'; 27 | c1--, c2--, size_r--; 28 | dr1 = *(n1 + c1) - 48, dr2 = *(n2 + c2) - 48; 29 | while (bg >= 0) 30 | { 31 | op = dr1 + dr2 + add; 32 | if (op >= 10) 33 | add = op / 10; 34 | else 35 | add = 0; 36 | if (op > 0) 37 | *(r + bg) = (op % 10) + 48; 38 | else 39 | *(r + bg) = '0'; 40 | if (c1 > 0) 41 | c1--, dr1 = *(n1 + c1) - 48; 42 | else 43 | dr1 = 0; 44 | if (c2 > 0) 45 | c2--, dr2 = *(n2 + c2) - 48; 46 | else 47 | dr2 = 0; 48 | bg--, size_r--; 49 | } 50 | if (*(r) == '0') 51 | return (r + 1); 52 | else 53 | return (r); 54 | } 55 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/104-print_buffer.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * print_buffer - prints a buffer 6 | * @b: buffer. 7 | * @size: size of buffer. 8 | * Return: no return. 9 | */ 10 | void print_buffer(char *b, int size) 11 | { 12 | int j, k, l; 13 | 14 | if (size <= 0) 15 | printf("\n"); 16 | else 17 | { 18 | for (j = 0; j < size; j += 10) 19 | { 20 | printf("%.8x:", j); 21 | for (k = j; k < j + 10; k++) 22 | { 23 | if (k % 2 == 0) 24 | printf(" "); 25 | if (k < size) 26 | printf("%.2x", *(b + k)); 27 | else 28 | printf(" "); 29 | } 30 | printf(" "); 31 | for (l = j; l < j + 10; l++) 32 | { 33 | if (l >= size) 34 | break; 35 | if (*(b + l) < 32 || *(b + l) > 126) 36 | printf("%c", '.'); 37 | else 38 | printf("%c", *(b + l)); 39 | } 40 | printf("\n"); 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/2-strncpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strncpy - copies a string 4 | * @dest: destination. 5 | * @src: source. 6 | * @n: amount of bytes from src. 7 | * Return: the pointer to dest. 8 | */ 9 | 10 | char *_strncpy(char *dest, char *src, int n) 11 | { 12 | int i; 13 | 14 | for (i = 0; i < n && src[i] != '\0'; i++) 15 | dest[i] = src[i]; 16 | for ( ; i < n; i++) 17 | dest[i] = '\0'; 18 | 19 | return (dest); 20 | } 21 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/3-strcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strcmp - compares two strings 4 | * @s1: first string. 5 | * @s2: second string. 6 | * Return: 0 if s1 and s2 are equals, 7 | * another number if not. 8 | */ 9 | 10 | int _strcmp(char *s1, char *s2) 11 | { 12 | int i = 0, op = 0; 13 | 14 | while (op == 0) 15 | { 16 | if ((*(s1 + i) == '\0') && (*(s2 + i) == '\0')) 17 | break; 18 | op = *(s1 + i) - *(s2 + i); 19 | i++; 20 | } 21 | 22 | return (op); 23 | } 24 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/4-rev_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * reverse_array - reverses the content of an array 4 | * of integers. 5 | * @a: array. 6 | * @n: number of elements of the array. 7 | * Return: no return. 8 | */ 9 | 10 | void reverse_array(int *a, int n) 11 | { 12 | int i, j, temp; 13 | 14 | for (i = 0; i < n - 1; i++) 15 | { 16 | for (j = i + 1; j > 0; j--) 17 | { 18 | temp = *(a + j); 19 | *(a + j) = *(a + (j - 1)); 20 | *(a + (j - 1)) = temp; 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/5-string_toupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * string_toupper - changes all lowercase letters of a string 4 | * to uppercase 5 | * @s: input string. 6 | * Return: the pointer to dest. 7 | */ 8 | 9 | char *string_toupper(char *s) 10 | { 11 | int count = 0; 12 | 13 | while (*(s + count) != '\0') 14 | { 15 | if ((*(s + count) >= 97) && (*(s + count) <= 122)) 16 | *(s + count) = *(s + count) - 32; 17 | count++; 18 | } 19 | 20 | return (s); 21 | } 22 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/6-cap_string.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * cap_string - capitalizes all words of a string 4 | * @s: input string. 5 | * Return: the pointer to dest. 6 | */ 7 | 8 | char *cap_string(char *s) 9 | { 10 | int count = 0, i; 11 | int sep_words[] = {32, 9, 10, 44, 59, 46, 33, 63, 34, 40, 41, 123, 125}; 12 | 13 | if (*(s + count) >= 97 && *(s + count) <= 122) 14 | *(s + count) = *(s + count) - 32; 15 | count++; 16 | while (*(s + count) != '\0') 17 | { 18 | for (i = 0; i < 13; i++) 19 | { 20 | if (*(s + count) == sep_words[i]) 21 | { 22 | if ((*(s + (count + 1)) >= 97) && (*(s + (count + 1)) <= 122)) 23 | *(s + (count + 1)) = *(s + (count + 1)) - 32; 24 | break; 25 | } 26 | } 27 | count++; 28 | } 29 | return (s); 30 | } 31 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/7-leet.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * leet - encodes a string into 1337 4 | * @s: input string. 5 | * Return: the pointer to dest. 6 | */ 7 | 8 | char *leet(char *s) 9 | { 10 | int count = 0, i; 11 | int low_letters[] = {97, 101, 111, 116, 108}; 12 | int upp_letters[] = {65, 69, 79, 84, 76}; 13 | int numbers[] = {52, 51, 48, 55, 49}; 14 | 15 | while (*(s + count) != '\0') 16 | { 17 | for (i = 0; i < 5; i++) 18 | { 19 | if (*(s + count) == low_letters[i] || *(s + count) == upp_letters[i]) 20 | { 21 | *(s + count) = numbers[i]; 22 | break; 23 | } 24 | } 25 | count++; 26 | } 27 | 28 | return (s); 29 | } 30 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | # 0x06 More pointers, arrays and strings 2 | Project done during **Full Stack Software Engineering** at **ALX SE**. More pointers and arrays in **C language**. 3 | 4 | ## Technologies 5 | * C files are compiled using `gcc` 6 | * C files are written according to the Betty coding 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-strcat.c` | Concatenates two strings | 15 | | `1-strncat.c` | Concatenates two strings, using `n` bytes from `src` | 16 | | `2-strncpy.c` | Copies a string | 17 | | `3-strcmp.c` | Compares two strings | 18 | | `4-rev_array.c` | Reverses the content of an array of integers | 19 | | `5-string_toupper.c` | Changes all lowercase letters of a string to uppercase | 20 | | `6-cap_string.c` | Capitalizes all words of a string | 21 | | `7-leet.c` | Encodes a string into 1337 | 22 | | `100-rot13.c` | Encodes a string using rot13 | 23 | | `101-print_number.c` | Prints an integer | 24 | | `102-magic.c` | Changes the value of an index of an integer array | 25 | | `103-infinite_add.c` | Adds two numbers | 26 | | `104-print_buffer.c` | Prints a buffer | 27 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | #ifndef _MAIN_H_ 2 | #define _MAIN_H_ 3 | 4 | char *_strcat(char *dest, char *src); 5 | char *_strncat(char *dest, char *src, int n); 6 | char *_strncpy(char *dest, char *src, int n); 7 | int _strcmp(char *s1, char *s2); 8 | void reverse_array(int *a, int n); 9 | char *string_toupper(char *); 10 | char *cap_string(char *); 11 | char *leet(char *); 12 | char *rot13(char *); 13 | void print_number(int n); 14 | int _putchar(char); 15 | char *infinite_add(char *n1, char *n2, char *r, int size_r); 16 | void print_buffer(char *b, int size); 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/0-memset.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _memset - fills memory with a constant byte, 4 | * @s: memory area. 5 | * @b: constant byte. 6 | * @n: bytes filled. 7 | * Return: the pointer to dest. 8 | */ 9 | char *_memset(char *s, char b, unsigned int n) 10 | { 11 | unsigned int i; 12 | 13 | for (i = 0; i < n; i++) 14 | *(s + i) = b; 15 | 16 | return (s); 17 | } 18 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/1-memcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _memcpy - copies memory area, 4 | * @dest: destination memory area. 5 | * @src: source memory area. 6 | * @n: bytes filled. 7 | * Return: the pointer to dest. 8 | */ 9 | char *_memcpy(char *dest, char *src, unsigned int n) 10 | { 11 | unsigned int i; 12 | 13 | for (i = 0; i < n; i++) 14 | *(dest + i) = *(src + i); 15 | 16 | return (dest); 17 | } 18 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/100-set_string.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *set_string - sets the value of a pointer to a char. 4 | *@s: pointer to pointer. 5 | *@to: pointer to char. 6 | * 7 | *Return: void. 8 | */ 9 | void set_string(char **s, char *to) 10 | { 11 | *s = to; 12 | } 13 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/101-crackme_password: -------------------------------------------------------------------------------- 1 | abc123 -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/2-strchr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strchr - locates a character in a string, 4 | * @s: string. 5 | * @c: character. 6 | * Return: the pointer to the first occurrence of the character c. 7 | */ 8 | char *_strchr(char *s, char c) 9 | { 10 | unsigned int i = 0; 11 | 12 | for (; *(s + i) != '\0'; i++) 13 | if (*(s + i) == c) 14 | return (s + i); 15 | if (*(s + i) == c) 16 | return (s + i); 17 | return ('\0'); 18 | } 19 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/3-strspn.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strspn - gets the length of a prefix substring. 5 | * @s: initial segment. 6 | * @accept: accepted bytes. 7 | * Return: the number of accepted bytes. 8 | */ 9 | unsigned int _strspn(char *s, char *accept) 10 | { 11 | unsigned int i, j, bool; 12 | 13 | for (i = 0; *(s + i) != '\0'; i++) 14 | { 15 | bool = 1; 16 | for (j = 0; *(accept + j) != '\0'; j++) 17 | { 18 | if (*(s + i) == *(accept + j)) 19 | { 20 | bool = 0; 21 | break; 22 | } 23 | } 24 | if (bool == 1) 25 | break; 26 | } 27 | return (i); 28 | } 29 | -------------------------------------------------------------------------------- /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 | char *_strpbrk(char *s, char *accept) 11 | { 12 | unsigned int i, j; 13 | 14 | for (i = 0; *(s + i) != '\0'; i++) 15 | { 16 | for (j = 0; *(accept + j) != '\0'; j++) 17 | { 18 | if (*(s + i) == *(accept + j)) 19 | return (s + i); 20 | } 21 | } 22 | return ('\0'); 23 | } 24 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/5-strstr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strstr - finds the first occurrence of the substring. 5 | * needle in the string haystack. 6 | * @haystack: entire string. 7 | * @needle: substring. 8 | * Return: pointer to the beginning of located substring or 9 | * NULL if the substring is not found. 10 | */ 11 | char *_strstr(char *haystack, char *needle) 12 | { 13 | char *bhaystack; 14 | char *pneedle; 15 | 16 | while (*haystack != '\0') 17 | { 18 | bhaystack = haystack; 19 | pneedle = needle; 20 | 21 | while (*haystack != '\0' && *pneedle != '\0' && *haystack == *pneedle) 22 | { 23 | haystack++; 24 | pneedle++; 25 | } 26 | if (!*pneedle) 27 | return (bhaystack); 28 | haystack = bhaystack + 1; 29 | } 30 | return (0); 31 | } 32 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/7-print_chessboard.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_chessboard - prints the chessboard 5 | * @a: input pointer. 6 | * Return: no return. 7 | */ 8 | void print_chessboard(char (*a)[8]) 9 | { 10 | unsigned int i, m = 0; 11 | 12 | for (i = 0; i < 64; i++) 13 | { 14 | if (i % 8 == 0 && i != 0) 15 | { 16 | m = i; 17 | _putchar('\n'); 18 | } 19 | _putchar(a[i / 8][i - m]); 20 | } 21 | _putchar('\n'); 22 | } 23 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/8-print_diagsums.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * print_diagsums - prints the sum of the two 5 | * diagonals of a square matrix of integers 6 | * @a: input pointer. 7 | * @size: size of the matrix 8 | * Return: no return. 9 | */ 10 | void print_diagsums(int *a, int size) 11 | { 12 | int i, sum1 = 0, sum2 = 0; 13 | 14 | for (i = 0; i < (size * size); i++) 15 | { 16 | if (i % (size + 1) == 0) 17 | sum1 += *(a + i); 18 | if (i % (size - 1) == 0 && i != 0 && i < size * size - 1) 19 | sum2 += *(a + i); 20 | } 21 | printf("%d, %d\n", sum1, sum2); 22 | } 23 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | ## 0X07 Even more pointers, arrays and strings 2 | 3 | Project done during **Full Stack Software Engineering** at **ALX SE**. It aims to learn about pointers and arrays in **C language**. 4 | 5 | ## Technologies 6 | * C files are compiled using `gcc` 7 | * C files are written according to the betty standard 8 | * Tested on Ubuntu 20.04 LTS 9 | 10 | ## Files 11 | All of the following files are programs written in C: 12 | 13 | | Filename | Description | 14 | | -------- | ----------- | 15 | | `0-memset.c` | Fills memory with a constant byte | 16 | | `1-memcpy.c` | Copies memory area | 17 | | `2-strchr.c` | Locates a character in a string | 18 | | `3-strspn.c` | Gets the length of a prefix substring | 19 | | `4-strpbrk.c` | Searches a string for any of a set of bytes | 20 | | `5-strstr.c` | Locates a substring | 21 | | `7-print_chessboard.c` | Prints the chessboard | 22 | | `8-print_diagsums.c` | Prints the sum of the two diagonals of a square matrix of integers | 23 | | `100-set_string.c` | Sets the value of a pointer to a char | 24 | | `101-crackme_password.c` | Contains the password for the `crackme2` executable | 25 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | #ifndef _MAIN_H_ 2 | #define _MAIN_H_ 3 | 4 | char *_memset(char *s, char b, unsigned int n); 5 | char *_memcpy(char *dest, char *src, unsigned int n); 6 | char *_strchr(char *s, char c); 7 | unsigned int _strspn(char *s, char *accept); 8 | char *_strpbrk(char *s, char *accept); 9 | char *_strstr(char *haystack, char *needle); 10 | int _putchar(char); 11 | void print_chessboard(char (*a)[8]); 12 | void print_diagsums(int *a, int size); 13 | void set_string(char **s, char *to); 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /0x08-recursion/0-puts_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _puts_recursion - prints a string, followed by a new line. 5 | * @s: string 6 | * Return: no return. 7 | */ 8 | void _puts_recursion(char *s) 9 | { 10 | if (*s != '\0') 11 | { 12 | _putchar(*s); 13 | _puts_recursion(s + 1); 14 | } 15 | else 16 | _putchar('\n'); 17 | } 18 | -------------------------------------------------------------------------------- /0x08-recursion/1-print_rev_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _print_rev_recursion - prints a string in reverse. 5 | * @s: string 6 | * Return: no return. 7 | */ 8 | void _print_rev_recursion(char *s) 9 | { 10 | if (*s != '\0') 11 | { 12 | _print_rev_recursion(s + 1); 13 | _putchar(*s); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /0x08-recursion/100-is_palindrome.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strlen_recursion - returns the length of a string. 5 | * @s: string 6 | * Return: the length of a string. 7 | */ 8 | int _strlen_recursion(char *s) 9 | { 10 | if (*s == '\0') 11 | return (0); 12 | else 13 | return (1 + _strlen_recursion(s + 1)); 14 | } 15 | 16 | /** 17 | * comparator - compares each character of the string. 18 | * @s: string 19 | * @n1: smallest iterator. 20 | * @n2: biggest iterator. 21 | * Return: . 22 | */ 23 | int comparator(char *s, int n1, int n2) 24 | { 25 | if (*(s + n1) == *(s + n2)) 26 | { 27 | if (n1 == n2 || n1 == n2 + 1) 28 | return (1); 29 | return (0 + comparator(s, n1 + 1, n2 - 1)); 30 | } 31 | return (0); 32 | } 33 | 34 | /** 35 | * is_palindrome - detects if a string is a palindrome. 36 | * @s: string. 37 | * Return: 1 if s is a palindrome, 0 if not. 38 | */ 39 | int is_palindrome(char *s) 40 | { 41 | if (*s == '\0') 42 | return (1); 43 | return (comparator(s, 0, _strlen_recursion(s) - 1)); 44 | } 45 | -------------------------------------------------------------------------------- /0x08-recursion/101-wildcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * wildcmp - compares two strings. 5 | * @s1: string 1. 6 | * @s2: string 2. It can contains a * as a special character. 7 | * Return: 1 if are identical, 0 if not. 8 | */ 9 | int wildcmp(char *s1, char *s2) 10 | { 11 | if (*s2 == '*' && *(s2 + 1) != '\0' && *s1 == '\0') 12 | return (0); 13 | if (*s1 == '\0' && *s2 == '\0') 14 | return (1); 15 | if (*s1 == *s2) 16 | return (wildcmp(s1 + 1, s2 + 1)); 17 | if (*s2 == '*') 18 | return (wildcmp(s1, s2 + 1) || wildcmp(s1 + 1, s2)); 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /0x08-recursion/2-strlen_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strlen_recursion - returns the length of a string. 5 | * @s: string 6 | * Return: the length of a string. 7 | */ 8 | int _strlen_recursion(char *s) 9 | { 10 | if (*s == '\0') 11 | return (0); 12 | else 13 | return (1 + _strlen_recursion(s + 1)); 14 | } 15 | -------------------------------------------------------------------------------- /0x08-recursion/3-factorial.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * factorial - returns the factorial of a given number. 5 | * @n: input number. 6 | * Return: factorial of the number. 7 | */ 8 | int factorial(int n) 9 | { 10 | if (n < 0) 11 | return (-1); 12 | else if (n == 0) 13 | return (1); 14 | else 15 | return (n * factorial(n - 1)); 16 | } 17 | -------------------------------------------------------------------------------- /0x08-recursion/4-pow_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _pow_recursion - returns the value of x raised to the power of y. 5 | * @x: base. 6 | * @y: exponent. 7 | * Return: value of the exponentiation. 8 | */ 9 | int _pow_recursion(int x, int y) 10 | { 11 | if (y < 0) 12 | return (-1); 13 | else if (y == 0) 14 | return (1); 15 | else 16 | return (x * _pow_recursion(x, y - 1)); 17 | } 18 | -------------------------------------------------------------------------------- /0x08-recursion/5-sqrt_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * power_operation - returns the natural square root of a number. 5 | * @n: input number. 6 | * @c: iterator. 7 | * Return: square root or -1. 8 | */ 9 | int power_operation(int n, int c) 10 | { 11 | if (c % (n / c) == 0) 12 | { 13 | if (c * (n / c) == n) 14 | return (c); 15 | else 16 | return (-1); 17 | } 18 | return (0 + power_operation(n, c + 1)); 19 | } 20 | /** 21 | * _sqrt_recursion - returns the natural square root of a number. 22 | * @n: input number. 23 | * Return: natural square root. 24 | */ 25 | int _sqrt_recursion(int n) 26 | { 27 | if (n < 0) 28 | return (-1); 29 | if (n == 0) 30 | return (0); 31 | if (n == 1) 32 | return (1); 33 | return (power_operation(n, 2)); 34 | } 35 | -------------------------------------------------------------------------------- /0x08-recursion/6-is_prime_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * is_prime - detects if an input number is a prime number. 5 | * @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/README.md: -------------------------------------------------------------------------------- 1 | # 0x08 Recursion 2 | 3 | Project done during ** Software Engineering ** at **ALX**. ABOUT recursion and how to implement it in **C language**. 4 | 5 | ## Technologies 6 | * C files are compiled using `gcc` 7 | * C files are written according to the betty standard 8 | * Tested on Ubuntu 20.04 LTS 9 | 10 | ## Files 11 | All of the following files are programs written in C: 12 | 13 | | Filename | Description | 14 | | -------- | ----------- | 15 | | `0-puts_recursion.c` | Prints a string | 16 | | `1-print_rev_recursion.c` | Prints a string in reverse | 17 | | `2-strlen_recursion.c` | Returns the length of a string | 18 | | `3-factorial.c` | Returns the factorial of a given number | 19 | | `4-pow_recursion.c` | Returns the value of `x` raised to the power of `y` | 20 | | `5-sqrt_recursion.c` | Returns the natural sqaure root of a number | 21 | | `6-is_prime_number.c` | Returns `1` if the input integer is a prime number, otherwise return `0` | 22 | | `100-is_palindrome.c` | Returns `1` if a string is a palindrome and `0` if not | 23 | | `101-wildcmp.c` | Compares two strings and returns `1` if the strings can be considered identical. Otherwise, returns `0` | 24 | -------------------------------------------------------------------------------- /0x08-recursion/main.h: -------------------------------------------------------------------------------- 1 | #ifndef _MAIN_H_ 2 | #define _MAIN_H_ 3 | 4 | int _putchar(char); 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/0-isupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isupper - checks if parameter is an uppercase character. 5 | * @c: input character. 6 | * Return: 1 if is an uppercase character, 0 in other case. 7 | */ 8 | int _isupper(int c) 9 | { 10 | if (c >= 65 && c <= 90) 11 | { 12 | return (1); 13 | } 14 | else 15 | { 16 | return (0); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /0x09-static_libraries/0-isupper.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tecnophille/alx-low_level_programming/0e22613fb01cad5635f187c402011ff726386b2e/0x09-static_libraries/0-isupper.o -------------------------------------------------------------------------------- /0x09-static_libraries/0-memset.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _memset - fills memory with a constant byte, 4 | * @s: memory area. 5 | * @b: constant byte. 6 | * @n: bytes filled. 7 | * Return: the pointer to dest. 8 | */ 9 | char *_memset(char *s, char b, unsigned int n) 10 | { 11 | unsigned int i; 12 | 13 | for (i = 0; i < n; i++) 14 | *(s + i) = b; 15 | 16 | return (s); 17 | } 18 | -------------------------------------------------------------------------------- /0x09-static_libraries/0-memset.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tecnophille/alx-low_level_programming/0e22613fb01cad5635f187c402011ff726386b2e/0x09-static_libraries/0-memset.o -------------------------------------------------------------------------------- /0x09-static_libraries/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/0-strcat.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tecnophille/alx-low_level_programming/0e22613fb01cad5635f187c402011ff726386b2e/0x09-static_libraries/0-strcat.o -------------------------------------------------------------------------------- /0x09-static_libraries/1-isdigit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isdigit - checks if parameter is a number between 0 to 9. 5 | * @c: input number. 6 | * Return: 1 if is a number (0 to 9), 0 in other case. 7 | */ 8 | int _isdigit(int c) 9 | { 10 | if (c >= 48 && c <= 57) 11 | { 12 | return (1); 13 | } 14 | else 15 | { 16 | return (0); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /0x09-static_libraries/1-isdigit.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tecnophille/alx-low_level_programming/0e22613fb01cad5635f187c402011ff726386b2e/0x09-static_libraries/1-isdigit.o -------------------------------------------------------------------------------- /0x09-static_libraries/1-memcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _memcpy - copies memory area, 4 | * @dest: destination memory area. 5 | * @src: source memory area. 6 | * @n: bytes filled. 7 | * Return: the pointer to dest. 8 | */ 9 | char *_memcpy(char *dest, char *src, unsigned int n) 10 | { 11 | unsigned int i; 12 | 13 | for (i = 0; i < n; i++) 14 | *(dest + i) = *(src + i); 15 | 16 | return (dest); 17 | } 18 | -------------------------------------------------------------------------------- /0x09-static_libraries/1-memcpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tecnophille/alx-low_level_programming/0e22613fb01cad5635f187c402011ff726386b2e/0x09-static_libraries/1-memcpy.o -------------------------------------------------------------------------------- /0x09-static_libraries/1-strncat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strncat - concatenates two strings, 4 | * @dest: destination. 5 | * @src: source. 6 | * @n: print amount of bytes used from src. 7 | * Return: the pointer to destination 8 | */ 9 | char *_strncat(char *dest, char *src, int n) 10 | { 11 | int count = 0, count2 = 0; 12 | 13 | while (*(dest + count) != '\0') 14 | { 15 | count++; 16 | } 17 | 18 | while (count2 < n) 19 | { 20 | *(dest + count) = *(src + count2); 21 | if (*(src + count2) == '\0') 22 | break; 23 | count++; 24 | count2++; 25 | } 26 | return (dest); 27 | } 28 | -------------------------------------------------------------------------------- /0x09-static_libraries/1-strncat.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tecnophille/alx-low_level_programming/0e22613fb01cad5635f187c402011ff726386b2e/0x09-static_libraries/1-strncat.o -------------------------------------------------------------------------------- /0x09-static_libraries/100-atoi.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _atoi - converts a string to an integer. 5 | * @s: input string. 6 | * Return: integer. 7 | */ 8 | int _atoi(char *s) 9 | { 10 | unsigned int count = 0, size = 0, oi = 0, pn = 1, m = 1, i; 11 | 12 | while (*(s + count) != '\0') 13 | { 14 | if (size > 0 && (*(s + count) < '0' || *(s + count) > '9')) 15 | break; 16 | 17 | if (*(s + count) == '-') 18 | pn *= -1; 19 | 20 | if ((*(s + count) >= '0') && (*(s + count) <= '9')) 21 | { 22 | if (size > 0) 23 | m *= 10; 24 | size++; 25 | } 26 | count++; 27 | } 28 | 29 | for (i = count - size; i < count; i++) 30 | { 31 | oi = oi + ((*(s + i) - 48) * m); 32 | m /= 10; 33 | } 34 | return (oi * pn); 35 | } 36 | -------------------------------------------------------------------------------- /0x09-static_libraries/100-atoi.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tecnophille/alx-low_level_programming/0e22613fb01cad5635f187c402011ff726386b2e/0x09-static_libraries/100-atoi.o -------------------------------------------------------------------------------- /0x09-static_libraries/2-strchr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strchr - locates a character in a string, 4 | * @s: string. 5 | * @c: character. 6 | * Return: the pointer to the first occurrence of the character c. 7 | */ 8 | char *_strchr(char *s, char c) 9 | { 10 | unsigned int i = 0; 11 | 12 | for (; *(s + i) != '\0'; i++) 13 | if (*(s + i) == c) 14 | return (s + i); 15 | if (*(s + i) == c) 16 | return (s + i); 17 | return ('\0'); 18 | } 19 | -------------------------------------------------------------------------------- /0x09-static_libraries/2-strchr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tecnophille/alx-low_level_programming/0e22613fb01cad5635f187c402011ff726386b2e/0x09-static_libraries/2-strchr.o -------------------------------------------------------------------------------- /0x09-static_libraries/2-strlen.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strlen - returns the length of a string. 5 | * @s: input string. 6 | * Return: length of a string. 7 | */ 8 | int _strlen(char *s) 9 | { 10 | int count = 0; 11 | 12 | while (*(s + count) != '\0') 13 | count++; 14 | return (count); 15 | } 16 | -------------------------------------------------------------------------------- /0x09-static_libraries/2-strlen.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tecnophille/alx-low_level_programming/0e22613fb01cad5635f187c402011ff726386b2e/0x09-static_libraries/2-strlen.o -------------------------------------------------------------------------------- /0x09-static_libraries/2-strncpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strncpy - copies a string 4 | * @dest: destination. 5 | * @src: source. 6 | * @n: amount of bytes from src. 7 | * Return: the pointer to dest. 8 | */ 9 | 10 | char *_strncpy(char *dest, char *src, int n) 11 | { 12 | int i; 13 | 14 | for (i = 0; i < n && src[i] != '\0'; i++) 15 | dest[i] = src[i]; 16 | for ( ; i < n; i++) 17 | dest[i] = '\0'; 18 | 19 | return (dest); 20 | } 21 | -------------------------------------------------------------------------------- /0x09-static_libraries/2-strncpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tecnophille/alx-low_level_programming/0e22613fb01cad5635f187c402011ff726386b2e/0x09-static_libraries/2-strncpy.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-islower.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _islower - Shows 1 if the input is a 5 | * lowercase character. Another cases, shows 6 | * 0 7 | * 8 | * @c: The character in ASCII code 9 | * 10 | * Return: 1 for lowercase character. 0 for the rest. 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/3-islower.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tecnophille/alx-low_level_programming/0e22613fb01cad5635f187c402011ff726386b2e/0x09-static_libraries/3-islower.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-puts.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _puts - prints a string, followed by a new line. 5 | * @str: input string. 6 | * Return: no return. 7 | */ 8 | void _puts(char *str) 9 | { 10 | int count = 0; 11 | 12 | while (count >= 0) 13 | { 14 | if (str[count] == '\0') 15 | { 16 | _putchar('\n'); 17 | break; 18 | } 19 | _putchar(str[count]); 20 | count++; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-puts.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tecnophille/alx-low_level_programming/0e22613fb01cad5635f187c402011ff726386b2e/0x09-static_libraries/3-puts.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-strcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strcmp - compares two strings 4 | * @s1: first string. 5 | * @s2: second string. 6 | * Return: 0 if s1 and s2 are equals, 7 | * another number if not. 8 | */ 9 | 10 | int _strcmp(char *s1, char *s2) 11 | { 12 | int i = 0, op = 0; 13 | 14 | while (op == 0) 15 | { 16 | if ((*(s1 + i) == '\0') && (*(s2 + i) == '\0')) 17 | break; 18 | op = *(s1 + i) - *(s2 + i); 19 | i++; 20 | } 21 | 22 | return (op); 23 | } 24 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-strcmp.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tecnophille/alx-low_level_programming/0e22613fb01cad5635f187c402011ff726386b2e/0x09-static_libraries/3-strcmp.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-strspn.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strspn - gets the length of a prefix substring. 5 | * @s: initial segment. 6 | * @accept: accepted bytes. 7 | * Return: the number of accepted bytes. 8 | */ 9 | unsigned int _strspn(char *s, char *accept) 10 | { 11 | unsigned int i, j, bool; 12 | 13 | for (i = 0; *(s + i) != '\0'; i++) 14 | { 15 | bool = 1; 16 | for (j = 0; *(accept + j) != '\0'; j++) 17 | { 18 | if (*(s + i) == *(accept + j)) 19 | { 20 | bool = 0; 21 | break; 22 | } 23 | } 24 | if (bool == 1) 25 | break; 26 | } 27 | return (i); 28 | } 29 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-strspn.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tecnophille/alx-low_level_programming/0e22613fb01cad5635f187c402011ff726386b2e/0x09-static_libraries/3-strspn.o -------------------------------------------------------------------------------- /0x09-static_libraries/4-isalpha.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isalpha - Shows 1 if the input is a 5 | * letter Another cases, shows 0 6 | * 7 | * @c: The character in ASCII code 8 | * 9 | * Return: 1 for letters. 0 for the rest. 10 | */ 11 | int _isalpha(int c) 12 | { 13 | if ((c >= 97 && c <= 122) || (c >= 65 && c <= 90)) 14 | { 15 | return (1); 16 | } 17 | else 18 | { 19 | return (0); 20 | } 21 | _putchar('\n'); 22 | } 23 | -------------------------------------------------------------------------------- /0x09-static_libraries/4-isalpha.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tecnophille/alx-low_level_programming/0e22613fb01cad5635f187c402011ff726386b2e/0x09-static_libraries/4-isalpha.o -------------------------------------------------------------------------------- /0x09-static_libraries/4-strpbrk.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strpbrk - searches a string for any of a set of bytes. 5 | * @s: first string. 6 | * @accept: second string. 7 | * Return: a pointer to the byte in s that matches one of the 8 | * bytes in accept, or NULL if no such byte is found. 9 | */ 10 | char *_strpbrk(char *s, char *accept) 11 | { 12 | unsigned int i, j; 13 | 14 | for (i = 0; *(s + i) != '\0'; i++) 15 | { 16 | for (j = 0; *(accept + j) != '\0'; j++) 17 | { 18 | if (*(s + i) == *(accept + j)) 19 | return (s + i); 20 | } 21 | } 22 | return ('\0'); 23 | } 24 | -------------------------------------------------------------------------------- /0x09-static_libraries/4-strpbrk.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tecnophille/alx-low_level_programming/0e22613fb01cad5635f187c402011ff726386b2e/0x09-static_libraries/4-strpbrk.o -------------------------------------------------------------------------------- /0x09-static_libraries/5-strstr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strstr - finds the first occurrence of the substring. 5 | * needle in the string haystack. 6 | * @haystack: entire string. 7 | * @needle: substring. 8 | * Return: pointer to the beginning of located substring or 9 | * NULL if the substring is not found. 10 | */ 11 | char *_strstr(char *haystack, char *needle) 12 | { 13 | char *bhaystack; 14 | char *pneedle; 15 | 16 | while (*haystack != '\0') 17 | { 18 | bhaystack = haystack; 19 | pneedle = needle; 20 | 21 | while (*haystack != '\0' && *pneedle != '\0' && *haystack == *pneedle) 22 | { 23 | haystack++; 24 | pneedle++; 25 | } 26 | if (!*pneedle) 27 | return (bhaystack); 28 | haystack = bhaystack + 1; 29 | } 30 | return (0); 31 | } 32 | -------------------------------------------------------------------------------- /0x09-static_libraries/5-strstr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tecnophille/alx-low_level_programming/0e22613fb01cad5635f187c402011ff726386b2e/0x09-static_libraries/5-strstr.o -------------------------------------------------------------------------------- /0x09-static_libraries/6-abs.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _abs - Computes the absolute value 5 | * of an integer. 6 | * 7 | * @i: input number as an integer. 8 | * 9 | * Return: absolute value 10 | */ 11 | int _abs(int i) 12 | { 13 | if (i >= 0) 14 | { 15 | return (i); 16 | } 17 | else 18 | { 19 | return (i * -1); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /0x09-static_libraries/6-abs.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tecnophille/alx-low_level_programming/0e22613fb01cad5635f187c402011ff726386b2e/0x09-static_libraries/6-abs.o -------------------------------------------------------------------------------- /0x09-static_libraries/9-strcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * _strcpy - copies the string pointed to by src, 5 | * including the terminating null byte, to the 6 | * buffer pointed to by dest. 7 | * @dest: destination. 8 | * @src: source. 9 | * Return: the pointer to dest. 10 | */ 11 | char *_strcpy(char *dest, char *src) 12 | { 13 | int count = 0; 14 | 15 | while (count >= 0) 16 | { 17 | *(dest + count) = *(src + count); 18 | if (*(src + count) == '\0') 19 | break; 20 | count++; 21 | } 22 | return (dest); 23 | } 24 | -------------------------------------------------------------------------------- /0x09-static_libraries/9-strcpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tecnophille/alx-low_level_programming/0e22613fb01cad5635f187c402011ff726386b2e/0x09-static_libraries/9-strcpy.o -------------------------------------------------------------------------------- /0x09-static_libraries/README.md: -------------------------------------------------------------------------------- 1 | # Static libraries 2 | 3 | Project done during **Full Stack Software Engineering** at **ALX SE**. To learn about static libraries in **C language**. 4 | 5 | ## Technologies 6 | * Scripts written in Bash (1) 7 | * C files are compiled using `gcc` 8 | * C files are written according to the Betty coding standard 9 | * Tested on Ubuntu 20.04 LTS 10 | 11 | ## Files 12 | All of the following files are scripts and programs written in C: 13 | 14 | | Filename | Description | 15 | | -------- | ----------- | 16 | | `libmy.a` | Static library that contains a lot of functions | 17 | | `create_static_lib.sh` | Script that creates a static library called `liball.a` from all the `.c` files that are in the current directory | 18 | | `main.h` | Header file that contains all the prototypes of the used functions | 19 | -------------------------------------------------------------------------------- /0x09-static_libraries/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _putchar - writes the character c to stdout 5 | * @c: The character to print 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | int _putchar(char c) 11 | { 12 | return (write(1, &c, 1)); 13 | } 14 | -------------------------------------------------------------------------------- /0x09-static_libraries/_putchar.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tecnophille/alx-low_level_programming/0e22613fb01cad5635f187c402011ff726386b2e/0x09-static_libraries/_putchar.o -------------------------------------------------------------------------------- /0x09-static_libraries/create_static_lib.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -c *.c 3 | ar rc liball.a *.o 4 | ranlib liball.a 5 | -------------------------------------------------------------------------------- /0x09-static_libraries/liball.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tecnophille/alx-low_level_programming/0e22613fb01cad5635f187c402011ff726386b2e/0x09-static_libraries/liball.a -------------------------------------------------------------------------------- /0x09-static_libraries/libmy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tecnophille/alx-low_level_programming/0e22613fb01cad5635f187c402011ff726386b2e/0x09-static_libraries/libmy -------------------------------------------------------------------------------- /0x09-static_libraries/libmy.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tecnophille/alx-low_level_programming/0e22613fb01cad5635f187c402011ff726386b2e/0x09-static_libraries/libmy.a -------------------------------------------------------------------------------- /0x09-static_libraries/main.h: -------------------------------------------------------------------------------- 1 | #ifndef _MAIN_H_ 2 | #define _MAIN_H_ 3 | 4 | int _putchar(char c); 5 | int _islower(int c); 6 | int _isalpha(int c); 7 | int _abs(int n); 8 | int _isupper(int c); 9 | int _isdigit(int c); 10 | int _strlen(char *s); 11 | void _puts(char *s); 12 | char *_strcpy(char *dest, char *src); 13 | int _atoi(char *s); 14 | char *_strcat(char *dest, char *src); 15 | char *_strncat(char *dest, char *src, int n); 16 | char *_strncpy(char *dest, char *src, int n); 17 | int _strcmp(char *s1, char *s2); 18 | char *_memset(char *s, char b, unsigned int n); 19 | char *_memcpy(char *dest, char *src, unsigned int n); 20 | char *_strchr(char *s, char c); 21 | unsigned int _strspn(char *s, char *accept); 22 | char *_strpbrk(char *s, char *accept); 23 | char *_strstr(char *haystack, char *needle); 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /0x0A-argc_argv/0-whatsmyname.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * 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/100-change.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main - prints the minimum number of coins to make change for an amount. 5 | * of money. 6 | * @argc: number of command line arguments. 7 | * @argv: array that contains the program command line arguments. 8 | * Return: 0 - success. 9 | */ 10 | int main(int argc, char *argv[]) 11 | { 12 | int cents, ncoins = 0; 13 | 14 | if (argc == 1 || argc > 2) 15 | { 16 | printf("Error\n"); 17 | return (1); 18 | } 19 | 20 | cents = atoi(argv[1]); 21 | 22 | while (cents > 0) 23 | { 24 | if (cents >= 25) 25 | cents -= 25; 26 | else if (cents >= 10) 27 | cents -= 10; 28 | else if (cents >= 5) 29 | cents -= 5; 30 | else if (cents >= 2) 31 | cents -= 2; 32 | else if (cents >= 1) 33 | cents -= 1; 34 | ncoins += 1; 35 | } 36 | printf("%d\n", ncoins); 37 | return (0); 38 | } 39 | -------------------------------------------------------------------------------- /0x0A-argc_argv/2-args.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - prints 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 | #include 4 | /** 5 | * main - adds positive numbers. 6 | * @argc: number of command line arguments. 7 | * @argv: array that contains the program command line arguments. 8 | * Return: 0 - success. 9 | */ 10 | int main(int argc, char *argv[]) 11 | { 12 | int i, j, add = 0; 13 | 14 | for (i = 1; i < argc; i++) 15 | { 16 | for (j = 0; argv[i][j] != '\0'; j++) 17 | { 18 | if (!isdigit(argv[i][j])) 19 | { 20 | printf("Error\n"); 21 | return (1); 22 | } 23 | } 24 | add += atoi(argv[i]); 25 | } 26 | printf("%d\n", add); 27 | return (0); 28 | } 29 | -------------------------------------------------------------------------------- /0x0A-argc_argv/README.md: -------------------------------------------------------------------------------- 1 | # Argc, argv 2 | Project done during **Full Stack Software Engineering** at **ALX SE**. To learn about how to use arguments passed to a program in **C language**. 3 | 4 | ## Technologies 5 | * C files are compiled using `gcc` 6 | * C files are written according to the Betty coding 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` | Prints its name | 15 | | `1-args.c` | Prints the number of arguments passed into it | 16 | | `2-args.c` | Prints all arguments it receives | 17 | | `3-mul.c` | Multiplies two numbers | 18 | | `4-add.c` | Adds positive numbers | 19 | | `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 *cr; 13 | unsigned int i; 14 | 15 | if (size == 0) 16 | return (NULL); 17 | 18 | cr = malloc(sizeof(c) * size); 19 | 20 | if (cr == NULL) 21 | return (NULL); 22 | 23 | for (i = 0; i < size; i++) 24 | cr[i] = c; 25 | 26 | return (cr); 27 | } 28 | -------------------------------------------------------------------------------- /0x0B-malloc_free/1-strdup.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * _strdup - returns a pointer to a newly allocated space in memory. 5 | * @str: string. 6 | * 7 | * Return: pointer of an array of chars 8 | */ 9 | char *_strdup(char *str) 10 | { 11 | char *strout; 12 | unsigned int i, j; 13 | 14 | if (str == NULL) 15 | return (NULL); 16 | 17 | for (i = 0; str[i] != '\0'; i++) 18 | ; 19 | 20 | strout = (char *)malloc(sizeof(char) * (i + 1)); 21 | 22 | if (strout == NULL) 23 | return (NULL); 24 | 25 | for (j = 0; j <= i; j++) 26 | strout[j] = str[j]; 27 | 28 | return (strout); 29 | } 30 | -------------------------------------------------------------------------------- /0x0B-malloc_free/100-argstostr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * argstostr - concatenates all the arguments of a program. 6 | * @ac: argument count. 7 | * @av: argument vector. 8 | * 9 | * Return: pointer of an array of char 10 | */ 11 | char *argstostr(int ac, char **av) 12 | { 13 | char *aout; 14 | int c, i, j, ia; 15 | 16 | if (ac == 0) 17 | return (NULL); 18 | 19 | for (c = i = 0; i < ac; i++) 20 | { 21 | if (av[i] == NULL) 22 | return (NULL); 23 | 24 | for (j = 0; av[i][j] != '\0'; j++) 25 | c++; 26 | c++; 27 | } 28 | 29 | aout = malloc((c + 1) * sizeof(char)); 30 | 31 | if (aout == NULL) 32 | { 33 | free(aout); 34 | return (NULL); 35 | } 36 | 37 | for (i = j = ia = 0; ia < c; j++, ia++) 38 | { 39 | if (av[i][j] == '\0') 40 | { 41 | aout[ia] = '\n'; 42 | i++; 43 | ia++; 44 | j = 0; 45 | } 46 | if (ia < c - 1) 47 | aout[ia] = av[i][j]; 48 | } 49 | aout[ia] = '\0'; 50 | 51 | return (aout); 52 | } 53 | -------------------------------------------------------------------------------- /0x0B-malloc_free/101-strtow.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * ch_free_grid - frees a 2 dimensional array. 6 | * @grid: multidimensional array of char. 7 | * @height: height of the array. 8 | * 9 | * Return: no return 10 | */ 11 | void ch_free_grid(char **grid, unsigned int height) 12 | { 13 | if (grid != NULL && height != 0) 14 | { 15 | for (; height > 0; height--) 16 | free(grid[height]); 17 | free(grid[height]); 18 | free(grid); 19 | } 20 | } 21 | 22 | /** 23 | * strtow - splits a string into words. 24 | * @str: string. 25 | * 26 | * Return: pointer of an array of integers 27 | */ 28 | char **strtow(char *str) 29 | { 30 | char **aout; 31 | unsigned int c, height, i, j, a1; 32 | 33 | if (str == NULL || *str == '\0') 34 | return (NULL); 35 | for (c = height = 0; str[c] != '\0'; c++) 36 | if (str[c] != ' ' && (str[c + 1] == ' ' || str[c + 1] == '\0')) 37 | height++; 38 | aout = malloc((height + 1) * sizeof(char *)); 39 | if (aout == NULL || height == 0) 40 | { 41 | free(aout); 42 | return (NULL); 43 | } 44 | for (i = a1 = 0; i < height; i++) 45 | { 46 | for (c = a1; str[c] != '\0'; c++) 47 | { 48 | if (str[c] == ' ') 49 | a1++; 50 | if (str[c] != ' ' && (str[c + 1] == ' ' || str[c + 1] == '\0')) 51 | { 52 | aout[i] = malloc((c - a1 + 2) * sizeof(char)); 53 | if (aout[i] == NULL) 54 | { 55 | ch_free_grid(aout, i); 56 | return (NULL); 57 | } 58 | break; 59 | } 60 | } 61 | for (j = 0; a1 <= c; a1++, j++) 62 | aout[i][j] = str[a1]; 63 | aout[i][j] = '\0'; 64 | } 65 | aout[i] = NULL; 66 | return (aout); 67 | } 68 | -------------------------------------------------------------------------------- /0x0B-malloc_free/2-str_concat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * str_concat - concatenates two strings. 6 | * @s1: first string. 7 | * @s2: second string. 8 | * 9 | * Return: pointer of an array of chars 10 | */ 11 | char *str_concat(char *s1, char *s2) 12 | { 13 | char *strout; 14 | unsigned int i, j, k, limit; 15 | 16 | if (s1 == NULL) 17 | s1 = ""; 18 | if (s2 == NULL) 19 | s2 = ""; 20 | 21 | for (i = 0; s1[i] != '\0'; i++) 22 | ; 23 | 24 | for (j = 0; s2[j] != '\0'; j++) 25 | ; 26 | 27 | strout = malloc(sizeof(char) * (i + j + 1)); 28 | 29 | if (strout == NULL) 30 | { 31 | free(strout); 32 | return (NULL); 33 | } 34 | 35 | for (k = 0; k < i; k++) 36 | strout[k] = s1[k]; 37 | 38 | limit = j; 39 | for (j = 0; j <= limit; k++, j++) 40 | strout[k] = s2[j]; 41 | 42 | return (strout); 43 | } 44 | -------------------------------------------------------------------------------- /0x0B-malloc_free/3-alloc_grid.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * alloc_grid - returns a pointer to a 2 dimensional array of integers. 6 | * @width: width of the array. 7 | * @height: height of the array. 8 | * 9 | * Return: pointer of an array of integers 10 | */ 11 | int **alloc_grid(int width, int height) 12 | { 13 | int **gridout; 14 | int i, j; 15 | 16 | if (width < 1 || height < 1) 17 | return (NULL); 18 | 19 | gridout = malloc(height * sizeof(int *)); 20 | if (gridout == NULL) 21 | { 22 | free(gridout); 23 | return (NULL); 24 | } 25 | 26 | for (i = 0; i < height; i++) 27 | { 28 | gridout[i] = malloc(width * sizeof(int)); 29 | if (gridout[i] == NULL) 30 | { 31 | for (i--; i >= 0; i--) 32 | free(gridout[i]); 33 | free(gridout); 34 | return (NULL); 35 | } 36 | } 37 | 38 | for (i = 0; i < height; i++) 39 | for (j = 0; j < width; j++) 40 | gridout[i][j] = 0; 41 | 42 | return (gridout); 43 | } 44 | -------------------------------------------------------------------------------- /0x0B-malloc_free/4-free_grid.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * free_grid - frees a 2 dimensional grid. 6 | * @grid: multidimensional array of integers. 7 | * @height: height of the grid. 8 | * 9 | * Return: no return 10 | */ 11 | void free_grid(int **grid, int height) 12 | { 13 | if (grid != NULL && height != 0) 14 | { 15 | for (; height >= 0; height--) 16 | free(grid[height]); 17 | free(grid); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x0B-malloc_free/README.md: -------------------------------------------------------------------------------- 1 | # 0x0B-malloc_free 2 | Project done during **Full Stack Software Engineering** at **ALX SE**. To learn about automatic and dynamic memory allocation in **C language**. 3 | 4 | ## Technologies 5 | * C files are compiled using `gcc` 6 | * C files are written according to Betty coding 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-create_array.c` | Creates an array of chars, and initializes it with a specific char | 15 | | `1-strdup.c` | Returns a pointer to a newly allocated space in memory, which contains a copy of the string given as a parameter | 16 | | `2-str_concat.c` | Concatenates two strings | 17 | | `3-alloc_grid.c` | Returns a pointer to a 2 dimensional array of integers | 18 | | `4-free_grid.c` | Frees a 2 dimensional grid previously allocated in the heap memory | 19 | | `100-argstostr.c` | Concatenates all the arguments of a program | 20 | | `101-strtow.c` | Splits a string into words | 21 | -------------------------------------------------------------------------------- /0x0B-malloc_free/main.h: -------------------------------------------------------------------------------- 1 | #ifndef _MAIN_H_ 2 | #define _MAIN_H_ 3 | 4 | char *create_array(unsigned int size, char c); 5 | char *_strdup(char *str); 6 | char *str_concat(char *s1, char *s2); 7 | int **alloc_grid(int width, int height); 8 | void free_grid(int **grid, int height); 9 | char *argstostr(int ac, char **av); 10 | 11 | #endif 12 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/0-malloc_checked.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * malloc_checked - allocates memory. 5 | * @b: amount of bytes. 6 | * 7 | * Return: pointer to the allocated memory. 8 | * if malloc fails, status value is equal to 98. 9 | */ 10 | void *malloc_checked(unsigned int b) 11 | { 12 | char *p; 13 | 14 | p = malloc(b); 15 | if (p == NULL) 16 | exit(98); 17 | return (p); 18 | } 19 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/1-string_nconcat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * string_nconcat - concatenates two strings. 6 | * @s1: first string. 7 | * @s2: second string. 8 | * @n: amount of bytes. 9 | * 10 | * Return: pointer to the allocated memory. 11 | * if malloc fails, status value is equal to 98. 12 | */ 13 | char *string_nconcat(char *s1, char *s2, unsigned int n) 14 | { 15 | char *sout; 16 | unsigned int ls1, ls2, lsout, i; 17 | 18 | if (s1 == NULL) 19 | s1 = ""; 20 | 21 | if (s2 == NULL) 22 | s2 = ""; 23 | 24 | for (ls1 = 0; s1[ls1] != '\0'; ls1++) 25 | ; 26 | 27 | for (ls2 = 0; s2[ls2] != '\0'; ls2++) 28 | ; 29 | 30 | if (n > ls2) 31 | n = ls2; 32 | 33 | lsout = ls1 + n; 34 | 35 | sout = malloc(lsout + 1); 36 | 37 | if (sout == NULL) 38 | return (NULL); 39 | 40 | for (i = 0; i < lsout; i++) 41 | if (i < ls1) 42 | sout[i] = s1[i]; 43 | else 44 | sout[i] = s2[i - ls1]; 45 | 46 | sout[i] = '\0'; 47 | 48 | return (sout); 49 | } 50 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/100-realloc.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * _realloc - reallocates a memory block. 5 | * @ptr: pointer to the memory previously allocated. 6 | * @old_size: size, in bytes, of the allocated space of ptr. 7 | * @new_size: new size, in bytes, of the new memory block. 8 | * 9 | * Return: ptr. 10 | * if new_size == old_size, returns ptr without changes. 11 | * if malloc fails, returns NULL. 12 | */ 13 | void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size) 14 | { 15 | if (new_size == 0 && ptr != NULL) 16 | { 17 | free(ptr); 18 | return (NULL); 19 | } 20 | 21 | if (ptr == NULL) 22 | ptr = malloc(new_size); 23 | 24 | if (new_size == old_size) 25 | return (ptr); 26 | 27 | free(ptr); 28 | ptr = malloc(new_size); 29 | 30 | return (ptr); 31 | } 32 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/101-mul: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tecnophille/alx-low_level_programming/0e22613fb01cad5635f187c402011ff726386b2e/0x0C-more_malloc_free/101-mul -------------------------------------------------------------------------------- /0x0C-more_malloc_free/2-calloc.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * _calloc - allocates memory for an array. 5 | * @nmemb: number of elements. 6 | * @size: size of bytes. 7 | * 8 | * Return: pointer to the allocated memory. 9 | * if nmemb or size is 0, returns NULL. 10 | * if malloc fails, returns NULL. 11 | */ 12 | void *_calloc(unsigned int nmemb, unsigned int size) 13 | { 14 | char *p; 15 | unsigned int i; 16 | 17 | if (nmemb == 0 || size == 0) 18 | return (NULL); 19 | 20 | p = malloc(nmemb * size); 21 | 22 | if (p == NULL) 23 | return (NULL); 24 | 25 | for (i = 0; i < (nmemb * size); i++) 26 | p[i] = 0; 27 | 28 | return (p); 29 | } 30 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/3-array_range.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * array_range - creates an array of integers. 5 | * @min: minimum value. 6 | * @max: maximum value. 7 | * 8 | * Return: pointer to the newly created array. 9 | * if man > mix, returns NULL. 10 | * if malloc fails, returns NULL. 11 | */ 12 | int *array_range(int min, int max) 13 | { 14 | int *ar; 15 | int i; 16 | 17 | if (min > max) 18 | return (NULL); 19 | 20 | ar = malloc(sizeof(*ar) * ((max - min) + 1)); 21 | 22 | if (ar == NULL) 23 | return (NULL); 24 | 25 | for (i = 0; min <= max; i++, min++) 26 | ar[i] = min; 27 | 28 | return (ar); 29 | } 30 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/README.md: -------------------------------------------------------------------------------- 1 | # 0x0C-more_malloc_free 2 | Project done during **Full Stack Software Engineering course** at **ALX School**. To learn about how to use `malloc`, `calloc`, `realloc` and `exit` functions in **C language**. 3 | 4 | ## Technologies 5 | * C files are compiled using `gcc` 6 | * C files are written according to the Betty coding 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-malloc_checked.c` | Allocates memory using `malloc` | 15 | | `1-string_nconcat.c` | Concatenates two strings | 16 | | `2-calloc.c` | Allocates memory for an array, using `malloc` | 17 | | `3-array_range.c` | Creates an array of integers | 18 | | `100-realloc.c` | Reallocates a memory block using `malloc` and `free` | 19 | | `101-mul.c` | Multiplies two positive numbers | 20 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/main.h: -------------------------------------------------------------------------------- 1 | #ifndef _MAIN_H_ 2 | #define _MAIN_H_ 3 | 4 | void *malloc_checked(unsigned int b); 5 | char *string_nconcat(char *s1, char *s2, unsigned int n); 6 | void *_calloc(unsigned int nmemb, unsigned int size); 7 | int *array_range(int min, int max); 8 | void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size); 9 | 10 | #endif 11 | -------------------------------------------------------------------------------- /0x0D-preprocessor/0-object_like_macro.h: -------------------------------------------------------------------------------- 1 | #ifndef _OBJECT_LIKE_MACRO_ 2 | #define _OBJECT_LIKE_MACRO_ 3 | 4 | #define SIZE 1024 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /0x0D-preprocessor/1-pi.h: -------------------------------------------------------------------------------- 1 | #ifndef _PI_ 2 | #define _PI_ 3 | 4 | #define PI 3.14159265359 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /0x0D-preprocessor/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints the name of the file. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | printf("%s\n", __FILE__); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x0D-preprocessor/3-function_like_macro.h: -------------------------------------------------------------------------------- 1 | #ifndef _FUNCTION_LIKE_MACRO_ 2 | #define _FUNCTION_LIKE_MACRO_ 3 | 4 | #define ABS(x) ((x) < (0) ? ((x) * (-1)) : (x)) 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /0x0D-preprocessor/4-sum.h: -------------------------------------------------------------------------------- 1 | #ifndef _SUM_LIKE_MACRO_ 2 | #define _SUM_LIKE_MACRO_ 3 | 4 | #define SUM(x, y) ((x) + (y)) 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /0x0D-preprocessor/README.md: -------------------------------------------------------------------------------- 1 | # Preprocessor 2 | Project done during **Full Stack Software Engineering* at **ALX SE**. Learning about how to include guard in the header files and macros in **C language**. 3 | 4 | ## Technologies 5 | * C files are compiled using `gcc` 6 | * C files are written according to the Betty coding 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-object_like_macro.h` | Header file that defines a macro named `SIZE` | 15 | | `1-pi.h` | Header file that defines a macro named `PI` | 16 | | `2-main.c` | Prints the name of the file it was compiled from | 17 | | `3-function_like_macro.h` | Function-like macro `ABS(x)` that computes the absolute value of a number `x` | 18 | | `4-sum.h` | Function-like macro `SUM(x,y)` that computes the sum of the numbers `x` and `y` | 19 | 20 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/1-init_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | 3 | /** 4 | * init_dog - initializes a variable of type struct dog. 5 | * @d: struct dog. 6 | * @name: name of the dog. 7 | * @age: age of the dog. 8 | * @owner: owner of the dog. 9 | * 10 | * Return: no return. 11 | */ 12 | void init_dog(struct dog *d, char *name, float age, char *owner) 13 | { 14 | if (d) 15 | { 16 | d->name = name; 17 | d->age = age; 18 | d->owner = owner; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/2-print_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | 4 | /** 5 | * print_dog - prints a struct dog. 6 | * @d: struct dog. 7 | * 8 | * Return: no return. 9 | */ 10 | void print_dog(struct dog *d) 11 | { 12 | if (d) 13 | { 14 | if (!(d->name)) 15 | printf("Name: (nil)\n"); 16 | else 17 | printf("Name: %s\n", d->name); 18 | 19 | printf("Age: %f\n", d->age); 20 | 21 | if (!(d->owner)) 22 | printf("Owner: (nil)\n"); 23 | else 24 | printf("Owner: %s\n", d->owner); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/4-new_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | 4 | /** 5 | * new_dog - creates a new dog. 6 | * @name: name of the dog. 7 | * @age: age of the dog. 8 | * @owner: owner of the dog. 9 | * 10 | * Return: struct dog. 11 | * if fails, returns NULL. 12 | */ 13 | dog_t *new_dog(char *name, float age, char *owner) 14 | { 15 | dog_t *p_dog; 16 | int i, lname, lowner; 17 | 18 | p_dog = malloc(sizeof(*p_dog)); 19 | if (p_dog == NULL || !(name) || !(owner)) 20 | { 21 | free(p_dog); 22 | return (NULL); 23 | } 24 | 25 | for (lname = 0; name[lname]; lname++) 26 | ; 27 | 28 | for (lowner = 0; owner[lowner]; lowner++) 29 | ; 30 | 31 | p_dog->name = malloc(lname + 1); 32 | p_dog->owner = malloc(lowner + 1); 33 | 34 | if (!(p_dog->name) || !(p_dog->owner)) 35 | { 36 | free(p_dog->owner); 37 | free(p_dog->name); 38 | free(p_dog); 39 | return (NULL); 40 | } 41 | 42 | for (i = 0; i < lname; i++) 43 | p_dog->name[i] = name[i]; 44 | p_dog->name[i] = '\0'; 45 | 46 | p_dog->age = age; 47 | 48 | for (i = 0; i < lowner; i++) 49 | p_dog->owner[i] = owner[i]; 50 | p_dog->owner[i] = '\0'; 51 | 52 | return (p_dog); 53 | } 54 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/5-free_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | 4 | /** 5 | * free_dog - frees dogs. 6 | * @d: struct dog. 7 | * 8 | * Return: no return. 9 | */ 10 | void free_dog(dog_t *d) 11 | { 12 | if (d) 13 | { 14 | free(d->name); 15 | free(d->owner); 16 | free(d); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/README.md: -------------------------------------------------------------------------------- 1 | # 0x0E-structures_typedef 2 | Project done during **Full Stack Software Engineering** at **ALX**. Learning about structures and `typedef` in **C language**. 3 | 4 | ## Technologies 5 | * C files are compiled using `gcc` 6 | * C files are written according to the Betty coding 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 | | `dog.h` | Header file that contains a `struct dog` | 15 | | `1-init_dog.c` | Initialize a variable of type `struct dog` | 16 | | `2-print_dog.c` | Prints a `struct dog` | 17 | | `4-new_dog.c` | Creates a new dog | 18 | | `5-free_dog.c` | Frees a dog | 19 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/dog.h: -------------------------------------------------------------------------------- 1 | #ifndef _DOG_H_ 2 | #define _DOG_H_ 3 | 4 | /** 5 | * dog_t - Typedef for struct dog 6 | */ 7 | typedef struct dog dog_t; 8 | 9 | /** 10 | * struct dog - struct that stores some information of a dog 11 | * @name: name of the dog 12 | * @age: age of the dog 13 | * @owner: owner of the dog 14 | * 15 | * Description: struct called "dog" that stores its name, its age 16 | * and the name of its owner. 17 | */ 18 | struct dog 19 | { 20 | char *name; 21 | float age; 22 | char *owner; 23 | }; 24 | 25 | void init_dog(struct dog *d, char *name, float age, char *owner); 26 | void print_dog(struct dog *d); 27 | dog_t *new_dog(char *name, float age, char *owner); 28 | void free_dog(dog_t *d); 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /0x0F-function_pointers/0-print_name.c: -------------------------------------------------------------------------------- 1 | #include "function_pointers.h" 2 | 3 | /** 4 | * print_name - prints a name. 5 | * @name: input name. 6 | * @f: function pointer. 7 | * 8 | * Return: no return. 9 | */ 10 | void print_name(char *name, void (*f)(char *)) 11 | { 12 | if (name && f) 13 | f(name); 14 | } 15 | -------------------------------------------------------------------------------- /0x0F-function_pointers/1-array_iterator.c: -------------------------------------------------------------------------------- 1 | #include "function_pointers.h" 2 | 3 | /** 4 | * array_iterator - executes a function given as a 5 | * parameter on each element of an array. 6 | * @array: input integer array. 7 | * @size: size of the array. 8 | * @action: pointer to the function. 9 | * 10 | * Return: no return. 11 | */ 12 | void array_iterator(int *array, size_t size, void (*action)(int)) 13 | { 14 | unsigned int i; 15 | 16 | if (array && action) 17 | for (i = 0; i < size; i++) 18 | action(array[i]); 19 | } 20 | -------------------------------------------------------------------------------- /0x0F-function_pointers/100-main_opcodes.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * @argc: argument count. 7 | * @argv: argument vector. 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(int argc, char *argv[]) 12 | { 13 | char *opc = (char *) main; 14 | int i, nbytes; 15 | 16 | if (argc != 2) 17 | { 18 | printf("Error\n"); 19 | exit(1); 20 | } 21 | 22 | nbytes = atoi(argv[1]); 23 | 24 | if (nbytes < 0) 25 | { 26 | printf("Error\n"); 27 | exit(2); 28 | } 29 | 30 | for (i = 0; i < nbytes; i++) 31 | { 32 | printf("%02x", opc[i] & 0xFF); 33 | if (i != nbytes - 1) 34 | printf(" "); 35 | } 36 | 37 | printf("\n"); 38 | return (0); 39 | } 40 | -------------------------------------------------------------------------------- /0x0F-function_pointers/2-int_index.c: -------------------------------------------------------------------------------- 1 | #include "function_pointers.h" 2 | 3 | /** 4 | * int_index - searches for an integer 5 | * @array: input integer array. 6 | * @size: size of the array. 7 | * @cmp: pointer to the function to be used 8 | * to compare values. 9 | * 10 | * Return: index of the first eement for which the cmp 11 | * function does not return 0. If no elements matches, 12 | * return -1. If size <= 0, return -1. 13 | */ 14 | int int_index(int *array, int size, int (*cmp)(int)) 15 | { 16 | int i; 17 | 18 | if (array && cmp) 19 | { 20 | if (size <= 0) 21 | return (-1); 22 | 23 | for (i = 0; i < size; i++) 24 | if (cmp(array[i])) 25 | return (i); 26 | } 27 | 28 | return (-1); 29 | } 30 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-calc.h: -------------------------------------------------------------------------------- 1 | #ifndef _CALC_H_ 2 | #define _CALC_H_ 3 | 4 | #include 5 | #include 6 | /** 7 | * struct op - Struct op 8 | * 9 | * @op: The operator 10 | * @f: The function associated 11 | */ 12 | typedef struct op 13 | { 14 | char *op; 15 | int (*f)(int a, int b); 16 | } op_t; 17 | 18 | int op_add(int a, int b); 19 | int op_sub(int a, int b); 20 | int op_mul(int a, int b); 21 | int op_div(int a, int b); 22 | int op_mod(int a, int b); 23 | int (*get_op_func(char *s))(int, int); 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-get_op_func.c: -------------------------------------------------------------------------------- 1 | #include "3-calc.h" 2 | 3 | /** 4 | * get_op_func - selects the correct function to perform 5 | * the operation asked by the user. 6 | * @s: char operator. 7 | * 8 | * Return: pointer to the function that corresponds to the operator. 9 | */ 10 | int (*get_op_func(char *s))(int, int) 11 | { 12 | op_t ops[] = { 13 | {"+", op_add}, 14 | {"-", op_sub}, 15 | {"*", op_mul}, 16 | {"/", op_div}, 17 | {"%", op_mod}, 18 | {NULL, NULL} 19 | }; 20 | int i = 0; 21 | 22 | while (i < 10) 23 | { 24 | if (s[0] == ops->op[i]) 25 | break; 26 | i++; 27 | } 28 | 29 | return (ops[i / 2].f); 30 | } 31 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-main.c: -------------------------------------------------------------------------------- 1 | #include "3-calc.h" 2 | 3 | /** 4 | * main - check the code for Holberton School students. 5 | * @argc: argument count. 6 | * @argv: argument vector. 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(int argc, char *argv[]) 11 | { 12 | int a, b; 13 | int (*operation)(int, int); 14 | 15 | if (argc != 4) 16 | { 17 | printf("Error\n"); 18 | exit(98); 19 | } 20 | 21 | if (argv[2][1]) 22 | { 23 | printf("Error\n"); 24 | exit(99); 25 | } 26 | 27 | operation = get_op_func(argv[2]); 28 | 29 | if (operation == NULL) 30 | { 31 | printf("Error\n"); 32 | exit(99); 33 | } 34 | 35 | a = atoi(argv[1]); 36 | b = atoi(argv[3]); 37 | 38 | printf("%d\n", operation(a, b)); 39 | return (0); 40 | } 41 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-op_functions.c: -------------------------------------------------------------------------------- 1 | #include "3-calc.h" 2 | 3 | /** 4 | * op_add - adds two numbers. 5 | * @a: first number. 6 | * @b: second number. 7 | * 8 | * Return: add. 9 | */ 10 | int op_add(int a, int b) 11 | { 12 | return (a + b); 13 | } 14 | 15 | /** 16 | * op_sub - subctracts two numbers. 17 | * @a: first number. 18 | * @b: second number. 19 | * 20 | * Return: difference. 21 | */ 22 | int op_sub(int a, int b) 23 | { 24 | return (a - b); 25 | } 26 | 27 | /** 28 | * op_mul - multiplies two numbers. 29 | * @a: first number. 30 | * @b: second number. 31 | * 32 | * Return: multiplication. 33 | */ 34 | int op_mul(int a, int b) 35 | { 36 | return (a * b); 37 | } 38 | 39 | /** 40 | * op_div - divides two numbers. 41 | * @a: first number. 42 | * @b: second number. 43 | * 44 | * Return: division. 45 | */ 46 | int op_div(int a, int b) 47 | { 48 | if (b == 0) 49 | { 50 | printf("Error\n"); 51 | exit(100); 52 | } 53 | return (a / b); 54 | } 55 | 56 | /** 57 | * op_mod - calculates the module of two numbers. 58 | * @a: first number. 59 | * @b: second number. 60 | * 61 | * Return: remainder of the division. 62 | */ 63 | int op_mod(int a, int b) 64 | { 65 | if (b == 0) 66 | { 67 | printf("Error\n"); 68 | exit(100); 69 | } 70 | return (a % b); 71 | } 72 | -------------------------------------------------------------------------------- /0x0F-function_pointers/README.md: -------------------------------------------------------------------------------- 1 | # 0x0F-function_pointers 2 | Project done during **Full Stack Software Engineering** at **ALX**. To learn about function pointers in **C language**. 3 | 4 | ## Technologies 5 | * C files are compiled using `gcc` 6 | * C files are written according to the Betty coding 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-print_name.c` | Prints a name | 15 | | `1-array_iterator.c` | Executes a function given as a parameter on each element of an array | 16 | | `2-int_index.c` | Searches for an integer | 17 | | `3-calc.h` | Header file that contains all function prototypes and data structures | 18 | | `3-op_functions.c` | File that contains functions that calculates arithmetic operations | 19 | | `3-get_op_func.c` | Function pointer that returns the pointer of the arithmetic functions | 20 | | `3-main.c` | Entry point | 21 | | `100-main_opcodes.c` | Prints the opcodes of its own main function | 22 | -------------------------------------------------------------------------------- /0x0F-function_pointers/function_pointers.h: -------------------------------------------------------------------------------- 1 | #ifndef _FUNCTION_POINTERS_ 2 | #define _FUNCTION_POINTERS_ 3 | 4 | #include 5 | 6 | void print_name(char *name, void (*f)(char *)); 7 | void array_iterator(int *array, size_t size, void (*action)(int)); 8 | int int_index(int *array, int size, int (*cmp)(int)); 9 | 10 | #endif 11 | -------------------------------------------------------------------------------- /0x10-variadic_functions/0-sum_them_all.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | 3 | /** 4 | * sum_them_all - returns the sum of all its parameters. 5 | * @n: amount of the arguments. 6 | * 7 | * Return: sum of its parameters. 8 | */ 9 | int sum_them_all(const unsigned int n, ...) 10 | { 11 | va_list valist; 12 | unsigned int i; 13 | int sum = 0; 14 | 15 | if (n == 0) 16 | return (0); 17 | 18 | va_start(valist, n); 19 | 20 | for (i = 0; i < n; i++) 21 | sum += va_arg(valist, int); 22 | 23 | va_end(valist); 24 | 25 | return (sum); 26 | } 27 | -------------------------------------------------------------------------------- /0x10-variadic_functions/1-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | 3 | /** 4 | * print_numbers - prints numbers. 5 | * @separator: string to be printed between numbers. 6 | * @n: number of integers passed to the function. 7 | * 8 | * Return: no return. 9 | */ 10 | void print_numbers(const char *separator, const unsigned int n, ...) 11 | { 12 | va_list valist; 13 | unsigned int i; 14 | 15 | va_start(valist, n); 16 | 17 | for (i = 0; i < n; i++) 18 | { 19 | printf("%d", va_arg(valist, int)); 20 | if (separator && i < n - 1) 21 | printf("%s", separator); 22 | } 23 | 24 | printf("\n"); 25 | va_end(valist); 26 | } 27 | -------------------------------------------------------------------------------- /0x10-variadic_functions/2-print_strings.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | 3 | /** 4 | * print_strings - prints strings. 5 | * @separator: string to be printed between the strings. 6 | * @n: number of strings passed to the function. 7 | * 8 | * Return: no return. 9 | */ 10 | void print_strings(const char *separator, const unsigned int n, ...) 11 | { 12 | va_list valist; 13 | unsigned int i; 14 | char *str; 15 | 16 | va_start(valist, n); 17 | 18 | for (i = 0; i < n; i++) 19 | { 20 | str = va_arg(valist, char *); 21 | 22 | if (str) 23 | printf("%s", str); 24 | else 25 | printf("(nil)"); 26 | 27 | if (i < n - 1) 28 | if (separator) 29 | printf("%s", separator); 30 | } 31 | 32 | printf("\n"); 33 | va_end(valist); 34 | } 35 | -------------------------------------------------------------------------------- /0x10-variadic_functions/3-print_all.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | 3 | /** 4 | * print_all - prints anything. 5 | * @format: a list of types of arguments passed to the function. 6 | * 7 | * Return: no return. 8 | */ 9 | void print_all(const char * const format, ...) 10 | { 11 | va_list valist; 12 | unsigned int i = 0, j, c = 0; 13 | char *str; 14 | const char t_arg[] = "cifs"; 15 | 16 | va_start(valist, format); 17 | while (format && format[i]) 18 | { 19 | j = 0; 20 | while (t_arg[j]) 21 | { 22 | if (format[i] == t_arg[j] && c) 23 | { 24 | printf(", "); 25 | break; 26 | } j++; 27 | } 28 | switch (format[i]) 29 | { 30 | case 'c': 31 | printf("%c", va_arg(valist, int)), c = 1; 32 | break; 33 | case 'i': 34 | printf("%d", va_arg(valist, int)), c = 1; 35 | break; 36 | case 'f': 37 | printf("%f", va_arg(valist, double)), c = 1; 38 | break; 39 | case 's': 40 | str = va_arg(valist, char *), c = 1; 41 | if (!str) 42 | { 43 | printf("(nil)"); 44 | break; 45 | } 46 | printf("%s", str); 47 | break; 48 | } i++; 49 | } 50 | printf("\n"), va_end(valist); 51 | } 52 | -------------------------------------------------------------------------------- /0x10-variadic_functions/README.md: -------------------------------------------------------------------------------- 1 | # 0x10-variadic_functions 2 | Project done during **Full Stack Software Engineering** at **ALX**. To learn about variadic functions in **C language**. 3 | 4 | ## Technologies 5 | * C files are compiled using `gcc` 6 | * C files are written according to the Betty coding 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-sum_them_all.c` | Returns the sum of all its parameters | 15 | | `1-print_numbers.c` | Prints numbers | 16 | | `2-print_strings.c` | Prints strings | 17 | | `3-print_all.c` | Prints anything | 18 | -------------------------------------------------------------------------------- /0x10-variadic_functions/variadic_functions.h: -------------------------------------------------------------------------------- 1 | #ifndef _VARIADIC_FUNCTIONS_ 2 | #define _VARIADIC_FUNCTIONS_ 3 | 4 | #include 5 | #include 6 | 7 | int sum_them_all(const unsigned int n, ...); 8 | void print_numbers(const char *separator, const unsigned int n, ...); 9 | void print_strings(const char *separator, const unsigned int n, ...); 10 | void print_all(const char * const format, ...); 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/0-print_list.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * print_list - prints all the elements of a list_t list. 4 | * @h: singly linked list. 5 | * Return: number of elements in the list. 6 | */ 7 | 8 | size_t print_list(const list_t *h) 9 | { 10 | size_t nelem; 11 | 12 | nelem = 0; 13 | while (h != NULL) 14 | { 15 | if (h->str == NULL) 16 | printf("[%d] %s\n", 0, "(nil)"); 17 | else 18 | printf("[%d] %s\n", h->len, h->str); 19 | h = h->next; 20 | nelem++; 21 | } 22 | return (nelem); 23 | } 24 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/1-list_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * list_len - returns then number of elements in a list. 4 | * @h: singly linked list. 5 | * Return: number of elements in the list. 6 | */ 7 | 8 | size_t list_len(const list_t *h) 9 | { 10 | size_t nelem; 11 | 12 | nelem = 0; 13 | while (h != NULL) 14 | { 15 | h = h->next; 16 | nelem++; 17 | } 18 | return (nelem); 19 | } 20 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/100-first.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * bmain - function executed before main 5 | * Return: no return. 6 | */ 7 | 8 | void __attribute__ ((constructor)) bmain() 9 | { 10 | printf("You're beat! and yet, you must allow"); 11 | printf(",\nI bore my house upon my back!\n"); 12 | } 13 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/101-hello_holberton.asm: -------------------------------------------------------------------------------- 1 | SECTION .data 2 | msg: db "Hello, Holberton", 0 3 | fmt: db "%s", 10, 0 4 | 5 | SECTION .text 6 | extern printf 7 | global main 8 | main: 9 | mov esi, msg 10 | mov edi, fmt 11 | mov eax, 0 12 | call printf 13 | 14 | mov eax, 0 15 | ret 16 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/2-add_node.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * add_node - adds a new node at the beginning 4 | * of a list_t list. 5 | * @head: head of the linked list. 6 | * @str: string to store in the list. 7 | * Return: address of the head. 8 | */ 9 | 10 | list_t *add_node(list_t **head, const char *str) 11 | { 12 | list_t *new; 13 | size_t nchar; 14 | 15 | new = malloc(sizeof(list_t)); 16 | if (new == NULL) 17 | return (NULL); 18 | 19 | new->str = strdup(str); 20 | 21 | for (nchar = 0; str[nchar]; nchar++) 22 | ; 23 | 24 | new->len = nchar; 25 | new->next = *head; 26 | *head = new; 27 | 28 | return (*head); 29 | } 30 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/3-add_node_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_node_end - adds a new node at the end 5 | * of a list_t list. 6 | * @head: head of the linked list. 7 | * @str: string to store in the list. 8 | * Return: address of the head. 9 | */ 10 | 11 | list_t *add_node_end(list_t **head, const char *str) 12 | { 13 | list_t *new, *temp; 14 | size_t nchar; 15 | 16 | new = malloc(sizeof(list_t)); 17 | if (new == NULL) 18 | return (NULL); 19 | 20 | new->str = strdup(str); 21 | 22 | for (nchar = 0; str[nchar]; nchar++) 23 | ; 24 | 25 | new->len = nchar; 26 | new->next = NULL; 27 | temp = *head; 28 | 29 | if (temp == NULL) 30 | { 31 | *head = new; 32 | } 33 | else 34 | { 35 | while (temp->next != NULL) 36 | temp = temp->next; 37 | temp->next = new; 38 | } 39 | 40 | return (*head); 41 | } 42 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/4-free_list.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_list - frees a list 5 | * @head: head of the linked list. 6 | * Return: no return. 7 | */ 8 | 9 | void free_list(list_t *head) 10 | { 11 | list_t *current; 12 | 13 | while ((current = head) != NULL) 14 | { 15 | head = head->next; 16 | free(current->str); 17 | free(current); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/README.md: -------------------------------------------------------------------------------- 1 | # 0x12-singly_linked_lists 2 | 3 | Project done during **Full Stack Software Engineering** at **ALX**. Learning about linked lists in **C language**. 4 | 5 | ## Technologies 6 | * C files are compiled using `gcc` 7 | * C files are written according to the Betty coding standard 8 | * Tested on Ubuntu 20.04 LTS 9 | 10 | ## Files 11 | All of the following files are programs written in C: 12 | 13 | | Filename | Description | 14 | | -------- | ----------- | 15 | | `0-print_list.c` | Prints all the elements of a `list_t` list | 16 | | `1-list_len.c` | Returns the number of elements in a linked `list_t` list | 17 | | `2-add_node.c` | Adds a new node at the beginning of a `list_t` list | 18 | | `3-add_node_end.c` | Adds a new node at the end of a `list_t` list | 19 | | `4-free_list.c` | Frees a `list_t` list | 20 | | `100-first.c` | Prints a message before main function is executed | 21 | | `101-hello_holberton.asm` | Assembly code that prints `Hello, Holberton` only using `printf` function | 22 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef _LISTS_ 2 | #define _LISTS_ 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | /** 9 | * struct list_s - singly linked list 10 | * @str: string - (malloc'ed string) 11 | * @len: length of the string 12 | * @next: points to the next node 13 | * 14 | * Description: singly linked list node structure 15 | * for Holberton project 16 | */ 17 | typedef struct list_s 18 | { 19 | char *str; 20 | unsigned int len; 21 | struct list_s *next; 22 | } list_t; 23 | 24 | size_t print_list(const list_t *h); 25 | size_t list_len(const list_t *h); 26 | list_t *add_node(list_t **head, const char *str); 27 | list_t *add_node_end(list_t **head, const char *str); 28 | void free_list(list_t *head); 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/0-print_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * print_listint - prints all the elements of a list. 5 | * @h: head of a list. 6 | * 7 | * Return: numbers of nodes. 8 | */ 9 | size_t print_listint(const listint_t *h) 10 | { 11 | size_t nnodes = 0; 12 | 13 | while (h != NULL) 14 | { 15 | printf("%d\n", h->n); 16 | h = h->next; 17 | nnodes++; 18 | } 19 | return (nnodes); 20 | } 21 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/1-listint_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * listint_len - returns the number of elements in 5 | * a linked list. 6 | * @h: head of a list. 7 | * 8 | * Return: numbers of nodes. 9 | */ 10 | size_t listint_len(const listint_t *h) 11 | { 12 | size_t nnodes = 0; 13 | 14 | while (h != NULL) 15 | { 16 | h = h->next; 17 | nnodes++; 18 | } 19 | return (nnodes); 20 | } 21 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/10-delete_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * delete_nodeint_at_index - deletes the node at index 5 | * of a linked list. 6 | * @head: head of a list. 7 | * @index: index of the list where the node is 8 | * deleted. 9 | * 10 | * Return: 1 if it succeeded, -1 if it failed. 11 | */ 12 | int delete_nodeint_at_index(listint_t **head, unsigned int index) 13 | { 14 | unsigned int i; 15 | listint_t *prev; 16 | listint_t *next; 17 | 18 | prev = *head; 19 | 20 | if (index != 0) 21 | { 22 | for (i = 0; i < index - 1 && prev != NULL; i++) 23 | { 24 | prev = prev->next; 25 | } 26 | } 27 | 28 | if (prev == NULL || (prev->next == NULL && index != 0)) 29 | { 30 | return (-1); 31 | } 32 | 33 | next = prev->next; 34 | 35 | if (index != 0) 36 | { 37 | prev->next = next->next; 38 | free(next); 39 | } 40 | else 41 | { 42 | free(prev); 43 | *head = next; 44 | } 45 | 46 | return (1); 47 | } 48 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/100-reverse_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * reverse_listint - reverses a linked list. 5 | * @head: head of a list. 6 | * 7 | * Return: pointer to the first node. 8 | */ 9 | listint_t *reverse_listint(listint_t **head) 10 | { 11 | listint_t *p; 12 | listint_t *n; 13 | 14 | p = NULL; 15 | n = NULL; 16 | 17 | while (*head != NULL) 18 | { 19 | n = (*head)->next; 20 | (*head)->next = p; 21 | p = *head; 22 | *head = n; 23 | } 24 | 25 | *head = p; 26 | return (*head); 27 | } 28 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/101-print_listint_safe.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_listp - frees a linked list 5 | * @head: head of a list. 6 | * 7 | * Return: no return. 8 | */ 9 | void free_listp(listp_t **head) 10 | { 11 | listp_t *temp; 12 | listp_t *curr; 13 | 14 | if (head != NULL) 15 | { 16 | curr = *head; 17 | while ((temp = curr) != NULL) 18 | { 19 | curr = curr->next; 20 | free(temp); 21 | } 22 | *head = NULL; 23 | } 24 | } 25 | 26 | /** 27 | * print_listint_safe - prints a linked list. 28 | * @head: head of a list. 29 | * 30 | * Return: number of nodes in the list. 31 | */ 32 | size_t print_listint_safe(const listint_t *head) 33 | { 34 | size_t nnodes = 0; 35 | listp_t *hptr, *new, *add; 36 | 37 | hptr = NULL; 38 | while (head != NULL) 39 | { 40 | new = malloc(sizeof(listp_t)); 41 | 42 | if (new == NULL) 43 | exit(98); 44 | 45 | new->p = (void *)head; 46 | new->next = hptr; 47 | hptr = new; 48 | 49 | add = hptr; 50 | 51 | while (add->next != NULL) 52 | { 53 | add = add->next; 54 | if (head == add->p) 55 | { 56 | printf("-> [%p] %d\n", (void *)head, head->n); 57 | free_listp(&hptr); 58 | return (nnodes); 59 | } 60 | } 61 | 62 | printf("[%p] %d\n", (void *)head, head->n); 63 | head = head->next; 64 | nnodes++; 65 | } 66 | 67 | free_listp(&hptr); 68 | return (nnodes); 69 | } 70 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/102-free_listint_safe.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_listp2 - frees a linked list 5 | * @head: head of a list. 6 | * 7 | * Return: no return. 8 | */ 9 | void free_listp2(listp_t **head) 10 | { 11 | listp_t *temp; 12 | listp_t *curr; 13 | 14 | if (head != NULL) 15 | { 16 | curr = *head; 17 | while ((temp = curr) != NULL) 18 | { 19 | curr = curr->next; 20 | free(temp); 21 | } 22 | *head = NULL; 23 | } 24 | } 25 | 26 | /** 27 | * free_listint_safe - frees a linked list. 28 | * @h: head of a list. 29 | * 30 | * Return: size of the list that was freed. 31 | */ 32 | size_t free_listint_safe(listint_t **h) 33 | { 34 | size_t nnodes = 0; 35 | listp_t *hptr, *new, *add; 36 | listint_t *curr; 37 | 38 | hptr = NULL; 39 | while (*h != NULL) 40 | { 41 | new = malloc(sizeof(listp_t)); 42 | 43 | if (new == NULL) 44 | exit(98); 45 | 46 | new->p = (void *)*h; 47 | new->next = hptr; 48 | hptr = new; 49 | 50 | add = hptr; 51 | 52 | while (add->next != NULL) 53 | { 54 | add = add->next; 55 | if (*h == add->p) 56 | { 57 | *h = NULL; 58 | free_listp2(&hptr); 59 | return (nnodes); 60 | } 61 | } 62 | 63 | curr = *h; 64 | *h = (*h)->next; 65 | free(curr); 66 | nnodes++; 67 | } 68 | 69 | *h = NULL; 70 | free_listp2(&hptr); 71 | return (nnodes); 72 | } 73 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/103-find_loop.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * find_listint_loop - finds the loop in a linked list. 5 | * @head: head of a list. 6 | * 7 | * Return: the address of the node where the loop starts. 8 | */ 9 | listint_t *find_listint_loop(listint_t *head) 10 | { 11 | listint_t *p2; 12 | listint_t *prev; 13 | 14 | p2 = head; 15 | prev = head; 16 | while (head && p2 && p2->next) 17 | { 18 | head = head->next; 19 | p2 = p2->next->next; 20 | 21 | if (head == p2) 22 | { 23 | head = prev; 24 | prev = p2; 25 | while (1) 26 | { 27 | p2 = prev; 28 | while (p2->next != head && p2->next != prev) 29 | { 30 | p2 = p2->next; 31 | } 32 | if (p2->next == head) 33 | break; 34 | 35 | head = head->next; 36 | } 37 | return (p2->next); 38 | } 39 | } 40 | 41 | return (NULL); 42 | } 43 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/2-add_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_nodeint - add a new node at the beginning 5 | * of a linked list 6 | * @head: head of a list. 7 | * @n: n element. 8 | * 9 | * Return: address of the new element. NUll if it failed. 10 | */ 11 | listint_t *add_nodeint(listint_t **head, const int n) 12 | { 13 | listint_t *new; 14 | 15 | new = malloc(sizeof(listint_t)); 16 | 17 | if (new == NULL) 18 | return (NULL); 19 | 20 | new->n = n; 21 | new->next = *head; 22 | *head = new; 23 | 24 | return (*head); 25 | } 26 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/3-add_nodeint_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_nodeint_end - add a new node at the end 5 | * of a linked list 6 | * @head: head of a list. 7 | * @n: n element. 8 | * 9 | * Return: address of the new element. NUll if it failed. 10 | */ 11 | listint_t *add_nodeint_end(listint_t **head, const int n) 12 | { 13 | listint_t *new; 14 | listint_t *temp; 15 | 16 | (void)temp; 17 | 18 | new = malloc(sizeof(listint_t)); 19 | 20 | if (new == NULL) 21 | return (NULL); 22 | 23 | new->n = n; 24 | new->next = NULL; 25 | temp = *head; 26 | if (*head == NULL) 27 | { 28 | *head = new; 29 | } 30 | else 31 | { 32 | while (temp->next != NULL) 33 | { 34 | temp = temp->next; 35 | } 36 | temp->next = new; 37 | } 38 | 39 | return (*head); 40 | } 41 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/4-free_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_listint - frees a linked list 5 | * @head: head of a list. 6 | * 7 | * Return: no return. 8 | */ 9 | void free_listint(listint_t *head) 10 | { 11 | listint_t *temp; 12 | 13 | while ((temp = head) != NULL) 14 | { 15 | head = head->next; 16 | free(temp); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/5-free_listint2.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_listint2 - frees a linked list 5 | * @head: head of a list. 6 | * 7 | * Return: no return. 8 | */ 9 | void free_listint2(listint_t **head) 10 | { 11 | listint_t *temp; 12 | listint_t *curr; 13 | 14 | if (head != NULL) 15 | { 16 | curr = *head; 17 | while ((temp = curr) != NULL) 18 | { 19 | curr = curr->next; 20 | free(temp); 21 | } 22 | *head = NULL; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/6-pop_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * pop_listint - deletes the head node of 5 | * a linked list 6 | * @head: head of a list. 7 | * 8 | * Return: head node's data. 9 | */ 10 | int pop_listint(listint_t **head) 11 | { 12 | int hnode; 13 | listint_t *h; 14 | listint_t *curr; 15 | 16 | if (*head == NULL) 17 | return (0); 18 | 19 | curr = *head; 20 | 21 | hnode = curr->n; 22 | 23 | h = curr->next; 24 | 25 | free(curr); 26 | 27 | *head = h; 28 | 29 | return (hnode); 30 | } 31 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/7-get_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * get_nodeint_at_index - returns the nth node of a linked list 5 | * @head: head of a list. 6 | * @index: index of the node. 7 | * 8 | * Return: nth node. If node does not exist, returns NULL. 9 | */ 10 | listint_t *get_nodeint_at_index(listint_t *head, unsigned int index) 11 | { 12 | unsigned int i; 13 | 14 | for (i = 0; i < index && head != NULL; i++) 15 | { 16 | head = head->next; 17 | } 18 | 19 | return (head); 20 | } 21 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/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; 13 | 14 | sum = 0; 15 | while (head != NULL) 16 | { 17 | sum += head->n; 18 | head = head->next; 19 | } 20 | 21 | return (sum); 22 | } 23 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/9-insert_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * insert_nodeint_at_index - inserts a new node 5 | * at a given position. 6 | * @head: head of a list. 7 | * @idx: index of the list where the new node is 8 | * added. 9 | * @n: integer element. 10 | * 11 | * Return: the address of the new node, or NULL if it 12 | * failed. 13 | */ 14 | listint_t *insert_nodeint_at_index(listint_t **head, unsigned int idx, int n) 15 | { 16 | unsigned int i; 17 | listint_t *new; 18 | listint_t *h; 19 | 20 | h = *head; 21 | 22 | if (idx != 0) 23 | { 24 | for (i = 0; i < idx - 1 && h != NULL; i++) 25 | { 26 | h = h->next; 27 | } 28 | } 29 | 30 | if (h == NULL && idx != 0) 31 | return (NULL); 32 | 33 | new = malloc(sizeof(listint_t)); 34 | if (new == NULL) 35 | return (NULL); 36 | 37 | new->n = n; 38 | 39 | if (idx == 0) 40 | { 41 | new->next = *head; 42 | *head = new; 43 | } 44 | else 45 | { 46 | new->next = h->next; 47 | h->next = new; 48 | } 49 | 50 | return (new); 51 | } 52 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/README.md: -------------------------------------------------------------------------------- 1 | # 0x03-more_singly_linked_lists 2 | Project done during **Full Stack Software Engineering** at **ALX**. Learning about singly linked lists in **C language**. 3 | 4 | ## Technologies 5 | * C files are compiled using `gcc` 6 | * C files are written according to the Betty coding 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-print_listint.c` | Prints all the elements of a `listint_t` list | 15 | | `1-listint_len.c` | Returns the number of elements in a linked `listint_t` list | 16 | | `2-add_nodeint.c` | Adds a new node at the beginning of a `listint_t` list | 17 | | `3-add_nodeint_end.c` | Adds a new node at the end of a `listint_t` list | 18 | | `4-free_listint.c` | Frees a `listint_t` list | 19 | | `5-free_listint2.c` | Frees a `listint_t` list | 20 | | `6-pop_listint.c` | Deletes the head node of a `listint_t` linked list, and returns the head node's data (n) | 21 | | `7-get_nodeint.c` | Returns the nth node of a `listint_t` linked list | 22 | | `8-sum_listint.c` | Returns the sum of all the data (n) of a `listint_t` linked list | 23 | | `9-insert_nodeint.c` | Inserts a new node at a given position | 24 | | `10-delete_nodeint.c` | Deletes the node at index `index` of a `listint_t` linked list | 25 | | `100-reverse_listint.c` | Reverses a `listint_t` linked list | 26 | | `101-print_listint_safe.c` | Prints a `listint_t` linked list | 27 | | `102-free_listint_safe.c` | Frees a `listint_t` list | 28 | | `103-find_loop.c` | Finds the loop in a linked list | 29 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef _LISTS_ 2 | #define _LISTS_ 3 | 4 | #include 5 | #include 6 | 7 | /** 8 | * struct listint_s - singly linked list 9 | * @n: integer 10 | * @next: points to the next node 11 | * 12 | * Description: singly linked list node structure 13 | * for Alx project 14 | */ 15 | typedef struct listint_s 16 | { 17 | int n; 18 | struct listint_s *next; 19 | } listint_t; 20 | 21 | /** 22 | * struct listp_s - singly linked list 23 | * @p: pointers of nodes 24 | * @next: points to the next node 25 | * 26 | * Description: singly linked list of pointers 27 | */ 28 | typedef struct listp_s 29 | { 30 | void *p; 31 | struct listp_s *next; 32 | } listp_t; 33 | 34 | size_t print_listint(const listint_t *h); 35 | size_t listint_len(const listint_t *h); 36 | listint_t *add_nodeint(listint_t **head, const int n); 37 | listint_t *add_nodeint_end(listint_t **head, const int n); 38 | void free_listint(listint_t *head); 39 | void free_listint2(listint_t **head); 40 | int pop_listint(listint_t **head); 41 | listint_t *get_nodeint_at_index(listint_t *head, unsigned int index); 42 | int sum_listint(listint_t *head); 43 | listint_t *insert_nodeint_at_index(listint_t **head, unsigned int idx, int n); 44 | int delete_nodeint_at_index(listint_t **head, unsigned int index); 45 | listint_t *reverse_listint(listint_t **head); 46 | size_t print_listint_safe(const listint_t *head); 47 | size_t free_listint_safe(listint_t **h); 48 | listint_t *find_listint_loop(listint_t *head); 49 | 50 | #endif 51 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/.101-password.swo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tecnophille/alx-low_level_programming/0e22613fb01cad5635f187c402011ff726386b2e/0x14-bit_manipulation/.101-password.swo -------------------------------------------------------------------------------- /0x14-bit_manipulation/.101-password.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tecnophille/alx-low_level_programming/0e22613fb01cad5635f187c402011ff726386b2e/0x14-bit_manipulation/.101-password.swp -------------------------------------------------------------------------------- /0x14-bit_manipulation/0-binary_to_uint.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * binary_to_uint - converts a binary number to an 5 | * unsigned int. 6 | * @b: binary. 7 | * 8 | * Return: unsigned int. 9 | */ 10 | unsigned int binary_to_uint(const char *b) 11 | { 12 | unsigned int ui; 13 | int len, base_two; 14 | 15 | if (!b) 16 | return (0); 17 | 18 | ui = 0; 19 | 20 | for (len = 0; b[len] != '\0'; len++) 21 | ; 22 | 23 | for (len--, base_two = 1; len >= 0; len--, base_two *= 2) 24 | { 25 | if (b[len] != '0' && b[len] != '1') 26 | { 27 | return (0); 28 | } 29 | 30 | if (b[len] & 1) 31 | { 32 | ui += base_two; 33 | } 34 | } 35 | 36 | return (ui); 37 | } 38 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/1-print_binary.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_binary - prints the binary representation 5 | * of a number. 6 | * @n: unsigned long int. 7 | * 8 | * Return: no return. 9 | */ 10 | void print_binary(unsigned long int n) 11 | { 12 | if (n >> 0) 13 | { 14 | if (n >> 1) 15 | print_binary(n >> 1); 16 | _putchar((n & 1) + '0'); 17 | } 18 | else 19 | { 20 | _putchar('0'); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/100-get_endianness.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * get_endianness - checks the endianness 5 | * 6 | * Return: 0 if big endian, 1 if little endian 7 | */ 8 | int get_endianness(void) 9 | { 10 | unsigned int x; 11 | char *c; 12 | 13 | x = 1; 14 | c = (char *) &x; 15 | 16 | return ((int)*c); 17 | } 18 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/101-password: -------------------------------------------------------------------------------- 1 | Hol 2 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/2-get_bit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * get_bit - returns the value of a bit at a given 5 | * index. 6 | * @n: unsigned long int input. 7 | * @index: index of the bit. 8 | * 9 | * Return: value of the bit. 10 | */ 11 | int get_bit(unsigned long int n, unsigned int index) 12 | { 13 | unsigned int i; 14 | 15 | if (n == 0 && index < 64) 16 | return (0); 17 | 18 | for (i = 0; i <= 63; n >>= 1, i++) 19 | { 20 | if (index == i) 21 | { 22 | return (n & 1); 23 | } 24 | } 25 | 26 | return (-1); 27 | } 28 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/3-set_bit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * set_bit - sets the value of a bit to 1. 5 | * at a given index. 6 | * @n: pointer of an unsigned long int. 7 | * @index: index of the bit. 8 | * 9 | * Return: 1 if it worked, -1 if it didn't. 10 | */ 11 | int set_bit(unsigned long int *n, unsigned int index) 12 | { 13 | unsigned int m; 14 | 15 | if (index > 63) 16 | return (-1); 17 | 18 | m = 1 << index; 19 | *n = (*n | m); 20 | 21 | return (1); 22 | } 23 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/4-clear_bit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * clear_bit - sets the value of a bit to 0. 5 | * at a given index. 6 | * @n: pointer of an unsigned long int. 7 | * @index: index of the bit. 8 | * 9 | * Return: 1 if it worked, -1 if it didn't. 10 | */ 11 | int clear_bit(unsigned long int *n, unsigned int index) 12 | { 13 | unsigned int m; 14 | 15 | if (index > 63) 16 | return (-1); 17 | 18 | m = 1 << index; 19 | 20 | if (*n & m) 21 | *n ^= m; 22 | 23 | return (1); 24 | } 25 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/5-flip_bits.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * flip_bits - returns the number of bits you would 5 | * need to flip to get from one number to another 6 | * @n: number one. 7 | * @m: number two. 8 | * 9 | * Return: number of bits. 10 | */ 11 | unsigned int flip_bits(unsigned long int n, unsigned long int m) 12 | { 13 | unsigned int nbits; 14 | 15 | for (nbits = 0; n || m; n >>= 1, m >>= 1) 16 | { 17 | if ((n & 1) != (m & 1)) 18 | nbits++; 19 | } 20 | 21 | return (nbits); 22 | } 23 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/README.md: -------------------------------------------------------------------------------- 1 | # Bit manipulation 2 | Project done during **Full Stack Software Engineering studies** at **ALX**. Learning about how to manipulate bits and use bitwise operators in **C language**. 3 | 4 | ## Technologies 5 | * Scripts written in Bash 4.3.11(1) 6 | * C files are compiled using `gcc` 7 | * C files are written according to the Betty coding standard 8 | * Tested on Ubuntu 20.04 LTS 9 | 10 | ## Files 11 | All of the following files are scripts and programs written in C: 12 | 13 | | Filename | Description | 14 | | -------- | ----------- | 15 | | `0-binary_to_uint.c` | Converts a binary number to an `unsigned int` | 16 | | `1-print_binary.c` | Prints the binary representation of a number | 17 | | `2-get_bit.c` | Returns the value of a bit at a given index | 18 | | `3-set_bit.c` | Sets the value of a bit to `1` at a given index | 19 | | `4-clear_bit.c` | Sets the value of a bit to `0` at a given index | 20 | | `5-flip_bits.c` | Returns the number of bits needed to flip to get from one number to another | 21 | | `100-get_endianess.c` | Checks the endianness | 22 | | `101-password` | Script that contains the password of `crackme3` file | 23 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/main.h: -------------------------------------------------------------------------------- 1 | #ifndef _MAIN_ 2 | #define _MAIN_ 3 | 4 | unsigned int binary_to_uint(const char *b); 5 | void print_binary(unsigned long int n); 6 | int _putchar(char c); 7 | int get_bit(unsigned long int n, unsigned int index); 8 | int set_bit(unsigned long int *n, unsigned int index); 9 | int clear_bit(unsigned long int *n, unsigned int index); 10 | unsigned int flip_bits(unsigned long int n, unsigned long int m); 11 | int get_endianness(void); 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /0x15-file_io/0-read_textfile.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * read_textfile - reads a text file and prints the letters 5 | * @filename: filename. 6 | * @letters: numbers of letters printed. 7 | * 8 | * Return: numbers of letters printed. It fails, returns 0. 9 | */ 10 | ssize_t read_textfile(const char *filename, size_t letters) 11 | { 12 | int fd; 13 | ssize_t nrd, nwr; 14 | char *buf; 15 | 16 | if (!filename) 17 | return (0); 18 | 19 | fd = open(filename, O_RDONLY); 20 | 21 | if (fd == -1) 22 | return (0); 23 | 24 | buf = malloc(sizeof(char) * (letters)); 25 | if (!buf) 26 | return (0); 27 | 28 | nrd = read(fd, buf, letters); 29 | nwr = write(STDOUT_FILENO, buf, nrd); 30 | 31 | close(fd); 32 | 33 | free(buf); 34 | 35 | return (nwr); 36 | } 37 | -------------------------------------------------------------------------------- /0x15-file_io/1-create_file.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * create_file - creates a file 5 | * @filename: filename. 6 | * @text_content: content writed in the file. 7 | * 8 | * Return: 1 if it success. -1 if it fails. 9 | */ 10 | int create_file(const char *filename, char *text_content) 11 | { 12 | int fd; 13 | int nletters; 14 | int rwr; 15 | 16 | if (!filename) 17 | return (-1); 18 | 19 | fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0600); 20 | 21 | if (fd == -1) 22 | return (-1); 23 | 24 | if (!text_content) 25 | text_content = ""; 26 | 27 | for (nletters = 0; text_content[nletters]; nletters++) 28 | ; 29 | 30 | rwr = write(fd, text_content, nletters); 31 | 32 | if (rwr == -1) 33 | return (-1); 34 | 35 | close(fd); 36 | 37 | return (1); 38 | } 39 | -------------------------------------------------------------------------------- /0x15-file_io/2-append_text_to_file.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * append_text_to_file - appends text at the end of a file 5 | * @filename: filename. 6 | * @text_content: added content. 7 | * 8 | * Return: 1 if the file exists. -1 if the fails does not exist 9 | * or if it fails. 10 | */ 11 | int append_text_to_file(const char *filename, char *text_content) 12 | { 13 | int fd; 14 | int nletters; 15 | int rwr; 16 | 17 | if (!filename) 18 | return (-1); 19 | 20 | fd = open(filename, O_WRONLY | O_APPEND); 21 | 22 | if (fd == -1) 23 | return (-1); 24 | 25 | if (text_content) 26 | { 27 | for (nletters = 0; text_content[nletters]; nletters++) 28 | ; 29 | 30 | rwr = write(fd, text_content, nletters); 31 | 32 | if (rwr == -1) 33 | return (-1); 34 | } 35 | 36 | close(fd); 37 | 38 | return (1); 39 | } 40 | -------------------------------------------------------------------------------- /0x15-file_io/3-cp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * error_file - checks if files can be opened. 6 | * @file_from: file_from. 7 | * @file_to: file_to. 8 | * @argv: arguments vector. 9 | * Return: no return. 10 | */ 11 | void error_file(int file_from, int file_to, char *argv[]) 12 | { 13 | if (file_from == -1) 14 | { 15 | dprintf(STDERR_FILENO, "Error: Can't read from file %s\n", argv[1]); 16 | exit(98); 17 | } 18 | if (file_to == -1) 19 | { 20 | dprintf(STDERR_FILENO, "Error: Can't write to %s\n", argv[2]); 21 | exit(99); 22 | } 23 | } 24 | 25 | /** 26 | * main - check the code for Holberton School students. 27 | * @argc: number of arguments. 28 | * @argv: arguments vector. 29 | * Return: Always 0. 30 | */ 31 | int main(int argc, char *argv[]) 32 | { 33 | int file_from, file_to, err_close; 34 | ssize_t nchars, nwr; 35 | char buf[1024]; 36 | 37 | if (argc != 3) 38 | { 39 | dprintf(STDERR_FILENO, "%s\n", "Usage: cp file_from file_to"); 40 | exit(97); 41 | } 42 | 43 | file_from = open(argv[1], O_RDONLY); 44 | file_to = open(argv[2], O_CREAT | O_WRONLY | O_TRUNC | O_APPEND, 0664); 45 | error_file(file_from, file_to, argv); 46 | 47 | nchars = 1024; 48 | while (nchars == 1024) 49 | { 50 | nchars = read(file_from, buf, 1024); 51 | if (nchars == -1) 52 | error_file(-1, 0, argv); 53 | nwr = write(file_to, buf, nchars); 54 | if (nwr == -1) 55 | error_file(0, -1, argv); 56 | } 57 | 58 | err_close = close(file_from); 59 | if (err_close == -1) 60 | { 61 | dprintf(STDERR_FILENO, "Error: Can't close fd %d\n", file_from); 62 | exit(100); 63 | } 64 | 65 | err_close = close(file_to); 66 | if (err_close == -1) 67 | { 68 | dprintf(STDERR_FILENO, "Error: Can't close fd %d\n", file_from); 69 | exit(100); 70 | } 71 | return (0); 72 | } 73 | -------------------------------------------------------------------------------- /0x15-file_io/README.md: -------------------------------------------------------------------------------- 1 | # 0x15-file_io 2 | 3 | Project done during **Full Stack Software Engineering studies** at **ALX**. Learning about how to handle files (open, close, read and write), file descriptors, system calls and file permissions in **C language**. 4 | 5 | ## Technologies 6 | * C files are compiled using `gcc` 7 | * C files are written according to the Betty coding standard 8 | * Tested on Ubuntu 20.04 LTS 9 | 10 | ## Files 11 | All of the following files are programs written in C: 12 | 13 | | Filename | Description | 14 | | -------- | ----------- | 15 | | `0-read_textfile.c` | Reads a text file and prints it to the `POSIX` standard output | 16 | | `1-create_file.c` | Creates a file | 17 | | `2-append_text_to_file.c` | Appends text at the end of a file | 18 | | `3-cp.c` | Copies the content of a file to another file | 19 | | `100-elf_header.c` | Displays the information contained in the `ELF` header at the start of an `ELF` file | 20 | -------------------------------------------------------------------------------- /0x15-file_io/main.h: -------------------------------------------------------------------------------- 1 | #ifndef _MAIN_ 2 | #define _MAIN_ 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | ssize_t read_textfile(const char *filename, size_t letters); 11 | int create_file(const char *filename, char *text_content); 12 | int append_text_to_file(const char *filename, char *text_content); 13 | 14 | #endif 15 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/0-print_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * print_dlistint - print elements of doubly linked list 5 | * @h: the head of list 6 | * Return: number of nodes 7 | */ 8 | size_t print_dlistint(const dlistint_t *h) 9 | { 10 | /* declarations */ 11 | size_t nodes = 0; 12 | 13 | /* walk through list and print */ 14 | while (h) 15 | { 16 | printf("%d\n", h->n); 17 | h = h->next; 18 | nodes++; 19 | } 20 | return (nodes); 21 | } 22 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/1-dlistint_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * dlistint_len - get number of nodes 5 | * @h: the head of list 6 | * Return: number of nodes 7 | */ 8 | size_t dlistint_len(const dlistint_t *h) 9 | { 10 | /* declarations */ 11 | int nodes = 0; 12 | 13 | /* count nodes as we move through list */ 14 | while (h) 15 | { 16 | nodes += 1; 17 | h = h->next; 18 | } 19 | return (nodes); 20 | } 21 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/100-password: -------------------------------------------------------------------------------- 1 | en C Pyfo neZ 2 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/102-result: -------------------------------------------------------------------------------- 1 | 906609 -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/103-keygen.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /** 6 | * f4 - finds the biggest number 7 | * 8 | * @usrn: username 9 | * @len: length of username 10 | * Return: the biggest number 11 | */ 12 | int f4(char *usrn, int len) 13 | { 14 | int ch; 15 | int vch; 16 | unsigned int rand_num; 17 | 18 | ch = *usrn; 19 | vch = 0; 20 | 21 | while (vch < len) 22 | { 23 | if (ch < usrn[vch]) 24 | ch = usrn[vch]; 25 | vch += 1; 26 | } 27 | 28 | srand(ch ^ 14); 29 | rand_num = rand(); 30 | 31 | return (rand_num & 63); 32 | } 33 | 34 | /** 35 | * f5 - multiplies each char of username 36 | * 37 | * @usrn: username 38 | * @len: length of username 39 | * Return: multiplied char 40 | */ 41 | int f5(char *usrn, int len) 42 | { 43 | int ch; 44 | int vch; 45 | 46 | ch = vch = 0; 47 | 48 | while (vch < len) 49 | { 50 | ch = ch + usrn[vch] * usrn[vch]; 51 | vch += 1; 52 | } 53 | 54 | return (((unsigned int)ch ^ 239) & 63); 55 | } 56 | 57 | /** 58 | * f6 - generates a random char 59 | * 60 | * @usrn: username 61 | * Return: a random char 62 | */ 63 | int f6(char *usrn) 64 | { 65 | int ch; 66 | int vch; 67 | 68 | ch = vch = 0; 69 | 70 | while (vch < *usrn) 71 | { 72 | ch = rand(); 73 | vch += 1; 74 | } 75 | 76 | return (((unsigned int)ch ^ 229) & 63); 77 | } 78 | 79 | /** 80 | * main - Entry point 81 | * 82 | * @argc: arguments count 83 | * @argv: arguments vector 84 | * Return: Always 0 85 | */ 86 | int main(int argc, char **argv) 87 | { 88 | char keygen[7]; 89 | int len, ch, vch; 90 | long alph[] = { 91 | 0x3877445248432d41, 0x42394530534e6c37, 0x4d6e706762695432, 92 | 0x74767a5835737956, 0x2b554c59634a474f, 0x71786636576a6d34, 93 | 0x723161513346655a, 0x6b756f494b646850 }; 94 | (void) argc; 95 | 96 | for (len = 0; argv[1][len]; len++) 97 | ; 98 | /* ----------- f1 ----------- */ 99 | keygen[0] = ((char *)alph)[(len ^ 59) & 63]; 100 | /* ----------- f2 ----------- */ 101 | ch = vch = 0; 102 | while (vch < len) 103 | { 104 | ch = ch + argv[1][vch]; 105 | vch = vch + 1; 106 | } 107 | keygen[1] = ((char *)alph)[(ch ^ 79) & 63]; 108 | /* ----------- f3 ----------- */ 109 | ch = 1; 110 | vch = 0; 111 | while (vch < len) 112 | { 113 | ch = argv[1][vch] * ch; 114 | vch = vch + 1; 115 | } 116 | keygen[2] = ((char *)alph)[(ch ^ 85) & 63]; 117 | /* ----------- f4 ----------- */ 118 | keygen[3] = ((char *)alph)[f4(argv[1], len)]; 119 | /* ----------- f5 ----------- */ 120 | keygen[4] = ((char *)alph)[f5(argv[1], len)]; 121 | /* ----------- f6 ----------- */ 122 | keygen[5] = ((char *)alph)[f6(argv[1])]; 123 | keygen[6] = '\0'; 124 | for (ch = 0; keygen[ch]; ch++) 125 | printf("%c", keygen[ch]); 126 | return (0); 127 | } 128 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/2-add_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_dnodeint - add a node at head of d list 5 | * @head: the head 6 | * @n: the int to add 7 | * Return: addy of new node or NULL on fail 8 | */ 9 | dlistint_t *add_dnodeint(dlistint_t **head, const int n) 10 | { 11 | /* declarations */ 12 | dlistint_t *new = malloc(sizeof(dlistint_t)); 13 | 14 | /* check for null && malloc fail, free new if it exists, return null */ 15 | if (!head || !new) 16 | return (new ? free(new), NULL : NULL); 17 | /* assign some things to new node */ 18 | new->n = n; 19 | new->prev = NULL; 20 | /* if no other nodes, make this one the ending node as well */ 21 | if (!*head) 22 | { 23 | *head = new; 24 | new->next = NULL; 25 | } 26 | /* otherwise, fit it in nicely */ 27 | else 28 | { 29 | new->next = *head; 30 | (*head)->prev = new; 31 | *head = new; 32 | } 33 | return (new); 34 | } 35 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/3-add_dnodeint_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_dnodeint_end - add a node at the end of a d list 5 | * @head: the head 6 | * @n: the int to add 7 | * Return: address of new node or NULL 8 | */ 9 | dlistint_t *add_dnodeint_end(dlistint_t **head, const int n) 10 | { 11 | /*declarations */ 12 | dlistint_t *new = malloc(sizeof(dlistint_t)); 13 | dlistint_t *location; 14 | 15 | /* check for null && malloc fail, free new if it exists, return NULL */ 16 | if (!head || !new) 17 | return (new ? free(new), NULL : NULL); 18 | /* assign some things to new */ 19 | new->n = n; 20 | new->next = NULL; 21 | /* if there are no other nodes make new the first and last */ 22 | if (!*head) 23 | { 24 | new->prev = NULL; 25 | *head = new; 26 | } 27 | /* otherwise, find the end and install new */ 28 | else 29 | { 30 | location = *head; 31 | while (location->next) 32 | { 33 | location = location->next; 34 | } 35 | location->next = new; 36 | new->prev = location; 37 | } 38 | return (new); 39 | } 40 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/4-free_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_dlistint - free a doubly linked list 5 | * @head: the head of list 6 | * Return: void 7 | */ 8 | void free_dlistint(dlistint_t *head) 9 | { 10 | dlistint_t *phree; 11 | 12 | while (head) 13 | { 14 | phree = head; 15 | head = head->next; 16 | free(phree); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/5-get_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * get_dnodeint_at_index - get the nth node 5 | * @head: the head of the list 6 | * @index: the number of the node to get 7 | * Return: the addy of node or NULL 8 | */ 9 | dlistint_t *get_dnodeint_at_index(dlistint_t *head, unsigned int index) 10 | { 11 | /* declarations */ 12 | unsigned int i = 0; 13 | /* check for NULL */ 14 | if (!head) 15 | return (NULL); 16 | /* move through list to index, or bust */ 17 | for (i = 0; i < index; i++) 18 | { 19 | if (head->next) 20 | head = head->next; 21 | else 22 | return (NULL); 23 | } 24 | return (head); 25 | 26 | } 27 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/6-sum_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * sum_dlistint - sum all the ints in list 5 | * @head: head of the list 6 | * Return: sum or 0 if empty 7 | */ 8 | int sum_dlistint(dlistint_t *head) 9 | { 10 | /* declarations */ 11 | int sum = 0; 12 | 13 | /*if list is empty */ 14 | if (!head) 15 | return (0); 16 | /* otherwise, add them up */ 17 | while (head) 18 | { 19 | sum += head->n; 20 | head = head->next; 21 | } 22 | return (sum); 23 | } 24 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/7-insert_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * insert_dnodeint_at_index - insert a node at an index 5 | * @h: head of list 6 | * @idx: the index 7 | * @n: the int to put into the new node 8 | * Return: address of new node or NULL 9 | */ 10 | dlistint_t *insert_dnodeint_at_index(dlistint_t **h, unsigned int idx, int n) 11 | { 12 | /* declarations */ 13 | dlistint_t *location; 14 | dlistint_t *new = malloc(sizeof(dlistint_t)); 15 | 16 | /* check for NULL */ 17 | if (!h || !new) 18 | return (new ? free(new), NULL : NULL); 19 | /* assign some things to new */ 20 | location = *h; 21 | new->n = n; 22 | /* if index is 0 */ 23 | if (!idx) 24 | { 25 | new->prev = NULL; 26 | new->next = location ? location : NULL; 27 | if (location) 28 | { 29 | location->prev = new; 30 | } 31 | return (*h = new); 32 | } 33 | /* otherwise, move to place before index and install new node */ 34 | for (; location; location = location->next, idx--) 35 | { 36 | if (idx - 1 == 0) 37 | { 38 | new->prev = location; 39 | new->next = location->next; 40 | if (new->next) 41 | new->next->prev = new; 42 | location->next = new; 43 | return (new); 44 | } 45 | } 46 | /* if all else fails, free new & return NULL */ 47 | return (free(new), NULL); 48 | } 49 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/8-delete_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * delete_dnodeint_at_index - delete node at index 5 | * @head: head of list 6 | * @index: where to delete 7 | * Return: the 1 on success, -1 on failure 8 | */ 9 | int delete_dnodeint_at_index(dlistint_t **head, unsigned int index) 10 | { 11 | /* declarations */ 12 | dlistint_t *location, *temp; 13 | 14 | if (head) 15 | { 16 | /* set location to head, check for index == 0 */ 17 | location = *head; 18 | if (index < 1) 19 | { 20 | if (!location) 21 | return (-1); 22 | *head = location->next; 23 | if (*head) 24 | (*head)->prev = NULL; 25 | free(location); 26 | return (1); 27 | } 28 | /* look for the index, delete the right node, or fail if !index node */ 29 | for (; location; location = location->next, index--) 30 | { 31 | if (index - 1 == 0) 32 | { 33 | temp = location->next; 34 | if (!temp) 35 | break; 36 | location->next = temp->next; 37 | if (temp->next) 38 | temp->next->prev = location; 39 | free(temp); 40 | return (1); 41 | } 42 | } 43 | } 44 | return (-1); 45 | } 46 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/README.md: -------------------------------------------------------------------------------- 1 | # Doubly linked lists 2 | 3 | Project done during **Full Stack Software Engineering** at **ALX**. Learning about doubly linked list in **C language**. 4 | 5 | ## Technologies 6 | * Scripts written in Bash 7 | * C files are compiled using `gcc` 8 | * Tested on Ubuntu 20.04 LTS 9 | 10 | ## Files 11 | All of the following files are scripts and programs written in C: 12 | 13 | | Filename | Description | 14 | | -------- | ----------- | 15 | | `0-print_dlistint.c` | Prints all the elements of a `dlistint_t` list | 16 | | `1-dlistint_len.c` | Returns the number of elements in a linked `dlistint_t` list | 17 | | `2-add_dnodeint.c` | Adds a new node at the beginning of a `dlistint_t` list | 18 | | `3-add_dnodeint_end.c` | Adds a new node at the end of a `dlistint_t` list | 19 | | `4-free_dlistint.c` | Frees a `dlistint_t` list | 20 | | `5-get_dnodeint.c` | Returns the nth node of a `dlistint_t` linked list | 21 | | `6-sum_dlistint.c` | Returns the sum of all the data (n) of a `dlistint_t` linked list | 22 | | `7-insert_dnodeint.c` | Inserts a new node at a given position | 23 | | `8-delete_dnodeint.c` | Deletes the node at index `index` of a `dlistint_t` linked list | 24 | | `100-password` | Stores the password for `crackme4` executable file | 25 | | `102-result` | Stores the largest palindrome made from the product of two 3-digit numbers | 26 | | `103-keygen.c` | Generates a keygen for `crackme5` executable file | 27 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/keygen5: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tecnophille/alx-low_level_programming/0e22613fb01cad5635f187c402011ff726386b2e/0x17-doubly_linked_lists/keygen5 -------------------------------------------------------------------------------- /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 | * 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 /* LISTS_H */ 34 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/1-create_dynamic_lib.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -Wall -Werror -Wextra -pedantic -fPIC -c *.c 3 | gcc -shared -Wl,-soname,liball.so -o liball.so *.o 4 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/100-operations.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tecnophille/alx-low_level_programming/0e22613fb01cad5635f187c402011ff726386b2e/0x18-dynamic_libraries/100-operations.so -------------------------------------------------------------------------------- /0x18-dynamic_libraries/101-make_me_win.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | wget -P /tmp/ https://raw.github.com/Tecnophille/alx-low_level_programming/master/0x18-dynamic_libraries/preload.so 3 | export LD_PRELOAD=/tmp/preload.so 4 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/README.md: -------------------------------------------------------------------------------- 1 | # 0x18. C - Dynamic libraries 2 | 3 | ## Description 4 | What you should learn from this project: 5 | 6 | * What is a dynamic library, how does it work, how to create one, and how to use it 7 | * What is the environment variable $LD_LIBRARY_PATH and how to use it 8 | * What are the differences between static and shared libraries 9 | * Basic usage nm, ldd, ldconfig 10 | 11 | --- 12 | 13 | ### [0. A library is not a luxury but one of the necessities of life](./libholberton.so) 14 | * Create the dynamic library libholberton.so containing all the functions listed below: 15 | 16 | 17 | ### [1. Without libraries what have we? We have no past and no future](./1-create_dynamic_lib.sh) 18 | * Create a script that creates a dynamic library called liball.so from all the .c files that are in the current directory. 19 | 20 | 21 | ### [2. Either write something worth reading or do something worth writing](./100-operations.so) 22 | * Write a blog post describing the differences between static and dynamic libraries. It should cover: 23 | 24 | 25 | ### [3. Let's call C functions from Python](./101-make_me_win.sh) 26 | * I know, you’re missing C when coding in Python. So let’s fix that! 27 | 28 | 29 | --- 30 | 31 | ## Author 32 | * **Oliver Samuel** - [Tecnophille](https://github.com/Tecnophille) 33 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/libdynamic.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tecnophille/alx-low_level_programming/0e22613fb01cad5635f187c402011ff726386b2e/0x18-dynamic_libraries/libdynamic.so -------------------------------------------------------------------------------- /0x18-dynamic_libraries/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | #include 5 | #include 6 | 7 | int _putchar(char c); 8 | int _islower(int c); 9 | int _isalpha(int c); 10 | int _abs(int n); 11 | int _isupper(int c); 12 | int _isdigit(int c); 13 | int _strlen(char *s); 14 | void _puts(char *s); 15 | char *_strcpy(char *dest, char *src); 16 | int _atoi(char *s); 17 | char *_strcat(char *dest, char *src); 18 | char *_strncat(char *dest, char *src, int n); 19 | char *_strncpy(char *dest, char *src, int n); 20 | int _strcmp(char *s1, char *s2); 21 | char *_memset(char *s, char b, unsigned int n); 22 | char *_memcpy(char *dest, char *src, unsigned int n); 23 | char *_strchr(char *s, char c); 24 | unsigned int _strspn(char *s, char *accept); 25 | char *_strpbrk(char *s, char *accept); 26 | char *_strstr(char *haystack, char *needle); 27 | 28 | #endif /* MAIN_H */ 29 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/preload.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tecnophille/alx-low_level_programming/0e22613fb01cad5635f187c402011ff726386b2e/0x18-dynamic_libraries/preload.so -------------------------------------------------------------------------------- /0x1A-hash_tables/0-hash_table_create.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_create - creates a hash table with a given size 5 | * 6 | * @size: size of the hash table 7 | * Return: the created hash table, or NULL if function fails 8 | */ 9 | hash_table_t *hash_table_create(unsigned long int size) 10 | { 11 | hash_table_t *table; 12 | hash_node_t **array; 13 | unsigned long int i; 14 | 15 | table = malloc(sizeof(hash_table_t)); 16 | if (table == NULL) 17 | return (NULL); 18 | 19 | array = malloc(sizeof(hash_node_t *) * size); 20 | if (array == NULL) 21 | return (NULL); 22 | 23 | for (i = 0; i < size; i++) 24 | array[i] = NULL; 25 | 26 | table->array = array; 27 | table->size = size; 28 | 29 | return (table); 30 | } 31 | -------------------------------------------------------------------------------- /0x1A-hash_tables/1-djb2.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_djb2 - function that generates a random number through 5 | * a string given (hash number) 6 | * 7 | * @str: string 8 | * Return: hash number 9 | */ 10 | unsigned long int hash_djb2(const unsigned char *str) 11 | { 12 | unsigned long int hash; 13 | int c; 14 | 15 | hash = 5381; 16 | while ((c = *str++)) 17 | { 18 | hash = ((hash << 5) + hash) + c; /* hash * 33 + c */ 19 | } 20 | return (hash); 21 | } 22 | -------------------------------------------------------------------------------- /0x1A-hash_tables/2-key_index.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * key_index - function that generates a distributed index 5 | * according to a given hash 6 | * 7 | * @key: key passed 8 | * @size: size of the hash tables 9 | * Return: key index 10 | */ 11 | unsigned long int key_index(const unsigned char *key, unsigned long int size) 12 | { 13 | if (key == NULL || size == 0) 14 | return (0); 15 | return (hash_djb2(key) % size); 16 | } 17 | -------------------------------------------------------------------------------- /0x1A-hash_tables/3-hash_table_set.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * add_n_hash - adds a node at the beginning of a hash at a given index 5 | * 6 | * @head: head of the hash linked list 7 | * @key: key of the hash 8 | * @value: value to store 9 | * Return: head of the hash 10 | */ 11 | hash_node_t *add_n_hash(hash_node_t **head, const char *key, const char *value) 12 | { 13 | hash_node_t *tmp; 14 | 15 | tmp = *head; 16 | 17 | while (tmp != NULL) 18 | { 19 | if (strcmp(key, tmp->key) == 0) 20 | { 21 | free(tmp->value); 22 | tmp->value = strdup(value); 23 | return (*head); 24 | } 25 | tmp = tmp->next; 26 | } 27 | 28 | tmp = malloc(sizeof(hash_node_t)); 29 | 30 | if (tmp == NULL) 31 | return (NULL); 32 | 33 | tmp->key = strdup(key); 34 | tmp->value = strdup(value); 35 | tmp->next = *head; 36 | *head = tmp; 37 | 38 | return (*head); 39 | } 40 | 41 | /** 42 | * hash_table_set - adds a hash (key, value) to a given hash table 43 | * 44 | * @ht: pointer to the hash table 45 | * @key: key of the hash 46 | * @value: value to store 47 | * Return: 1 if successes, 0 if fails 48 | */ 49 | int hash_table_set(hash_table_t *ht, const char *key, const char *value) 50 | { 51 | unsigned long int k_index; 52 | 53 | if (ht == NULL) 54 | return (0); 55 | 56 | if (key == NULL || *key == '\0') 57 | return (0); 58 | 59 | k_index = key_index((unsigned char *)key, ht->size); 60 | 61 | if (add_n_hash(&(ht->array[k_index]), key, value) == NULL) 62 | return (0); 63 | 64 | return (1); 65 | } 66 | -------------------------------------------------------------------------------- /0x1A-hash_tables/4-hash_table_get.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_get - retrieves a value associated with a key 5 | * 6 | * @ht: pointer to the hash table 7 | * @key: key of the hash 8 | * Return: value of the hash. 9 | */ 10 | char *hash_table_get(const hash_table_t *ht, const char *key) 11 | { 12 | unsigned long int k_index; 13 | hash_node_t *tmp; 14 | 15 | if (ht == NULL) 16 | return (NULL); 17 | 18 | if (key == NULL || *key == '\0') 19 | return (NULL); 20 | 21 | k_index = key_index((unsigned char *)key, ht->size); 22 | 23 | tmp = ht->array[k_index]; 24 | 25 | while (tmp != NULL) 26 | { 27 | if (strcmp(tmp->key, key) == 0) 28 | return (tmp->value); 29 | tmp = tmp->next; 30 | } 31 | 32 | return (NULL); 33 | } 34 | -------------------------------------------------------------------------------- /0x1A-hash_tables/5-hash_table_print.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_print - prints the keys and values of the hash table 5 | * 6 | * @ht: pointer to the hash table 7 | * Return: no return 8 | */ 9 | void hash_table_print(const hash_table_t *ht) 10 | { 11 | unsigned long int i; 12 | hash_node_t *tmp; 13 | char *sep; 14 | 15 | if (ht == NULL) 16 | return; 17 | 18 | printf("{"); 19 | sep = ""; 20 | 21 | for (i = 0; i < ht->size; i++) 22 | { 23 | tmp = ht->array[i]; 24 | while (tmp != NULL) 25 | { 26 | printf("%s'%s': '%s'", sep, tmp->key, tmp->value); 27 | sep = ", "; 28 | tmp = tmp->next; 29 | } 30 | } 31 | printf("}\n"); 32 | } 33 | -------------------------------------------------------------------------------- /0x1A-hash_tables/6-hash_table_delete.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_delete - deletes a hash table 5 | * 6 | * @ht: pointer to the hash table 7 | * Return: no return 8 | */ 9 | void hash_table_delete(hash_table_t *ht) 10 | { 11 | unsigned long int i; 12 | hash_node_t *tmp1; 13 | hash_node_t *tmp2; 14 | 15 | if (ht == NULL) 16 | return; 17 | 18 | for (i = 0; i < ht->size; i++) 19 | { 20 | tmp1 = ht->array[i]; 21 | while ((tmp2 = tmp1) != NULL) 22 | { 23 | tmp1 = tmp1->next; 24 | free(tmp2->key); 25 | free(tmp2->value); 26 | free(tmp2); 27 | } 28 | } 29 | free(ht->array); 30 | free(ht); 31 | } 32 | -------------------------------------------------------------------------------- /0x1A-hash_tables/README.md: -------------------------------------------------------------------------------- 1 | # Hash Tables 2 | 3 | Project done during **Full Stack Software Engineering** at **ALX**. It aims to learn how to implement Hash Tables with **C language**. 4 | 5 | ## Technologies 6 | * Scripts written in Bash 7 | * C files are compiled using `gcc` 8 | * Tested on Ubuntu 20.04 LTS 9 | 10 | ## Files 11 | All of the following files are scripts and programs written in C: 12 | 13 | | Filename | Description | 14 | | -------- | ----------- | 15 | | `0-hash_table_create.c` | Function that creates a hash table | 16 | | `1-djb2.c` | Function that executes the djb2 algorithm | 17 | | `2-key_index.c` | Function that returns the index of a key | 18 | | `3-hash_table.c` | Function that adds an element to the hash table | 19 | | `4-hash_table_get.c` | Function that retrieves a value associated with a key | 20 | | `5-hash_table_print.c` | Function that prints a hash table | 21 | | `6-hash_table_delete.c` | Function that deletes a hash table | 22 | | `100-sorted_hash_table.c` | C file with some functions that implements a hash table with a sorted linked list | 23 | -------------------------------------------------------------------------------- /0x1C-makefiles/0-Makefile: -------------------------------------------------------------------------------- 1 | all: main.c school.c 2 | gcc main.c school.c -o school 3 | -------------------------------------------------------------------------------- /0x1C-makefiles/1-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | 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-island_perimeter.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | """Defines an island perimeter measuring function.""" 3 | 4 | 5 | def island_perimeter(grid): 6 | """Return the perimiter of an island. 7 | The grid represents water by 0 and land by 1. 8 | Args: 9 | grid (list): A list of list of integers representing an island. 10 | Returns: 11 | The perimeter of the island defined in grid. 12 | """ 13 | width = len(grid[0]) 14 | height = len(grid) 15 | edges = 0 16 | size = 0 17 | 18 | for i in range(height): 19 | for j in range(width): 20 | if grid[i][j] == 1: 21 | size += 1 22 | if (j > 0 and grid[i][j - 1] == 1): 23 | edges += 1 24 | if (i > 0 and grid[i - 1][j] == 1): 25 | edges += 1 26 | return size * 4 - edges * 2 27 | -------------------------------------------------------------------------------- /0x1C-makefiles/README.md: -------------------------------------------------------------------------------- 1 | # Makefiles 2 | 3 | Project done during **Full Stack Software Engineering** at **ALX**. It aims to learn what are makefiles, when, why and how to use them (with variables, explicit and implicit rules). 4 | 5 | ## Technologies 6 | * Scripts written in Bash 7 | * C files are compiled using `gcc 9.3.0` 8 | * Tested on Ubuntu 20.04 LTS 9 | * Version of `make`: GNU Make 4.2.1 10 | 11 | ## Files 12 | 13 | | Filename | Description | 14 | | -------- | ----------- | 15 | | `0-Makefile` | Builds an executable with `all` rule | 16 | | `1-Makefile` | Builds an executable specifying a compiler and source files (`CC` and `SRC` variables) | 17 | | `2-Makefile` | Builds an executable using `CC`, `SRC`, `OBJ` and `NAME` variables | 18 | | `3-Makefile` | Builds an executable using `clean`, `oclean`, `fclean` and `re` rules, and `CC`, `SRC`, `OBJ`, `NAME` and `RM` variables | 19 | | `4-Makefile` | Builds an executable using `clean`, `oclean`, `fclean` and `re` rules, and `CC`, `SRC`, `OBJ`, `NAME`, `RM` and `CFLAGS` variables | 20 | | `5-island_perimeter.py` | Function that returns the perimeter of an island described in `grid` list | 21 | | `100-Makefile` | Complete Makefile with all of the variables and rules previously mentioned | 22 | -------------------------------------------------------------------------------- /0x1C-makefiles/m.h: -------------------------------------------------------------------------------- 1 | #ifndef __M_H__ 2 | #define __M_H__ 3 | 4 | #include 5 | #include 6 | 7 | void print_school(void); 8 | 9 | #endif 10 | -------------------------------------------------------------------------------- /0x1C-makefiles/main.c: -------------------------------------------------------------------------------- 1 | #include "m.h" 2 | 3 | /** 4 | * main - Entry point 5 | * 6 | * Return: Always 7 | */ 8 | int main(void) 9 | { 10 | print_school(); 11 | return (EXIT_SUCCESS); 12 | } 13 | /* Holberton */ 14 | -------------------------------------------------------------------------------- /0x1C-makefiles/school.c: -------------------------------------------------------------------------------- 1 | #include "m.h" 2 | 3 | /** 4 | * print_holberton - print the logo of Holberton 5 | * 6 | * Return: Nothing 7 | * 8 | * Learn more: https://www.holbertonschool.com 9 | */ 10 | void print_school(void) 11 | { 12 | printf("j#0000000000000000000000000000000000000\n"); 13 | printf("j#000000000000000000@Q**g00000000000000\n"); 14 | printf("j#0000000000000000*]++]4000000000000000\n"); 15 | printf("j#000000000000000k]++]++*N#000000000000\n"); 16 | printf("j#0000000000000*C+++]++]++]J*0000000000\n"); 17 | printf("j#00000000000@+]++qwwwp=]++++]*00000000\n"); 18 | printf("j#0000000000*+++]q#0000k+]+]++]4#000000\n"); 19 | printf("j#00000000*C+]+]w#0000*]+++]+]++0000000\n"); 20 | printf("j#0000we+]wW000***C++]++]+]++++40000000\n"); 21 | printf("j#000000000*C+]+]]+]++]++]++]+q#0000000\n"); 22 | printf("j#0000000*]+]+++++++]++]+++]+++J0000000\n"); 23 | printf("j#000000C++]=]+]+]+]++]++]+]+]+]=000000\n"); 24 | printf("j#00000k+]++]+++]+]++qwW0000000AgW00000\n"); 25 | printf("j#00000k++]++]+]+++qW#00000000000000000\n"); 26 | printf("j#00000A]++]++]++]++J**0000000000000000\n"); 27 | printf("j#000000e]++]+++]++]++]J000000000000000\n"); 28 | printf("j#0000000A]++]+]++]++]++000000000000000\n"); 29 | printf("j#000000000w]++]+]++]+qW#00000000000000\n"); 30 | printf("j#00000000000w]++++]*0##000000000000000\n"); 31 | printf("j#0000000000000Ag]+]++*0000000000000000\n"); 32 | printf("j#00000000000000000we]+]Q00000000000000\n"); 33 | printf("j#0000000000000@@+wgdA]+J00000000000000\n"); 34 | printf("j#0000000000000k?qwgdC=]4#0000000000000\n"); 35 | printf("j#00000000000000w]+]++qw#00000000000000\n"); 36 | printf("\"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"); 37 | } 38 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/0-linear.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * linear_search - searches for a value in an array of 5 | * integers using the Linear search algorithm 6 | * 7 | * @array: input array 8 | * @size: size of the array 9 | * @value: value to search in 10 | * Return: Always EXIT_SUCCESS 11 | */ 12 | int linear_search(int *array, size_t size, int value) 13 | { 14 | int i; 15 | 16 | if (array == NULL) 17 | return (-1); 18 | 19 | for (i = 0; i < (int)size; i++) 20 | { 21 | printf("Value checked array[%u] = [%d]\n", i, array[i]); 22 | if (value == array[i]) 23 | return (i); 24 | } 25 | return (-1); 26 | } 27 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/1-binary.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * recursive_search - searches for a value in an array of 5 | * integers using the Binary search algorithm 6 | * 7 | * 8 | * @array: input array 9 | * @size: size of the array 10 | * @value: value to search in 11 | * Return: index of the number 12 | */ 13 | int recursive_search(int *array, size_t size, int value) 14 | { 15 | size_t half = size / 2; 16 | size_t i; 17 | 18 | if (array == NULL || size == 0) 19 | return (-1); 20 | 21 | printf("Searching in array"); 22 | 23 | for (i = 0; i < size; i++) 24 | printf("%s %d", (i == 0) ? ":" : ",", array[i]); 25 | 26 | printf("\n"); 27 | 28 | if (half && size % 2 == 0) 29 | half--; 30 | 31 | if (value == array[half]) 32 | return ((int)half); 33 | 34 | if (value < array[half]) 35 | return (recursive_search(array, half, value)); 36 | 37 | half++; 38 | 39 | return (recursive_search(array + half, size - half, value) + half); 40 | } 41 | 42 | /** 43 | * binary_search - calls to binary_search to return 44 | * the index of the number 45 | * 46 | * @array: input array 47 | * @size: size of the array 48 | * @value: value to search in 49 | * Return: index of the number 50 | */ 51 | int binary_search(int *array, size_t size, int value) 52 | { 53 | int index; 54 | 55 | index = recursive_search(array, size, value); 56 | 57 | if (index >= 0 && array[index] != value) 58 | return (-1); 59 | 60 | return (index); 61 | } 62 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/100-jump.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | #include 3 | 4 | /** 5 | * jump_search - searches for a value in an array of 6 | * integers using the Jump search algorithm 7 | * 8 | * @array: input array 9 | * @size: size of the array 10 | * @value: value to search in 11 | * Return: index of the number 12 | */ 13 | int jump_search(int *array, size_t size, int value) 14 | { 15 | int index, m, k, prev; 16 | 17 | if (array == NULL || size == 0) 18 | return (-1); 19 | 20 | m = (int)sqrt((double)size); 21 | k = 0; 22 | prev = index = 0; 23 | 24 | do { 25 | printf("Value checked array[%d] = [%d]\n", index, array[index]); 26 | 27 | if (array[index] == value) 28 | return (index); 29 | k++; 30 | prev = index; 31 | index = k * m; 32 | } while (index < (int)size && array[index] < value); 33 | 34 | printf("Value found between indexes [%d] and [%d]\n", prev, index); 35 | 36 | for (; prev <= index && prev < (int)size; prev++) 37 | { 38 | printf("Value checked array[%d] = [%d]\n", prev, array[prev]); 39 | if (array[prev] == value) 40 | return (prev); 41 | } 42 | 43 | return (-1); 44 | } 45 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/101-O: -------------------------------------------------------------------------------- 1 | O(sqrt(n)) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/102-interpolation.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * interpolation_search - searches for a value in an array of 5 | * integers using the Interpolation search algorithm 6 | * 7 | * @array: input array 8 | * @size: size of the array 9 | * @value: value to search in 10 | * Return: index of the number 11 | */ 12 | int interpolation_search(int *array, size_t size, int value) 13 | { 14 | size_t pos, low, high; 15 | double f; 16 | 17 | if (array == NULL) 18 | return (-1); 19 | 20 | low = 0; 21 | high = size - 1; 22 | 23 | while (size) 24 | { 25 | f = (double)(high - low) / (array[high] - array[low]) * (value - array[low]); 26 | pos = (size_t)(low + f); 27 | printf("Value checked array[%d]", (int)pos); 28 | 29 | if (pos >= size) 30 | { 31 | printf(" is out of range\n"); 32 | break; 33 | } 34 | else 35 | { 36 | printf(" = [%d]\n", array[pos]); 37 | } 38 | 39 | if (array[pos] == value) 40 | return ((int)pos); 41 | 42 | if (array[pos] < value) 43 | low = pos + 1; 44 | else 45 | high = pos - 1; 46 | 47 | if (low == high) 48 | break; 49 | } 50 | 51 | return (-1); 52 | } 53 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/103-exponential.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * _binary_search - Searches for a value in a sorted array 5 | * of integers using binary search. 6 | * @array: A pointer to the first element of the array to search. 7 | * @left: The starting index of the [sub]array to search. 8 | * @right: The ending index of the [sub]array to search. 9 | * @value: The value to search for. 10 | * 11 | * Return: If the value is not present or the array is NULL, -1. 12 | * Otherwise, the index where the value is located. 13 | * 14 | * Description: Prints the [sub]array being searched after each change. 15 | */ 16 | int _binary_search(int *array, size_t left, size_t right, int value) 17 | { 18 | size_t i; 19 | 20 | if (array == NULL) 21 | return (-1); 22 | 23 | while (right >= left) 24 | { 25 | printf("Searching in array: "); 26 | for (i = left; i < right; i++) 27 | printf("%d, ", array[i]); 28 | printf("%d\n", array[i]); 29 | 30 | i = left + (right - left) / 2; 31 | if (array[i] == value) 32 | return (i); 33 | if (array[i] > value) 34 | right = i - 1; 35 | else 36 | left = i + 1; 37 | } 38 | 39 | return (-1); 40 | } 41 | 42 | /** 43 | * exponential_search - Searches for a value in a sorted array 44 | * of integers using exponential search. 45 | * @array: A pointer to the first element of the array to search. 46 | * @size: The number of elements in the array. 47 | * @value: The value to search for. 48 | * 49 | * Return: If the value is not present or the array is NULL, -1. 50 | * Otherwise, the index where the value is located. 51 | * 52 | * Description: Prints a value every time it is compared in the array. 53 | */ 54 | int exponential_search(int *array, size_t size, int value) 55 | { 56 | size_t i = 0, right; 57 | 58 | if (array == NULL) 59 | return (-1); 60 | 61 | if (array[0] != value) 62 | { 63 | for (i = 1; i < size && array[i] <= value; i = i * 2) 64 | printf("Value checked array[%ld] = [%d]\n", i, array[i]); 65 | } 66 | 67 | right = i < size ? i : size - 1; 68 | printf("Value found between indexes [%ld] and [%ld]\n", i / 2, right); 69 | return (_binary_search(array, i / 2, right, value)); 70 | } 71 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/104-advanced_binary.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * rec_search - searches for a value in an array of 5 | * integers using the Binary search algorithm 6 | * 7 | * 8 | * @array: input array 9 | * @size: size of the array 10 | * @value: value to search in 11 | * Return: index of the number 12 | */ 13 | int rec_search(int *array, size_t size, int value) 14 | { 15 | size_t half = size / 2; 16 | size_t i; 17 | 18 | if (array == NULL || size == 0) 19 | return (-1); 20 | 21 | printf("Searching in array"); 22 | 23 | for (i = 0; i < size; i++) 24 | printf("%s %d", (i == 0) ? ":" : ",", array[i]); 25 | 26 | printf("\n"); 27 | 28 | if (half && size % 2 == 0) 29 | half--; 30 | 31 | if (value == array[half]) 32 | { 33 | if (half > 0) 34 | return (rec_search(array, half + 1, value)); 35 | return ((int)half); 36 | } 37 | 38 | if (value < array[half]) 39 | return (rec_search(array, half + 1, value)); 40 | 41 | half++; 42 | return (rec_search(array + half, size - half, value) + half); 43 | } 44 | 45 | /** 46 | * advanced_binary - calls to rec_search to return 47 | * the index of the number 48 | * 49 | * @array: input array 50 | * @size: size of the array 51 | * @value: value to search in 52 | * Return: index of the number 53 | */ 54 | int advanced_binary(int *array, size_t size, int value) 55 | { 56 | int index; 57 | 58 | index = rec_search(array, size, value); 59 | 60 | if (index >= 0 && array[index] != value) 61 | return (-1); 62 | 63 | return (index); 64 | } 65 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/105-jump_list.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | #include 3 | 4 | /** 5 | * jump_list - searches for a value in an array of 6 | * integers using the Jump search algorithm 7 | * 8 | * @list: input list 9 | * @size: size of the array 10 | * @value: value to search in 11 | * Return: index of the number 12 | */ 13 | listint_t *jump_list(listint_t *list, size_t size, int value) 14 | { 15 | size_t index, k, m; 16 | listint_t *prev; 17 | 18 | if (list == NULL || size == 0) 19 | return (NULL); 20 | 21 | m = (size_t)sqrt((double)size); 22 | index = 0; 23 | k = 0; 24 | 25 | do { 26 | prev = list; 27 | k++; 28 | index = k * m; 29 | 30 | while (list->next && list->index < index) 31 | list = list->next; 32 | 33 | if (list->next == NULL && index != list->index) 34 | index = list->index; 35 | 36 | printf("Value checked at index [%d] = [%d]\n", (int)index, list->n); 37 | 38 | } while (index < size && list->next && list->n < value); 39 | 40 | printf("Value found between indexes "); 41 | printf("[%d] and [%d]\n", (int)prev->index, (int)list->index); 42 | 43 | for (; prev && prev->index <= list->index; prev = prev->next) 44 | { 45 | printf("Value checked at index [%d] = [%d]\n", (int)prev->index, prev->n); 46 | if (prev->n == value) 47 | return (prev); 48 | } 49 | 50 | return (NULL); 51 | } 52 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/106-linear_skip.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * linear_skip - searches for a value in a skip list 5 | * 6 | * @list: input list 7 | * @value: value to search in 8 | * Return: index of the number 9 | */ 10 | skiplist_t *linear_skip(skiplist_t *list, int value) 11 | { 12 | skiplist_t *go; 13 | 14 | if (list == NULL) 15 | return (NULL); 16 | 17 | go = list; 18 | 19 | do { 20 | list = go; 21 | go = go->express; 22 | printf("Value checked at index "); 23 | printf("[%d] = [%d]\n", (int)go->index, go->n); 24 | } while (go->express && go->n < value); 25 | 26 | if (go->express == NULL) 27 | { 28 | list = go; 29 | while (go->next) 30 | go = go->next; 31 | } 32 | 33 | printf("Value found between indexes "); 34 | printf("[%d] and [%d]\n", (int)list->index, (int)go->index); 35 | 36 | while (list != go->next) 37 | { 38 | printf("Value checked at index [%d] = [%d]\n", (int)list->index, list->n); 39 | if (list->n == value) 40 | return (list); 41 | list = list->next; 42 | } 43 | 44 | return (NULL); 45 | } 46 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/107-O: -------------------------------------------------------------------------------- 1 | O(n) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/108-O: -------------------------------------------------------------------------------- 1 | O(sqrt(n)) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/2-O: -------------------------------------------------------------------------------- 1 | O(n) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/3-O: -------------------------------------------------------------------------------- 1 | O(1) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/4-O: -------------------------------------------------------------------------------- 1 | O(log(n)) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/5-O: -------------------------------------------------------------------------------- 1 | O(1) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/6-O: -------------------------------------------------------------------------------- 1 | O(nm) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/search_algos.h: -------------------------------------------------------------------------------- 1 | #ifndef _SORTING_ALGOS_H_ 2 | #define _SORTING_ALGOS_H_ 3 | 4 | #include 5 | #include 6 | 7 | /** 8 | * struct listint_s - singly linked list 9 | * 10 | * @n: Integer 11 | * @index: Index of the node in the list 12 | * @next: Pointer to the next node 13 | * 14 | * Description: singly linked list node structure 15 | * for Alx project 16 | */ 17 | typedef struct listint_s 18 | { 19 | int n; 20 | size_t index; 21 | struct listint_s *next; 22 | } listint_t; 23 | 24 | /** 25 | * struct skiplist_s - Singly linked list with an express lane 26 | * 27 | * @n: Integer 28 | * @index: Index of the node in the list 29 | * @next: Pointer to the next node 30 | * @express: Pointer to the next node in the express lane 31 | * 32 | * Description: singly linked list node structure with an express lane 33 | * for Alx project 34 | */ 35 | typedef struct skiplist_s 36 | { 37 | int n; 38 | size_t index; 39 | struct skiplist_s *next; 40 | struct skiplist_s *express; 41 | } skiplist_t; 42 | 43 | int linear_search(int *array, size_t size, int value); 44 | int binary_search(int *array, size_t size, int value); 45 | int jump_search(int *array, size_t size, int value); 46 | int interpolation_search(int *array, size_t size, int value); 47 | int exponential_search(int *array, size_t size, int value); 48 | int advanced_binary(int *array, size_t size, int value); 49 | listint_t *jump_list(listint_t *list, size_t size, int value); 50 | skiplist_t *linear_skip(skiplist_t *list, int value); 51 | 52 | #endif 53 | --------------------------------------------------------------------------------