├── alx └── README.md ├── 0x1E-search_algorithms ├── 107-O ├── 2-O ├── 3-O ├── 5-O ├── 6-O ├── 1-binary_recursive.c ├── 101-O ├── 108-O ├── 4-O ├── 0-linear.c ├── 0-main.c ├── 1-binary.c ├── 100-jump.c ├── 102-interpolation.c ├── 106-linear_skip.c ├── search_algos.h ├── 105-jump_list.c └── 104-advanced_binary.c ├── 0x14-bit_manipulation ├── 101-password ├── crackme3 ├── .1-print_binary.c.swp ├── 100-get_endianness.c ├── 1-print_binary.c ├── main.h ├── 3-set_bit.c ├── 4-clear_bit.c ├── 5-flip_bits.c ├── 2-get_bit.c ├── README.md └── 0-binary_to_uint.c ├── 0x15-file_io ├── README.md ├── 0-main.c ├── 1-main.c ├── main.h ├── 2-largest_number.c ├── 0-read_textfile.c ├── 1-create_file.c ├── 2-append_text_to_file.c ├── 3-print_remaining_days.c └── 3-cp.c ├── 0x17-doubly_linked_lists ├── 102-result ├── 100-password ├── 1-dlistint_len.c ├── 0-print_dlistint.c ├── 6-sum_dlistint.c ├── 4-free_dlistint.c ├── 5-get_dnodeint.c ├── 2-add_dnodeint.c ├── 3-add_dnodeint_end.c ├── lists.h ├── 8-delete_dnodeint.c └── 7-insert_dnodeint.c ├── 0x18-dynamic_libraries ├── README.md ├── 1-create_dynamic_lib.sh ├── main.o ├── 3-puts.o ├── 6-abs.o ├── liball.so ├── libmy.a ├── 0-isupper.o ├── 0-memset.o ├── 0-strcat.o ├── 1-isdigit.o ├── 1-memcpy.o ├── 1-strncat.o ├── 100-atoi.o ├── 2-strchr.o ├── 2-strlen.o ├── 2-strncpy.o ├── 3-islower.o ├── 3-strcmp.o ├── 3-strspn.o ├── 4-isalpha.o ├── 4-strpbrk.o ├── 5-strstr.o ├── 9-strcpy.o ├── _putchar.o ├── libdynamic.so ├── operations.o ├── 100-operations.so ├── 101-make_me_win.sh ├── 1-isdigit.c ├── 3-islower.c ├── 0-isupper.c ├── 6-abs.c ├── 3-puts.c ├── 4-isalpha.c ├── _putchar.c ├── 2-strlen.c ├── 0-memset.c ├── 1-memcpy.c ├── 3-strcmp.c ├── 0-strcat.c ├── 2-strncpy.c ├── 2-strchr.c ├── 1-strncat.c ├── 9-strcpy.c ├── 5-strstr.c ├── 4-strpbrk.c ├── 3-strspn.c ├── 100-atoi.c ├── main.h └── operations.c ├── 0x0B-malloc_free ├── README.md ├── main.h ├── 4-free_grid.c ├── 0-create_array.c ├── 1-strdup.c ├── 2-str_concat.c ├── 3-alloc_grid.c ├── 100-argstostr.c └── 101-strtow.c ├── 0x07-pointers_arrays_strings ├── 101-crackme_password ├── README.md ├── 100-set_string.c ├── 5-main.c ├── 0-memset.c ├── 1-memcpy.c ├── 7-print_chessboard.c ├── 2-strchr.c ├── main.h ├── 4-strpbrk.c ├── 3-strspn.c ├── 8-print_diagsums.c └── 5-strstr.c ├── 0x0C-more_malloc_free ├── README.md ├── _putchar.c ├── 0-malloc_checked.c ├── main.h ├── 3-array_range.c ├── 2-calloc.c ├── 100-realloc.c └── 1-string_nconcat.c ├── 0x00-hello_world ├── 1-compiler ├── 2-assembler ├── 3-name ├── 0-preprocessor ├── 100-intel ├── 4-puts.c ├── 5-printf.c ├── 101-quote.c └── 6-size.c ├── 0x0A-argc_argv ├── README.md ├── main.h ├── 0-whatsmyname.c ├── 1-args.c ├── 2-args.c ├── 3-mul.c ├── 4-add.c └── 100-change.c ├── 0x0D-preprocessor ├── README.md ├── 1-pi.h ├── 4-sum.h ├── 0-object_like_macro.h ├── 3-function_like_macro.h └── 2-main.c ├── 0x0E-structures_typedef ├── README.md ├── 5-free_dog.c ├── 1-init_dog.c ├── 2-print_dog.c ├── dog.h └── 4-new_dog.c ├── 0x0F-function_pointers ├── README.md ├── 0-print_name.c ├── function_pointers.h ├── 1-array_iterator.c ├── 3-calc.h ├── 3-get_op_func.c ├── 2-int_index.c ├── 3-main.c ├── 100-main_opcodes.c └── 3-op_functions.c ├── 0x06-pointers_arrays_strings ├── README.md ├── git-com ├── 2-strncpy.c ├── 5-string_toupper.c ├── 3-strcmp.c ├── 4-rev_array.c ├── 0-strcat.c ├── 102-magic.c ├── 101-print_number.c ├── main.h ├── 104-main.c ├── 1-strncat.c ├── 100-rot13.c ├── 7-leet.c ├── 6-cap_string.c ├── 104-print_buffer.c └── 103-infinite_add.c ├── 0x1C-makefiles ├── 0-Makefile ├── 1-Makefile ├── 2-Makefile ├── 3-Makefile ├── 4-Makefile ├── 100-Makefile └── 5-island_perimeter.py ├── 0x04-more_functions_nested_loops ├── README.md ├── a.out ├── git-command ├── 2-mul.c ├── 6-print_line.c ├── 1-isdigit.c ├── 0-isupper.c ├── 3-print_numbers.c ├── 4-print_most_numbers.c ├── main.h ├── 5-more_numbers.c ├── 8-print_square.c ├── 100-prime_factor.c ├── 101-print_number.c ├── 7-print_diagonal.c ├── 9-fizz_buzz.c └── 10-print_triangle.c ├── git_com ├── 0x08-recursion ├── git-c ├── README.md ├── 1-print_rev_recursion.c ├── 2-strlen_recursion.c ├── 0-puts_recursion.c ├── 3-factorial.c ├── 4-pow_recursion.c ├── main.h ├── 101-wildcmp.c ├── 5-sqrt_recursion.c ├── 6-is_prime_number.c └── 100-is_palindrome.c ├── 0x09-static_libraries ├── 6-abs.o ├── libmy.a ├── 0-memset.o ├── 0-strcat.o ├── 1-memcpy.o ├── 100-atoi.o ├── 2-strchr.o ├── 2-strlen.o ├── 3-puts.o ├── 3-strcmp.o ├── 3-strspn.o ├── 5-strstr.o ├── 9-strcpy.o ├── _putchar.o ├── liball.a ├── 0-isupper.o ├── 1-isdigit.o ├── 1-strncat.o ├── 2-strncpy.o ├── 3-islower.o ├── 4-isalpha.o ├── 4-strpbrk.o ├── create_static_lib.sh ├── git-com ├── README.md ├── 2-strlen.c ├── _putchar.c ├── 6-abs.c ├── 1-isdigit.c ├── 0-isupper.c ├── 0-memset.c ├── 3-puts.c ├── 1-memcpy.c ├── 4-isalpha.c ├── 2-strncpy.c ├── 2-strchr.c ├── 3-islower.c ├── 3-strcmp.c ├── 0-strcat.c ├── 9-strcpy.c ├── 1-strncat.c ├── 4-strpbrk.c ├── 3-strspn.c ├── 100-atoi.c ├── 5-strstr.c └── main.h ├── 0x03-debugging ├── git-command ├── README.md ├── 0-main.c ├── main.h ├── 1-main.c ├── 2-largest_number.c ├── 3-main_b.c └── 3-print_remaining_days.c ├── 0x02-functions_nested_loops ├── git-command ├── 0x04-more_functions_nested_loops ├── 10-add.c ├── 1-alphabet.c ├── 6-abs.c ├── 0-putchar.c ├── main.h ├── 101-natural.c ├── 2-print_alphabet_x10.c ├── README.md ├── 4-isalpha.c ├── 3-islower.c ├── 7-print_last_digit.c ├── 102-fibonacci.c ├── 11-print_to_98.c ├── 103-fibonacci.c ├── 5-sign.c ├── 9-times_table.c ├── 8-24_hours.c ├── 100-times_table.c └── 104-fibonacci.c ├── 0x05-pointers_arrays_strings ├── git-com ├── 0-reset_to_98.c ├── 1-swap.c ├── 2-strlen.c ├── 3-puts.c ├── 6-puts2.c ├── 8-print_array.c ├── README.md ├── main.h ├── 4-print_rev.c ├── 101-keygen.c ├── 7-puts_half.c ├── 9-strcpy.c ├── 5-rev_string.c ├── 100-atoi.c └── main.c ├── 0x12-singly_linked_lists ├── README.md ├── 100-first.c ├── 4-free_list.c ├── 1-list_len.c ├── 101-hello_holberton.asm ├── 0-print_list.c ├── 2-add_node.c ├── lists.h └── 3-add_node_end.c ├── 0x01-variables_if_else_while ├── README.md ├── 2-print_alphabet.c ├── 5-print_numbers.c ├── 7-print_tebahpla.c ├── 6-print_numberz.c ├── 4-print_alphabt.c ├── 3-print_alphabets.c ├── 8-print_base16.c ├── 9-print_comb.c ├── 0-positive_or_negative.c ├── 100-print_comb3.c ├── 102-print_comb5.c ├── 1-last_digit.c └── 101-print_comb4.c ├── 0x13-more_singly_linked_lists ├── 4-free_listint.c ├── 1-listint_len.c ├── 0-print_listint.c ├── 8-sum_listint.c ├── 5-free_listint2.c ├── 7-get_nodeint.c ├── 100-reverse_listint.c ├── 6-pop_listint.c ├── 2-add_nodeint.c ├── 3-add_nodeint_end.c ├── 103-find_loop.c ├── 10-delete_nodeint.c ├── 9-insert_nodeint.c ├── 102-free_listint_safe.c ├── 101-print_listint_safe.c └── lists.h ├── 0x1A-hash_tables ├── 2-key_index.c ├── 1-djb2.c ├── 0-hash_table_create.c ├── 6-hash_table_delete.c ├── 5-hash_table_print.c ├── 4-hash_table_get.c ├── 3-hash_table_set.c └── README.md └── 0x10-variadic_functions ├── variadic_functions.h ├── 0-sum_them_all.c ├── 1-print_numbers.c ├── 2-print_strings.c ├── 3-print_all.c └── README.md /alx/README.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/107-O: -------------------------------------------------------------------------------- 1 | O(n) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/2-O: -------------------------------------------------------------------------------- 1 | O(n) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/3-O: -------------------------------------------------------------------------------- 1 | O(1) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/5-O: -------------------------------------------------------------------------------- 1 | O(1) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/6-O: -------------------------------------------------------------------------------- 1 | O(nm) 2 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/101-password: -------------------------------------------------------------------------------- 1 | Hol 2 | -------------------------------------------------------------------------------- /0x15-file_io/README.md: -------------------------------------------------------------------------------- 1 | 0x03-debugging 2 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/102-result: -------------------------------------------------------------------------------- 1 | 906609 -------------------------------------------------------------------------------- /0x18-dynamic_libraries/README.md: -------------------------------------------------------------------------------- 1 | readme 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/1-binary_recursive.c: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/101-O: -------------------------------------------------------------------------------- 1 | O(sqrt(n)) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/108-O: -------------------------------------------------------------------------------- 1 | O(sqrt(n)) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/4-O: -------------------------------------------------------------------------------- 1 | O(log(n)) 2 | -------------------------------------------------------------------------------- /0x0B-malloc_free/README.md: -------------------------------------------------------------------------------- 1 | 0x0B-malloc_free 2 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/101-crackme_password: -------------------------------------------------------------------------------- 1 | abc123 -------------------------------------------------------------------------------- /0x0C-more_malloc_free/README.md: -------------------------------------------------------------------------------- 1 | 0x0C-more_malloc_free 2 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/100-password: -------------------------------------------------------------------------------- 1 | en C Pyfo neZ 2 | -------------------------------------------------------------------------------- /0x00-hello_world/1-compiler: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -c $CFILE 3 | -------------------------------------------------------------------------------- /0x00-hello_world/2-assembler: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -S $CFILE 3 | -------------------------------------------------------------------------------- /0x0A-argc_argv/README.md: -------------------------------------------------------------------------------- 1 | Readme 0x0A. C - argc, argv tasks 2 | -------------------------------------------------------------------------------- /0x0D-preprocessor/README.md: -------------------------------------------------------------------------------- 1 | 0x0D. C - Preprocessor tasks 2 | -------------------------------------------------------------------------------- /0x00-hello_world/3-name: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc $CFILE -o cisfun 3 | -------------------------------------------------------------------------------- /0x00-hello_world/0-preprocessor: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -E $CFILE -o c 3 | -------------------------------------------------------------------------------- /0x00-hello_world/100-intel: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -S -masm=intel $CFILE 3 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | 0x07-pointers_arrays_strings 2 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/README.md: -------------------------------------------------------------------------------- 1 | 0x0E. C - Structures, typedef tasks 2 | -------------------------------------------------------------------------------- /0x0F-function_pointers/README.md: -------------------------------------------------------------------------------- 1 | 0x0F. C - Function pointers_tasks 2 | -------------------------------------------------------------------------------- /0x0A-argc_argv/main.h: -------------------------------------------------------------------------------- 1 | #ifndef _MAIN_H_ 2 | #define _MAIN_H_ 3 | 4 | #endif 5 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | 0x06. C - More pointers, arrays and strings . 2 | -------------------------------------------------------------------------------- /0x1C-makefiles/0-Makefile: -------------------------------------------------------------------------------- 1 | all : main.c school.c 2 | gcc main.c school.c -o school 3 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/README.md: -------------------------------------------------------------------------------- 1 | 0x04. C - More functions, more nested loops 2 | -------------------------------------------------------------------------------- /0x0D-preprocessor/1-pi.h: -------------------------------------------------------------------------------- 1 | #ifndef _PI_ 2 | #define _PI_ 3 | 4 | #define PI 3.14159265359 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/1-create_dynamic_lib.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -fPIC -c *.c 3 | gcc -shared -o liball.so *.o 4 | -------------------------------------------------------------------------------- /0x1C-makefiles/1-Makefile: -------------------------------------------------------------------------------- 1 | 2 | CC = gcc 3 | SRC = main.c school.c 4 | all : $(SRC) 5 | $(CC) $(SRC) -o school 6 | -------------------------------------------------------------------------------- /git_com: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | git add . 3 | echo "enter your commit message" 4 | msg 5 | git commit -m "$msg" 6 | git push 7 | -------------------------------------------------------------------------------- /0x08-recursion/git-c: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | git add . 3 | echo "my commit message" 4 | read msg 5 | git commit -m "$msg" 6 | git push 7 | -------------------------------------------------------------------------------- /0x09-static_libraries/6-abs.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Solomonkassa/alx-low_level_programming/HEAD/0x09-static_libraries/6-abs.o -------------------------------------------------------------------------------- /0x09-static_libraries/libmy.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Solomonkassa/alx-low_level_programming/HEAD/0x09-static_libraries/libmy.a -------------------------------------------------------------------------------- /0x18-dynamic_libraries/main.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Solomonkassa/alx-low_level_programming/HEAD/0x18-dynamic_libraries/main.o -------------------------------------------------------------------------------- /0x09-static_libraries/0-memset.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Solomonkassa/alx-low_level_programming/HEAD/0x09-static_libraries/0-memset.o -------------------------------------------------------------------------------- /0x09-static_libraries/0-strcat.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Solomonkassa/alx-low_level_programming/HEAD/0x09-static_libraries/0-strcat.o -------------------------------------------------------------------------------- /0x09-static_libraries/1-memcpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Solomonkassa/alx-low_level_programming/HEAD/0x09-static_libraries/1-memcpy.o -------------------------------------------------------------------------------- /0x09-static_libraries/100-atoi.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Solomonkassa/alx-low_level_programming/HEAD/0x09-static_libraries/100-atoi.o -------------------------------------------------------------------------------- /0x09-static_libraries/2-strchr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Solomonkassa/alx-low_level_programming/HEAD/0x09-static_libraries/2-strchr.o -------------------------------------------------------------------------------- /0x09-static_libraries/2-strlen.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Solomonkassa/alx-low_level_programming/HEAD/0x09-static_libraries/2-strlen.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-puts.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Solomonkassa/alx-low_level_programming/HEAD/0x09-static_libraries/3-puts.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-strcmp.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Solomonkassa/alx-low_level_programming/HEAD/0x09-static_libraries/3-strcmp.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-strspn.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Solomonkassa/alx-low_level_programming/HEAD/0x09-static_libraries/3-strspn.o -------------------------------------------------------------------------------- /0x09-static_libraries/5-strstr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Solomonkassa/alx-low_level_programming/HEAD/0x09-static_libraries/5-strstr.o -------------------------------------------------------------------------------- /0x09-static_libraries/9-strcpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Solomonkassa/alx-low_level_programming/HEAD/0x09-static_libraries/9-strcpy.o -------------------------------------------------------------------------------- /0x09-static_libraries/_putchar.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Solomonkassa/alx-low_level_programming/HEAD/0x09-static_libraries/_putchar.o -------------------------------------------------------------------------------- /0x09-static_libraries/liball.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Solomonkassa/alx-low_level_programming/HEAD/0x09-static_libraries/liball.a -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/crackme3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Solomonkassa/alx-low_level_programming/HEAD/0x14-bit_manipulation/crackme3 -------------------------------------------------------------------------------- /0x18-dynamic_libraries/3-puts.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Solomonkassa/alx-low_level_programming/HEAD/0x18-dynamic_libraries/3-puts.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/6-abs.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Solomonkassa/alx-low_level_programming/HEAD/0x18-dynamic_libraries/6-abs.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/liball.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Solomonkassa/alx-low_level_programming/HEAD/0x18-dynamic_libraries/liball.so -------------------------------------------------------------------------------- /0x18-dynamic_libraries/libmy.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Solomonkassa/alx-low_level_programming/HEAD/0x18-dynamic_libraries/libmy.a -------------------------------------------------------------------------------- /0x09-static_libraries/0-isupper.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Solomonkassa/alx-low_level_programming/HEAD/0x09-static_libraries/0-isupper.o -------------------------------------------------------------------------------- /0x09-static_libraries/1-isdigit.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Solomonkassa/alx-low_level_programming/HEAD/0x09-static_libraries/1-isdigit.o -------------------------------------------------------------------------------- /0x09-static_libraries/1-strncat.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Solomonkassa/alx-low_level_programming/HEAD/0x09-static_libraries/1-strncat.o -------------------------------------------------------------------------------- /0x09-static_libraries/2-strncpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Solomonkassa/alx-low_level_programming/HEAD/0x09-static_libraries/2-strncpy.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-islower.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Solomonkassa/alx-low_level_programming/HEAD/0x09-static_libraries/3-islower.o -------------------------------------------------------------------------------- /0x09-static_libraries/4-isalpha.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Solomonkassa/alx-low_level_programming/HEAD/0x09-static_libraries/4-isalpha.o -------------------------------------------------------------------------------- /0x09-static_libraries/4-strpbrk.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Solomonkassa/alx-low_level_programming/HEAD/0x09-static_libraries/4-strpbrk.o -------------------------------------------------------------------------------- /0x09-static_libraries/create_static_lib.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -c -Wall -Werror -Wextra *.c 3 | ar -rc liball.a *.o 4 | ranlib liball.a 5 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/0-isupper.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Solomonkassa/alx-low_level_programming/HEAD/0x18-dynamic_libraries/0-isupper.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/0-memset.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Solomonkassa/alx-low_level_programming/HEAD/0x18-dynamic_libraries/0-memset.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/0-strcat.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Solomonkassa/alx-low_level_programming/HEAD/0x18-dynamic_libraries/0-strcat.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/1-isdigit.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Solomonkassa/alx-low_level_programming/HEAD/0x18-dynamic_libraries/1-isdigit.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/1-memcpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Solomonkassa/alx-low_level_programming/HEAD/0x18-dynamic_libraries/1-memcpy.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/1-strncat.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Solomonkassa/alx-low_level_programming/HEAD/0x18-dynamic_libraries/1-strncat.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/100-atoi.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Solomonkassa/alx-low_level_programming/HEAD/0x18-dynamic_libraries/100-atoi.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/2-strchr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Solomonkassa/alx-low_level_programming/HEAD/0x18-dynamic_libraries/2-strchr.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/2-strlen.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Solomonkassa/alx-low_level_programming/HEAD/0x18-dynamic_libraries/2-strlen.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/2-strncpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Solomonkassa/alx-low_level_programming/HEAD/0x18-dynamic_libraries/2-strncpy.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/3-islower.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Solomonkassa/alx-low_level_programming/HEAD/0x18-dynamic_libraries/3-islower.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/3-strcmp.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Solomonkassa/alx-low_level_programming/HEAD/0x18-dynamic_libraries/3-strcmp.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/3-strspn.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Solomonkassa/alx-low_level_programming/HEAD/0x18-dynamic_libraries/3-strspn.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/4-isalpha.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Solomonkassa/alx-low_level_programming/HEAD/0x18-dynamic_libraries/4-isalpha.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/4-strpbrk.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Solomonkassa/alx-low_level_programming/HEAD/0x18-dynamic_libraries/4-strpbrk.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/5-strstr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Solomonkassa/alx-low_level_programming/HEAD/0x18-dynamic_libraries/5-strstr.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/9-strcpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Solomonkassa/alx-low_level_programming/HEAD/0x18-dynamic_libraries/9-strcpy.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/_putchar.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Solomonkassa/alx-low_level_programming/HEAD/0x18-dynamic_libraries/_putchar.o -------------------------------------------------------------------------------- /0x03-debugging/git-command: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | git add . 3 | echo "enter your commit messege :" 4 | read msg 5 | git commit -m $msg 6 | git push 7 | -------------------------------------------------------------------------------- /0x09-static_libraries/git-com: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | git add . 3 | echo "enter your commit message" 4 | read msg 5 | git commit -m "$msg" 6 | git push 7 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/libdynamic.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Solomonkassa/alx-low_level_programming/HEAD/0x18-dynamic_libraries/libdynamic.so -------------------------------------------------------------------------------- /0x18-dynamic_libraries/operations.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Solomonkassa/alx-low_level_programming/HEAD/0x18-dynamic_libraries/operations.o -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Solomonkassa/alx-low_level_programming/HEAD/0x04-more_functions_nested_loops/a.out -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/git-com: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | git add . 3 | echo "your commit message :" 4 | read msg 5 | git commit -m "$msg" 6 | git push 7 | -------------------------------------------------------------------------------- /0x1C-makefiles/2-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | OBJ = $(SRC:.c=.o) 4 | NAME = school 5 | all : $(OBJ) 6 | $(CC) $(OBJ) -o $(NAME) 7 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/git-command: -------------------------------------------------------------------------------- 1 | !/bin/bash 2 | git add . 3 | echo "enter your commit messege :" 4 | read msg 5 | git commit -m $msg 6 | git push 7 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/git-com: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | git add . 3 | echo "ENTER YOUR COMMIT MESSAGE" 4 | read msg 5 | git commit -m "$msg" 6 | git push 7 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/100-operations.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Solomonkassa/alx-low_level_programming/HEAD/0x18-dynamic_libraries/100-operations.so -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/git-command: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | git add . 3 | echo "enter your commit message" 4 | read msg 5 | git commit -m "$msg" 6 | git push 7 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/.1-print_binary.c.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Solomonkassa/alx-low_level_programming/HEAD/0x14-bit_manipulation/.1-print_binary.c.swp -------------------------------------------------------------------------------- /0x03-debugging/README.md: -------------------------------------------------------------------------------- 1 | 0x03. C - Debugging 2 | 3 | * What is debugging 4 | * What are some methods of debugging manually 5 | * How to read the error messages 6 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/0x04-more_functions_nested_loops: -------------------------------------------------------------------------------- 1 | !/bin/bash 2 | git add . 3 | echo "enter your commit messege :" 4 | read msg 5 | git commit -m $msg 6 | git push 7 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x09-static_libraries/README.md: -------------------------------------------------------------------------------- 1 | 0x09. C - Static libraries 2 | 3 | * What is a static library, how does it work, how to create one, and how to use it 4 | * Basic usage of ar, ranlib, nm 5 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/101-make_me_win.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | wget -P /tmp https://github.com/Tolulope05/alx-low_level_programming/raw/master/0x18-dynamic_libraries/nrandom.so 3 | export LD_PRELOAD=/tmp/nrandom.so 4 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x03-debugging/0-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - Debugging. 5 | * 6 | * Return: 0 7 | */ 8 | 9 | int main(void) 10 | { 11 | int i; 12 | 13 | i = 0; 14 | positive_or_negative(i); 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/README.md: -------------------------------------------------------------------------------- 1 | ##click the link 2 | ----------------------------------------------------------------------------------------- 3 | 0x12-singly_linked_lists 4 | 5 | -------------------------------------------------------------------------------- /0x08-recursion/README.md: -------------------------------------------------------------------------------- 1 | ##Recursion 2 | 3 | Recursion is a method where 4 | the solution to a problem 5 | depends on solutions to smaller 6 | instances of the same problem. 7 | 8 | A recursive function is a 9 | function that calls itself. 10 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/2-mul.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | *mul - multiplies two numbers 5 | *@a: first number 6 | *@b: second number 7 | *Return: returns result 8 | */ 9 | 10 | int mul(int a, int b) 11 | { 12 | return (a * b); 13 | } 14 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/1-isdigit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isdigit - checks for a digit (0 through 9) 5 | * @c: int to be checked 6 | * Return: 1 if c is a digit, 0 otherwise 7 | */ 8 | int _isdigit(int c) 9 | { 10 | return (c >= '0' && c <= '9'); 11 | } 12 | -------------------------------------------------------------------------------- /0x15-file_io/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 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/3-islower.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _islower - checks for lowercase character 5 | * @c: the character to check 6 | * Return: 1 if c is lowercase, 0 otherwise 7 | */ 8 | int _islower(int c) 9 | { 10 | return (c >= 'a' && c <= 'z'); 11 | } 12 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/0-isupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isupper - checks for uppercase character 5 | * @c: the character to be checked 6 | * Return: 1 if c is uppercase, 0 otherwise 7 | */ 8 | int _isupper(int c) 9 | { 10 | return (c >= 'A' && c <= 'Z'); 11 | } 12 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/100-set_string.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * set_string - sets the value of a pointer to a char 5 | * @s: source adress. 6 | * @to: target adress. 7 | * Return: no return. 8 | */ 9 | void set_string(char **s, char *to) 10 | { 11 | *s = to; 12 | } 13 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/6-abs.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _abs - computes the absolute value of an integer 5 | * @n: the int to check 6 | * Return: the absolute value of int 7 | */ 8 | int _abs(int n) 9 | { 10 | if (n >= 0) 11 | { 12 | return (n); 13 | } 14 | return (-n); 15 | } 16 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 : oclean all 19 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/3-puts.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _puts - prints a string to stdout 5 | * @str: pointer to the string to print 6 | */ 7 | void _puts(char *str) 8 | { 9 | int i; 10 | 11 | for (i = 0; str[i] != '\0'; i++) 12 | { 13 | _putchar(str[i]); 14 | } 15 | _putchar('\n'); 16 | } 17 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/4-isalpha.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isalpha - checks for alphabetic character 5 | * @c: the character to be checked 6 | * Return: 1 if c is a letter, 0 otherwise 7 | */ 8 | int _isalpha(int c) 9 | { 10 | return ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')); 11 | } 12 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/6-print_line.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | *print_line - prints a straight line 6 | *@n: parameter 7 | *Return:returns nothing 8 | */ 9 | 10 | void print_line(int n) 11 | { 12 | while (n-- > 0) 13 | { 14 | putchar('_'); 15 | } 16 | putchar('\n'); 17 | } 18 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _putchar - writes the character c to stdout 5 | * @c: The character to print 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | int _putchar(char c) 11 | { 12 | return (write(1, &c, 1)); 13 | } 14 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _putchar - writes the character c to stdout 5 | * @c: The character to print 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | int _putchar(char c) 11 | { 12 | return (write(1, &c, 1)); 13 | } 14 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/1-isdigit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | *_isdigit - checks whether a character is a digit or not 5 | *@c: tested character 6 | *Return: 1 if it is, 0 otherwise 7 | */ 8 | 9 | int _isdigit(int c) 10 | { 11 | if ((c >= 48) && (c <= 57)) 12 | { 13 | return (1); 14 | } 15 | 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/2-strlen.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strlen - returns the length of a string 5 | * @s: string to evaluate 6 | * 7 | * Return: the length of the string 8 | */ 9 | int _strlen(char *s) 10 | { 11 | int i; 12 | 13 | i = 0; 14 | 15 | while (s[i] != '\0') 16 | { 17 | i++; 18 | } 19 | 20 | return (i); 21 | } 22 | -------------------------------------------------------------------------------- /0x1C-makefiles/4-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | OBJ = $(SRC:.c=.o) 4 | NAME = school 5 | RM = rm -f 6 | CFLAGS = -Wall -Werror -Wextra -pedantic 7 | 8 | all : $(OBJ) 9 | $(CC) $(OBJ) -o $(NAME) 10 | 11 | clean : 12 | $(RM) *~ $(NAME) 13 | 14 | oclean : 15 | $(RM) $(OBJ) 16 | 17 | fclean : clean oclean 18 | 19 | re : oclean all 20 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/README.md: -------------------------------------------------------------------------------- 1 | # C - Variables, if, else, while 2 | 3 | In this project, I learned about using arithmetic, logical, relational, and boolean 4 | operators in C. Additionally, I practiced declaring and defining variables of type 5 | `char`, `int`, and `unsigned int`, using `if` and `if...else` statements, and 6 | implementing `while` loops. 7 | 8 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/0-isupper.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * _isupper - function that checks for uppercase character. 6 | * @c: input value to check 7 | * Return: int. 8 | */ 9 | 10 | int _isupper(int c) 11 | { 12 | if (c >= 'A' && c <= 'Z') 13 | { 14 | return (1); 15 | } 16 | else 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/3-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | *print_numbers - function that print all numbers from 6 | *0 to 9 7 | * 8 | *Return: returns nothing 9 | */ 10 | 11 | void print_numbers(void) 12 | { 13 | int n; 14 | 15 | for (n = 48; n < 58; n++) 16 | { 17 | putchar(n); 18 | } 19 | putchar(10); 20 | } 21 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/5-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char *s = "wworld of this"; 12 | char *f = "world"; 13 | char *t; 14 | 15 | t = _strstr(s, f); 16 | printf("%s\n", t); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | .PHONY : all clean oclean fclean re 7 | 8 | all : m.h $(OBJ) 9 | $(CC) $(OBJ) -o $(NAME) 10 | 11 | clean : 12 | $(RM) *~ $(NAME) 13 | 14 | oclean : 15 | $(RM) $(OBJ) 16 | 17 | fclean : clean oclean 18 | 19 | re : oclean all 20 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/0-putchar.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *main - Entry point 4 | * 5 | *Return: nothing, return void 6 | */ 7 | int main(void) 8 | { 9 | _putchar('_'); 10 | _putchar('p'); 11 | _putchar('u'); 12 | _putchar('t'); 13 | _putchar('c'); 14 | _putchar('h'); 15 | _putchar('a'); 16 | _putchar('r'); 17 | _putchar('\n'); 18 | 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x15-file_io/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 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/main.h: -------------------------------------------------------------------------------- 1 | void print_alphabet(void); 2 | int _putchar(char c); 3 | void print_alphabet_x10(void); 4 | int _islower(int c); 5 | int _isalpha(int c); 6 | int print_sign(int n); 7 | int _abs(int); 8 | int print_last_digit(int); 9 | void jack_bauer(void); 10 | void times_table(void); 11 | int add(int, int); 12 | void print_to_98(int n); 13 | void print_times_table(int n); 14 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/101-natural.c: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x1A-hash_tables/2-key_index.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * key_index - function that get the index where key/value is stored 5 | * @key: type pointer char of key index 6 | * @size: type pointer char the size of the array 7 | * Return: Index of the key 8 | */ 9 | unsigned long int key_index(const unsigned char *key, unsigned long int size) 10 | { 11 | return (hash_djb2(key) % size); 12 | } 13 | -------------------------------------------------------------------------------- /0x00-hello_world/4-puts.c: -------------------------------------------------------------------------------- 1 | /* 2 | * File: 4-puts.c 3 | * Auth: Solomon kassa 4 | */ 5 | 6 | #include 7 | 8 | /** 9 | * main - Prints "Programming is like building a multilingual 10 | * puzzle, followed by a new line. 11 | * 12 | * Return: Always 0. 13 | */ 14 | int main(void) 15 | { 16 | puts("\"Programming is like building a multilingual puzzle"); 17 | 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /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/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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/2-print_alphabet_x10.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * print_alphabet_x10 - Prints the alphabet 10 times. 6 | * 7 | * Return: Always 0 (Success) 8 | */ 9 | void print_alphabet_x10(void) 10 | { 11 | int a, b; 12 | 13 | for (a = 0; a <= 9; a++) 14 | { 15 | for (b = 97; b <= 122; b++) 16 | { 17 | _putchar(b); 18 | } 19 | _putchar('\n'); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/100-first.c: -------------------------------------------------------------------------------- 1 | /* 2 | * File: 100-first.c 3 | * Auth: Solomon Kassa 4 | */ 5 | 6 | #include 7 | 8 | void __attribute__((constructor)) hare(void); 9 | 10 | /** 11 | * hare - Prints a string before the 12 | * main function is executed. 13 | */ 14 | void hare(void) 15 | { 16 | printf("You're beat! and yet, you must allow,\n" 17 | "I bore my house upon my back!\n"); 18 | } 19 | -------------------------------------------------------------------------------- /0x00-hello_world/5-printf.c: -------------------------------------------------------------------------------- 1 | /* 2 | * File: 5-printf.c 3 | * Auth: Solomon kassa 4 | */ 5 | 6 | #include 7 | 8 | /** 9 | * main - Prints "with proper grammar, but the outcome is 10 | * a piece of art," followed by a new line. 11 | * 12 | * Return: Always 0. 13 | */ 14 | int main(void) 15 | { 16 | printf("with proper grammar, but the outcome is a piece of art,\n"); 17 | 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/2-print_alphabet.c: -------------------------------------------------------------------------------- 1 | /* 2 | * File: 2-print_alphabet.c 3 | * Auth: Solomon Kassa 4 | */ 5 | 6 | #include 7 | 8 | /** 9 | * main - Prints the alphabet in lowercase. 10 | * 11 | * Return: Always 0. 12 | */ 13 | int main(void) 14 | { 15 | char letter; 16 | 17 | for (letter = 'a'; letter <= 'z'; letter++) 18 | putchar(letter); 19 | 20 | putchar('\n'); 21 | 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/README.md: -------------------------------------------------------------------------------- 1 | 0x02. C - Functions, nested loops 2 | 3 | * What are nested loops and how to use them 4 | * What is a function and how do you use functions 5 | * What is the difference between a declaration and a definition of a function 6 | * What is a prototype 7 | * Scope of variables 8 | * What are the gcc flags -Wall -Werror -pedantic -Wextra 9 | * What are header files and how to to use them with #include 10 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/4-print_most_numbers.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | *print_most_numbers - prints numbers except 2 and 4 6 | * 7 | *Return: returns nothing 8 | */ 9 | 10 | void print_most_numbers(void) 11 | { 12 | int n; 13 | 14 | for (n = 48; n < 58; n++) 15 | { 16 | if ((n == 50) || (n == 52)) 17 | { 18 | continue; 19 | } 20 | putchar(n); 21 | } 22 | putchar(10); 23 | } 24 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/5-print_numbers.c: -------------------------------------------------------------------------------- 1 | /* 2 | * File: 5-print_numbers.c 3 | * Auth: Solomon Kassa 4 | */ 5 | 6 | #include 7 | 8 | /** 9 | * main - Prints all single digit numbers of base 10 starting from 0. 10 | * 11 | * Return: Always 0. 12 | */ 13 | int main(void) 14 | { 15 | int num; 16 | 17 | for (num = 0; num < 10; num++) 18 | printf("%d", num); 19 | 20 | printf("\n"); 21 | 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/7-print_tebahpla.c: -------------------------------------------------------------------------------- 1 | /* 2 | * File: 7-print_tebahpla.c 3 | * Auth: Solomon Kassa 4 | */ 5 | 6 | #include 7 | 8 | /** 9 | * main - Prints the lowercase alphabet in reverse. 10 | * 11 | * Return: Always 0. 12 | */ 13 | int main(void) 14 | { 15 | char letter; 16 | 17 | for (letter = 'z'; letter >= 'a'; letter--) 18 | putchar(letter); 19 | 20 | putchar('\n'); 21 | 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/6-print_numberz.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - program that prints all single digit numbers of base 10 starting from 5 | * You are not allowed to use any variable of type char 6 | * You can only use the putchar function 7 | * Return: 0 8 | */ 9 | 10 | int main(void) 11 | { 12 | int n = 48; 13 | while (n <= 57) 14 | { 15 | putchar(n); 16 | n += 1; 17 | } 18 | printf("\n"); 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/1-dlistint_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * dlistint_len - returns the number of elements in a linked dlistint_t list. 5 | * @h: pointer to the list. 6 | * Return: number of nodes. 7 | **/ 8 | size_t dlistint_len(const dlistint_t *h) 9 | { 10 | const dlistint_t *node = h; 11 | size_t cont = 0; 12 | 13 | while (node) 14 | { 15 | cont++; 16 | node = node->next; 17 | } 18 | 19 | return (cont); 20 | } 21 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/0-memset.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * *_memset - fills memory with a constant byte 5 | * @s: memory area to be filled 6 | * @b: char to copy 7 | * @n: number of times to copy b 8 | * 9 | * Return: pointer to the memory area s 10 | */ 11 | char *_memset(char *s, char b, unsigned int n) 12 | { 13 | unsigned int i; 14 | 15 | for (i = 0; i < n; i++) 16 | { 17 | s[i] = b; 18 | } 19 | 20 | return (s); 21 | } 22 | -------------------------------------------------------------------------------- /0x1A-hash_tables/1-djb2.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_djb2 - function of the algorithm djb2 5 | * @str: Type pointer char of string 6 | * Return: Hash 7 | */ 8 | 9 | unsigned long int hash_djb2(const unsigned char *str) 10 | { 11 | unsigned long int hash; 12 | int c; 13 | 14 | hash = 5381; 15 | while ((c = *str++)) 16 | { 17 | hash = ((hash << 5) + hash) + c; /* hash * 33 + c */ 18 | } 19 | return (hash); 20 | } 21 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/1-memcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * *_memcpy - copies memory area 5 | * @dest: destination memory area 6 | * @src: memory area to copy from 7 | * @n: number of bytes to copy 8 | * 9 | * Return: pointer to dest 10 | */ 11 | char *_memcpy(char *dest, char *src, unsigned int n) 12 | { 13 | unsigned int i; 14 | 15 | for (i = 0; i < n; i++) 16 | { 17 | dest[i] = src[i]; 18 | } 19 | 20 | return (dest); 21 | } 22 | -------------------------------------------------------------------------------- /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/README.md: -------------------------------------------------------------------------------- 1 | # Low-Level Programming 2 | - Data Structures, Algorithms 3 | - Linux/Unix System Programming 4 | ### This repository contains programs written for the low-level programming and algorithm track at ALX-Africa SE. 5 | - In these projects, which occurred throughout the duration of the Year 1 curriculum 6 | - I learned about data structures 7 | - Algorithms, and other low-level programming concepts while working in the C language. 8 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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/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 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/4-free_list.c: -------------------------------------------------------------------------------- 1 | /* 2 | * File: 4-free_list.c 3 | * Auth: Solomon Kassa 4 | */ 5 | 6 | #include "lists.h" 7 | #include 8 | 9 | /** 10 | * free_list - Frees a list_t list. 11 | * @head: A pointer to the list_t list. 12 | */ 13 | void free_list(list_t *head) 14 | { 15 | list_t *tmp; 16 | 17 | while (head) 18 | { 19 | tmp = head->next; 20 | free(head->str); 21 | free(head); 22 | head = tmp; 23 | } 24 | } 25 | 26 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/0-print_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * print_dlistint - prints all the elements of a dlistint_t list. 5 | * @h: pointer to the list. 6 | * Return: number of nodes. 7 | **/ 8 | size_t print_dlistint(const dlistint_t *h) 9 | { 10 | const dlistint_t *node = h; 11 | size_t cont = 0; 12 | 13 | while (node) 14 | { 15 | printf("%i\n", node->n); 16 | cont++; 17 | node = node->next; 18 | } 19 | 20 | return (cont); 21 | } 22 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/6-sum_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * sum_dlistint - returns the sum of all the data (n) of a dlistint_t list. 5 | * @head: pointer to the list. 6 | * Return: sum of all data in the list, 0 if the lost is empty. 7 | **/ 8 | int sum_dlistint(dlistint_t *head) 9 | { 10 | dlistint_t *node = head; 11 | int sum = 0; 12 | 13 | while (node) 14 | { 15 | sum += node->n; 16 | node = node->next; 17 | } 18 | 19 | return (sum); 20 | } 21 | -------------------------------------------------------------------------------- /0x00-hello_world/101-quote.c: -------------------------------------------------------------------------------- 1 | /* 2 | * File: 101-quote.c 3 | * Authr: Solomon kassa 4 | */ 5 | 6 | #include 7 | 8 | /** 9 | * main - Prints "and that piece of art is useful" - Dora Korpar, 2015-10-19", 10 | * followed by a new line, to standard error. 11 | * 12 | * Return: Always 1. 13 | */ 14 | int main(void) 15 | { 16 | write(2, 17 | "and that piece of art is useful\" - Dora Korpar, 2015-10-19\n", 18 | 59); 19 | 20 | return (1); 21 | } 22 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/5-more_numbers.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | *more_numbers - prints numbers to 14 6 | * 7 | *Return: returns nothing 8 | */ 9 | 10 | void more_numbers(void) 11 | { 12 | int n1, n2; 13 | 14 | for (n1 = 0; n1 < 10; n1++) 15 | { 16 | for (n2 = 0; n2 <= 14; n2++) 17 | { 18 | if (n2 > 9) 19 | { 20 | putchar((n2 / 10) + '0'); 21 | } 22 | putchar((n2 % 10) + '0'); 23 | } 24 | putchar(10); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/4-print_alphabt.c: -------------------------------------------------------------------------------- 1 | /* 2 | * File: 4-print_alphabt.c 3 | * Auth: Solomon kassa 4 | */ 5 | 6 | #include 7 | 8 | /** 9 | * main - Prints the alphabet in lowercase, except for q and e. 10 | * 11 | * Return: Always 0. 12 | */ 13 | int main(void) 14 | { 15 | char letter; 16 | 17 | for (letter = 'a'; letter <= 'z'; letter++) 18 | { 19 | if (letter != 'e' && letter != 'q') 20 | putchar(letter); 21 | } 22 | 23 | putchar('\n'); 24 | 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/1-list_len.c: -------------------------------------------------------------------------------- 1 | /* 2 | * File: 1-list_len.c 3 | * Author : Solomon Kassa 4 | */ 5 | 6 | #include "lists.h" 7 | #include 8 | 9 | /** 10 | * list_len - returns then number of elements in a list. 11 | * @s: singly linked list. 12 | * Return: number of elements in the list. 13 | */ 14 | 15 | size_t list_len(const list_t *s) 16 | { 17 | size_t nodes; 18 | 19 | for (nodes = 0 ; s != NULL ; nodes++) 20 | { 21 | s = s->next; 22 | } 23 | 24 | return (nodes); 25 | } 26 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/3-strcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strcmp - compares two strings 5 | * @s1: first string to compare 6 | * @s2: second string to compare 7 | * 8 | * Return: less than 0 if s1 is less than s2, 0 if they're equal, 9 | * more than 0 if s1 is greater than s2 10 | */ 11 | int _strcmp(char *s1, char *s2) 12 | { 13 | while (*s1 == *s2) 14 | { 15 | if (*s1 == '\0') 16 | { 17 | return (0); 18 | } 19 | s1++; 20 | s2++; 21 | } 22 | return (*s1 - *s2); 23 | } 24 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x15-file_io/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 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/3-print_alphabets.c: -------------------------------------------------------------------------------- 1 | /* 2 | * File: 3-print_alphabets.c 3 | * Auth: Solomon Kassa 4 | */ 5 | 6 | #include 7 | 8 | /** 9 | * main - Prints the alphabet in lowercase, and then in uppercase. 10 | * 11 | * Return: Always 0. 12 | */ 13 | int main(void) 14 | { 15 | char letter; 16 | 17 | for (letter = 'a'; letter <= 'z'; letter++) 18 | putchar(letter); 19 | 20 | for (letter = 'A'; letter <= 'Z'; letter++) 21 | putchar(letter); 22 | 23 | putchar('\n'); 24 | 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/8-print_base16.c: -------------------------------------------------------------------------------- 1 | /* 2 | * File: 8-print_base16.c 3 | * Auth: Solomon Kassa 4 | */ 5 | 6 | #include 7 | 8 | /** 9 | * main - Prints all the numbers of base 16 in lowercase. 10 | * 11 | * Return: Always 0. 12 | */ 13 | int main(void) 14 | { 15 | int num; 16 | char letter; 17 | 18 | for (num = 0; num < 10; num++) 19 | putchar((num % 10) + '0'); 20 | 21 | for (letter = 'a'; letter <= 'f'; letter++) 22 | putchar(letter); 23 | 24 | putchar('\n'); 25 | 26 | return (0); 27 | } 28 | -------------------------------------------------------------------------------- /0x03-debugging/2-largest_number.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | #include "main.h" 4 | 5 | /** 6 | * largest_number - returns the largest of 3 numbers 7 | * @a: first integer 8 | * @b: second integer 9 | * @c: third integer 10 | * Return: largest number 11 | */ 12 | 13 | int largest_number(int a, int b, int c) 14 | { 15 | int largest; 16 | 17 | if (a >= b && a >= c) 18 | { 19 | largest = a; 20 | } 21 | else if (b >= a && b >= c) 22 | { 23 | largest = b; 24 | } 25 | else 26 | { 27 | largest = c; 28 | } 29 | 30 | return (largest); 31 | } 32 | 33 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/0-strcat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strcat - concatenates two strings 5 | * @dest: string to append to 6 | * @src: string to add 7 | * 8 | * Return: a pointer to the resulting string 9 | */ 10 | char *_strcat(char *dest, char *src) 11 | { 12 | int i, j; 13 | 14 | i = 0; 15 | j = 0; 16 | 17 | while (dest[i] != '\0') 18 | i++; 19 | 20 | while (src[j] != '\0') 21 | { 22 | dest[i] = src[j]; 23 | j++; 24 | i++; 25 | } 26 | 27 | dest[i] = '\0'; 28 | 29 | return (dest); 30 | } 31 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/9-print_comb.c: -------------------------------------------------------------------------------- 1 | /* 2 | * File: 9-print_comb.c 3 | * Auth: Solomon Kassa 4 | */ 5 | 6 | #include 7 | 8 | /** 9 | * main - Prints all possible combinations of single-digit numbers. 10 | * 11 | * Return: Always 0. 12 | */ 13 | int main(void) 14 | { 15 | int num; 16 | 17 | for (num = 0; num <= 9; num++) 18 | { 19 | putchar((num % 10) + '0'); 20 | if (num == 9) 21 | continue; 22 | 23 | putchar(','); 24 | putchar(' '); 25 | } 26 | 27 | putchar('\n'); 28 | 29 | return (0); 30 | } 31 | -------------------------------------------------------------------------------- /0x03-debugging/3-main_b.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * main - takes a date and prints how many days are left in the year, taking 6 | * leap years into account 7 | * Return: 0 8 | */ 9 | 10 | int main(void) 11 | { 12 | int month; 13 | int day; 14 | int year; 15 | 16 | month = 2; 17 | day = 29; 18 | year = 2000; 19 | 20 | printf("Date: %02d/%02d/%04d\n", month, day , year); 21 | 22 | day = convert_day(month, day); 23 | 24 | print_remaining_days(month, day, year); 25 | 26 | return (0); 27 | } 28 | 29 | -------------------------------------------------------------------------------- /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/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 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/4-free_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_dlistint - frees a dlistint_t list. 5 | * @head: pointer to the list. 6 | **/ 7 | void free_dlistint(dlistint_t *head) 8 | { 9 | dlistint_t *actual_node; 10 | dlistint_t *next_node; 11 | 12 | if (head) 13 | { 14 | actual_node = head; 15 | next_node = head->next; 16 | while (next_node) 17 | { 18 | free(actual_node); 19 | actual_node = next_node; 20 | next_node = next_node->next; 21 | } 22 | free(actual_node); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/101-hello_holberton.asm: -------------------------------------------------------------------------------- 1 | ; File: 101-hello_holberton.asm 2 | ; Auth: Solomon Kassa 3 | ; Desc: 64-bit assembly program that prints 4 | ; Hello, Holberton followed by a new line. 5 | 6 | extern printf 7 | 8 | section .text 9 | global main 10 | 11 | main: 12 | push rbp 13 | 14 | mov rdi,fmt 15 | mov rsi,msg 16 | mov rax,0 17 | call printf 18 | 19 | pop rbp 20 | 21 | mov rax,0 22 | ret 23 | 24 | section .data 25 | msg: db "Hello, Holberton", 0 26 | fmt: db "%s", 10, 0 27 | 28 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/2-strncpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strncpy - copies a string 5 | * @dest: destination string 6 | * @src: source string 7 | * @n: number of bytes to copy 8 | * 9 | * Return: pointer to the resulting string 10 | */ 11 | char *_strncpy(char *dest, char *src, int n) 12 | { 13 | int i; 14 | 15 | i = 0; 16 | 17 | while (src[i] != '\0' && i < n) 18 | { 19 | dest[i] = src[i]; 20 | i++; 21 | } 22 | 23 | while (i < n) 24 | { 25 | dest[i] = '\0'; 26 | i++; 27 | } 28 | 29 | return (dest); 30 | } 31 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/8-print_square.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | *print_square - prints squares 6 | *@size: parameter 7 | *Return: returns nothing 8 | */ 9 | 10 | void print_square(int size) 11 | { 12 | int inc1, inc2; 13 | 14 | if (size > 0) 15 | { 16 | for (inc1 = 0; inc1 < size; inc1++) 17 | { 18 | for (inc2 = 0; inc2 < (size - 1); inc2++) 19 | { 20 | putchar('#'); 21 | } 22 | 23 | putchar('#'); 24 | putchar('\n'); 25 | } 26 | } 27 | else 28 | { 29 | putchar('\n'); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/100-prime_factor.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | *main- prints the largest prime factor 5 | *of a number 6 | * 7 | *Return: returns 0 8 | */ 9 | 10 | int main(void) 11 | { 12 | long number = 612852475143; 13 | int inc; 14 | 15 | while (inc++ < number / 2) 16 | { 17 | if (number % inc == 0) 18 | { 19 | number /= 2; 20 | continue; 21 | } 22 | 23 | for (inc = 3; inc < number / 2; inc += 2) 24 | { 25 | if (number % inc == 0) 26 | number /= inc; 27 | } 28 | } 29 | printf("%ld\n", number); 30 | return (0); 31 | } 32 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/2-strchr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * *_strchr - locates a character in a string 6 | * @s: string to search 7 | * @c: char to find 8 | * 9 | * Return: a pointer to the first occurrence of the character 10 | * c in the string s, or NULL if the character is not found 11 | */ 12 | char *_strchr(char *s, char c) 13 | { 14 | int a; 15 | 16 | while (1) 17 | { 18 | a = *s++; 19 | if (a == c) 20 | { 21 | return (s - 1); 22 | } 23 | if (a == 0) 24 | { 25 | return (NULL); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | void _print(char *str, int l); 5 | char *mul(char n, char *num, int num_index, char *dest, int dest_index); 6 | int check_for_digits(char **av); 7 | void init(char *str, int l); 8 | int _putchar(char c); 9 | void *malloc_checked(unsigned int b); 10 | char *string_nconcat(char *s1, char *s2, unsigned int n); 11 | void *_calloc(unsigned int nmemb, unsigned int size); 12 | int *array_range(int min, int max); 13 | void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size); 14 | 15 | 16 | #endif/*MAIN_H*/ 17 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/7-print_diagonal.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | *print_diagonal - prints a diagonal 6 | *@n: parameter 7 | *Return: returns nothing 8 | */ 9 | 10 | void print_diagonal(int n) 11 | { 12 | int len, space; 13 | 14 | if (n > 0) 15 | { 16 | for (len = 0; len < n; len++) 17 | { 18 | for (space = 0; space < len; space++) 19 | { 20 | putchar(' '); 21 | } 22 | 23 | putchar('\\'); 24 | 25 | if (len == (n - 1)) 26 | { 27 | continue; 28 | } 29 | putchar('\n'); 30 | } 31 | } 32 | putchar('\n'); 33 | } 34 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x09-static_libraries/1-strncat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strncat - concatenates two strings, 4 | * @dest: destination. 5 | * @src: source. 6 | * @n: amount of bytes used from src. 7 | * Return: the pointer to dest. 8 | */ 9 | char *_strncat(char *dest, char *src, int n) 10 | { 11 | int count = 0, count2 = 0; 12 | 13 | while (*(dest + count) != '\0') 14 | { 15 | count++; 16 | } 17 | 18 | while (count2 < n) 19 | { 20 | *(dest + count) = *(src + count2); 21 | if (*(src + count2) == '\0') 22 | break; 23 | count++; 24 | count2++; 25 | } 26 | return (dest); 27 | } 28 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/104-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char buffer[] = "This is a string!\0And this is the rest of the #buffer :)\1\2\3\4\5\6\7#cisfun\n\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\x20\x21\x34\x56#pointersarefun #infernumisfun\n"; 12 | /*char buffer[] = "This is th";*/ 13 | 14 | printf("%s\n", buffer); 15 | printf("---------------------------------\n"); 16 | print_buffer(buffer, sizeof(buffer)); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/0-linear.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * linear_search - implement linear search algo 5 | * @array: given array to search in 6 | * @size: size of the array 7 | * @value: value to search for 8 | * Return: first index or -1 9 | */ 10 | int linear_search(int *array, size_t size, int value) 11 | { 12 | size_t i; 13 | 14 | if (array == NULL) 15 | return (-1); 16 | for (i = 0; i < size; i++) 17 | { 18 | printf("Value checked array[%lu] = [%d]\n", i, array[i]); 19 | if (array[i] == value) 20 | return (i); 21 | 22 | } 23 | return (-1); 24 | 25 | } 26 | -------------------------------------------------------------------------------- /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: amount of bytes used from src. 7 | * Return: the pointer to dest. 8 | */ 9 | char *_strncat(char *dest, char *src, int n) 10 | { 11 | int count = 0, count2 = 0; 12 | 13 | while (*(dest + count) != '\0') 14 | { 15 | count++; 16 | } 17 | 18 | while (count2 < n) 19 | { 20 | *(dest + count) = *(src + count2); 21 | if (*(src + count2) == '\0') 22 | break; 23 | count++; 24 | count2++; 25 | } 26 | return (dest); 27 | } 28 | -------------------------------------------------------------------------------- /0x09-static_libraries/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 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/5-get_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * get_dnodeint_at_index - returns the nth node of a dlistint_t linked list. 5 | * @head: pointer to the list. 6 | * @index: nth node. 7 | * Return: nth node, if the node doesn't exist, returns NULL. 8 | **/ 9 | dlistint_t *get_dnodeint_at_index(dlistint_t *head, unsigned int index) 10 | { 11 | dlistint_t *node = head; 12 | unsigned int cont = 0; 13 | 14 | while (node && cont != index) 15 | { 16 | cont++; 17 | node = node->next; 18 | } 19 | if (node && cont == index) 20 | return (node); 21 | return (NULL); 22 | } 23 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "search_algos.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always EXIT_SUCCESS 9 | */ 10 | int main(void) 11 | { 12 | int array[] = { 13 | 10, 1, 42, 3, 4, 42, 6, 7, -1, 9 14 | }; 15 | size_t size = sizeof(array) / sizeof(array[0]); 16 | 17 | printf("Found %d at index: %d\n\n", 3, linear_search(array, size, 3)); 18 | printf("Found %d at index: %d\n\n", 42, linear_search(array, size, 42)); 19 | printf("Found %d at index: %d\n", 999, linear_search(array, size, 999)); 20 | return (EXIT_SUCCESS); 21 | } 22 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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/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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/1-strncat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strncat - concatenates n bytes from a string to another 5 | * @dest: destination string 6 | * @src: source string 7 | * @n: number of bytes of str to concatenate 8 | * 9 | * Return: a pointer to the resulting string dest 10 | */ 11 | char *_strncat(char *dest, char *src, int n) 12 | { 13 | int i, j; 14 | 15 | i = 0; 16 | j = 0; 17 | 18 | while (dest[i] != '\0') 19 | i++; 20 | 21 | while (src[j] != '\0' && j < n) 22 | { 23 | dest[i] = src[j]; 24 | i++; 25 | j++; 26 | } 27 | 28 | dest[i] = '\0'; 29 | 30 | return (dest); 31 | } 32 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/0-print_list.c: -------------------------------------------------------------------------------- 1 | /* 2 | * File: 0-print_list.c 3 | * Auth: Solomon Kassa 4 | */ 5 | 6 | #include "lists.h" 7 | #include 8 | 9 | /** 10 | * print_list - Prints all the elements of a list_t list. 11 | * @s: The list_t list. 12 | * 13 | * Return: The number of nodes in s. 14 | */ 15 | 16 | size_t print_list(const list_t *s) 17 | { 18 | size_t nodes = 0; 19 | 20 | while (s != NULL) 21 | { 22 | if (s->str == NULL) 23 | printf("[%d] %s\n", 0, "(nil)"); 24 | else 25 | printf("[%d] %s\n", s->len, s->str); 26 | s = s->next; 27 | nodes++; 28 | } 29 | return (nodes); 30 | } 31 | 32 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/README.md: -------------------------------------------------------------------------------- 1 | ##This is a function that converts a binary string representation of a number to an unsigned integer. The function takes a pointer to a null-terminated string of binary characters as its argument. The function scans the binary string from left to right, and for each '1' character encountered, it shifts the current value of the result to the left by 1 bit and sets the least significant bit to 1. For each '0' character encountered, it shifts the result to the left by 1 bit. If the function encounters a character other than '0' or '1', it returns 0. The final value of the result is returned as the result of the function. 2 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/9-strcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * *_strcpy - copies the string pointed to by src 5 | * including the terminating null byte (\0) 6 | * to the buffer pointed to by dest 7 | * @dest: pointer to the buffer in which we copy the string 8 | * @src: string to be copied 9 | * 10 | * Return: the pointer to dest 11 | */ 12 | char *_strcpy(char *dest, char *src) 13 | { 14 | int len, i; 15 | 16 | len = 0; 17 | 18 | while (src[len] != '\0') 19 | { 20 | len++; 21 | } 22 | 23 | for (i = 0; i < len; i++) 24 | { 25 | dest[i] = src[i]; 26 | } 27 | dest[i] = '\0'; 28 | 29 | return (dest); 30 | } 31 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/0-positive_or_negative.c: -------------------------------------------------------------------------------- 1 | /* 2 | * File: 0-positive_or_negative.c 3 | * Auth: Solomon Kassa 4 | */ 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | /** 11 | * main - Prints a random number and states whether 12 | * it is positive, negative, or zero. 13 | * 14 | * Return: Always 0. 15 | */ 16 | int main(void) 17 | { 18 | int n; 19 | 20 | srand(time(0)); 21 | n = rand() - RAND_MAX / 2; 22 | 23 | if (n > 0) 24 | printf("%d is positive\n", n); 25 | else if (n < 0) 26 | printf("%d is negative\n", n); 27 | else 28 | printf("%d is zero\n", n); 29 | 30 | return (0); 31 | } 32 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/5-strstr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * *_strstr - locates a substring 6 | * @haystack: string to search in 7 | * @needle: substring to look for 8 | * 9 | * Return: pointer to the beginning of the located substring 10 | * or NULL if the substring is not found 11 | */ 12 | char *_strstr(char *haystack, char *needle) 13 | { 14 | int i, j; 15 | 16 | for (i = 0; haystack[i] != '\0'; i++) 17 | { 18 | for (j = 0; needle[j] != '\0'; j++) 19 | { 20 | if (haystack[i + j] != needle[j]) 21 | break; 22 | } 23 | if (!needle[j]) 24 | return (&haystack[i]); 25 | } 26 | return (NULL); 27 | } 28 | -------------------------------------------------------------------------------- /0x00-hello_world/6-size.c: -------------------------------------------------------------------------------- 1 | /* 2 | * File: 6-size.c 3 | * Auth: Solomon kassa 4 | */ 5 | 6 | #include 7 | 8 | /** 9 | * main - Prints the size of various types based on 10 | * the computer it is compiled and run on.. 11 | * 12 | * Return: Always 0. 13 | */ 14 | int main(void) 15 | { 16 | printf("Size of a char: %zu byte(s)\n", sizeof(char)); 17 | printf("Size of an int: %zu byte(s)\n", sizeof(int)); 18 | printf("Size of a long int: %zu byte(s)\n", sizeof(long int)); 19 | printf("Size of a long long int: %zu byte(s)\n", sizeof(long long int)); 20 | printf("Size of a float: %zu byte(s)\n", sizeof(float)); 21 | 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/4-strpbrk.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * *_strpbrk - searches a string for any of a set of bytes 6 | * @s: string to search 7 | * @accept: stringcontaining the bytes to look for 8 | * 9 | * Return: pointer to the byte in s that matches one of the bytes in accept 10 | * or NULL if no such byte is found 11 | */ 12 | char *_strpbrk(char *s, char *accept) 13 | { 14 | int i, j; 15 | 16 | for (i = 0; *s != '\0'; i++) 17 | { 18 | for (j = 0; accept[j] != '\0'; j++) 19 | { 20 | if (*s == accept[j]) 21 | { 22 | return (s); 23 | } 24 | } 25 | s++; 26 | } 27 | 28 | return (NULL); 29 | } 30 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x1A-hash_tables/0-hash_table_create.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_create - function to create a hash table 5 | * @size: Size of the array 6 | * Return: Null for error or a pointer to a new hash table 7 | */ 8 | hash_table_t *hash_table_create(unsigned long int size) 9 | { 10 | hash_table_t *ht; 11 | unsigned long int i; 12 | 13 | ht = malloc(sizeof(hash_table_t)); 14 | if (ht == NULL) 15 | return (NULL); 16 | 17 | ht->size = size; 18 | ht->array = malloc(sizeof(hash_node_t *) * size); 19 | if (ht->array == NULL) 20 | return (NULL); 21 | for (i = 0; i < size; ++i) 22 | { 23 | ht->array[i] = NULL; 24 | } 25 | return (ht); 26 | } 27 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x1A-hash_tables/6-hash_table_delete.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_delete - function that delete a hash table 5 | * @ht: type pointer of the hash_tabe 6 | */ 7 | 8 | void hash_table_delete(hash_table_t *ht) 9 | { 10 | unsigned long int i; 11 | hash_table_t *head = ht; 12 | hash_node_t *node, *temp; 13 | 14 | for (i = 0; i < ht->size; i++) 15 | { 16 | if (ht->array[i] != NULL) 17 | { 18 | node = ht->array[i]; 19 | while (node != NULL) 20 | { 21 | temp = node->next; 22 | free(node->key); 23 | free(node->value); 24 | free(node); 25 | node = temp; 26 | } 27 | } 28 | } 29 | free(head->array); 30 | free(head); 31 | } 32 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/10-print_triangle.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | *print_triangle - prints a triangle 6 | *@size:size parameter of triangle 7 | *Return: returns nothing 8 | */ 9 | 10 | void print_triangle(int size) 11 | { 12 | int inc1, inc2; 13 | 14 | if (size > 0) 15 | { 16 | for (inc1 = 1; inc1 <= size; inc1++) 17 | { 18 | for ((inc2 = size - inc1); inc2 > 0; inc2--) 19 | { 20 | putchar(' '); 21 | } 22 | 23 | for (inc2 = 0; inc2 < inc1; inc2++) 24 | { 25 | putchar('#'); 26 | } 27 | 28 | if (inc1 == size) 29 | { 30 | continue; 31 | } 32 | 33 | putchar('\n'); 34 | } 35 | } 36 | putchar('\n'); 37 | } 38 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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/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 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/3-strspn.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * *_strspn - gets the length of a prefix substring 5 | * @s: string to evaluate 6 | * @accept: string containing the list of characters to match in s 7 | * 8 | * Return: the number of bytes in the initial segment 9 | * of s which consist only of bytes from accept 10 | */ 11 | unsigned int _strspn(char *s, char *accept) 12 | { 13 | int i, j, f, flag; 14 | 15 | f = 0; 16 | 17 | for (i = 0; s[i] != '\0'; i++) 18 | { 19 | flag = 0; 20 | for (j = 0; accept[j] != '\0'; j++) 21 | { 22 | if (s[i] == accept[j]) 23 | { 24 | f++; 25 | flag = 1; 26 | } 27 | } 28 | if (flag == 0) 29 | { 30 | return (f); 31 | } 32 | } 33 | 34 | return (0); 35 | } 36 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/100-print_comb3.c: -------------------------------------------------------------------------------- 1 | /* 2 | * File: 100-print_comb3.c 3 | * Auth: Solomon Kassa 4 | */ 5 | 6 | #include 7 | 8 | /** 9 | * main - Prints all possible combinations of two different digits, 10 | * in ascending order, separated by a comma followed by a space. 11 | * 12 | * Return: Always 0. 13 | */ 14 | int main(void) 15 | { 16 | int digit1, digit2; 17 | 18 | for (digit1 = 0; digit1 < 9; digit1++) 19 | { 20 | for (digit2 = digit1 + 1; digit2 < 10; digit2++) 21 | { 22 | putchar((digit1 % 10) + '0'); 23 | putchar((digit2 % 10) + '0'); 24 | if (digit1 == 8 && digit2 == 9) 25 | continue; 26 | putchar(','); 27 | putchar(' '); 28 | } 29 | } 30 | 31 | putchar('\n'); 32 | 33 | return (0); 34 | } 35 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/2-add_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_dnodeint - adds a new node at the beginning 5 | * of a dlistint_t list 6 | * 7 | * @head: head of the list 8 | * @n: value of the element 9 | * Return: the address of the new element 10 | */ 11 | dlistint_t *add_dnodeint(dlistint_t **head, const int n) 12 | { 13 | dlistint_t *new; 14 | dlistint_t *h; 15 | 16 | new = malloc(sizeof(dlistint_t)); 17 | if (new == NULL) 18 | return (NULL); 19 | 20 | new->n = n; 21 | new->prev = NULL; 22 | h = *head; 23 | 24 | if (h != NULL) 25 | { 26 | while (h->prev != NULL) 27 | h = h->prev; 28 | } 29 | 30 | new->next = h; 31 | 32 | if (h != NULL) 33 | h->prev = new; 34 | 35 | *head = new; 36 | 37 | return (new); 38 | } 39 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/3-add_dnodeint_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_dnodeint_end - adds a new node at the end of a dlistint_t list. 5 | * @head: pointer to the first element of the list. 6 | * @n: int to set in the new node. 7 | * Return: address of the new element, or NULL if it failed 8 | **/ 9 | dlistint_t *add_dnodeint_end(dlistint_t **head, const int n) 10 | { 11 | dlistint_t *new, *aux = *head; 12 | 13 | new = malloc(sizeof(dlistint_t)); 14 | if (new == NULL) 15 | return (NULL); 16 | new->n = n; 17 | new->next = NULL; 18 | 19 | if (aux) 20 | { 21 | while (aux->next) 22 | aux = aux->next; 23 | new->prev = aux; 24 | aux->next = new; 25 | } 26 | else 27 | { 28 | *head = new; 29 | new->prev = NULL; 30 | } 31 | 32 | return (new); 33 | } 34 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * main - check the code for Holberton School students. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | int n; 11 | 12 | n = _atoi("xd-xds5"); 13 | /*n = 1000000000;*/ 14 | printf("%d\n", n); 15 | n = _atoi("-402"); 16 | printf("%d\n", n); 17 | n = _atoi(" ------++++++-----+++++--98"); 18 | printf("%d\n", n); 19 | n = _atoi("214748364"); 20 | printf("%d\n", n); 21 | n = _atoi("0"); 22 | printf("%d\n", n); 23 | n = _atoi("Suite 402"); 24 | printf("%d\n", n); 25 | n = _atoi(" + + - -98 Battery Street; San Francisco, CA 94111 - USA "); 26 | printf("%d\n", n); 27 | n = _atoi("---++++ -++ Sui - te - 402 #cisfun :)"); 28 | printf("%d\n", n); 29 | return (0); 30 | } 31 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/100-atoi.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _atoi - converts a string to an integer 5 | * @s: string to be converted 6 | * 7 | * Return: the int converted from the string 8 | */ 9 | int _atoi(char *s) 10 | { 11 | int i, d, n, len, f, digit; 12 | 13 | i = 0; 14 | d = 0; 15 | n = 0; 16 | len = 0; 17 | f = 0; 18 | digit = 0; 19 | 20 | while (s[len] != '\0') 21 | len++; 22 | 23 | while (i < len && f == 0) 24 | { 25 | if (s[i] == '-') 26 | ++d; 27 | 28 | if (s[i] >= '0' && s[i] <= '9') 29 | { 30 | digit = s[i] - '0'; 31 | if (d % 2) 32 | digit = -digit; 33 | n = n * 10 + digit; 34 | f = 1; 35 | if (s[i + 1] < '0' || s[i + 1] > '9') 36 | break; 37 | f = 0; 38 | } 39 | i++; 40 | } 41 | 42 | if (f == 0) 43 | return (0); 44 | 45 | return (n); 46 | } 47 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/102-print_comb5.c: -------------------------------------------------------------------------------- 1 | /* 2 | * File: 102-print_comb5.c 3 | * Auth: Solomon Kassa 4 | */ 5 | 6 | #include 7 | 8 | /** 9 | * main - Prints all possible combinations of two two-digit numbers, 10 | * ranging from 0-99, separated by a comma followed by a space. 11 | * 12 | * Return: Always 0. 13 | */ 14 | int main(void) 15 | { 16 | int num1, num2; 17 | 18 | for (num1 = 0; num1 <= 98; num1++) 19 | { 20 | for (num2 = num1 + 1; num2 <= 99; num2++) 21 | { 22 | putchar((num1 / 10) + '0'); 23 | putchar((num1 % 10) + '0'); 24 | putchar(' '); 25 | putchar((num2 / 10) + '0'); 26 | putchar((num2 % 10) + '0'); 27 | 28 | if (num1 == 98 && num2 == 99) 29 | continue; 30 | 31 | putchar(','); 32 | putchar(' '); 33 | } 34 | } 35 | 36 | putchar('\n'); 37 | 38 | return (0); 39 | } 40 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/main.h: -------------------------------------------------------------------------------- 1 | #ifndef DYNAMIC_LIB 2 | #define DYNAMIC_LIB 3 | #include 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 *str); 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 | #endif /* DYNAMIC_LIB */ 25 | -------------------------------------------------------------------------------- /0x1A-hash_tables/5-hash_table_print.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_print - fucntion to print a hash table 5 | * @ht: type pointer the hash_table 6 | */ 7 | 8 | void hash_table_print(const hash_table_t *ht) 9 | { 10 | unsigned long int i; 11 | hash_node_t *current_node; 12 | bool flag = false; 13 | 14 | if (ht == NULL) 15 | return; 16 | 17 | printf("{"); 18 | for (i = 0; i < ht->size; i++) 19 | { 20 | if (ht->array[i] != NULL) 21 | { 22 | if (flag == true) 23 | printf(", "); 24 | 25 | for (current_node = ht->array[i]; current_node != NULL; 26 | flag = true) 27 | { 28 | printf("'%s': '%s'", current_node->key, 29 | current_node->value); 30 | current_node = current_node->next; 31 | if (current_node != NULL) 32 | printf(", "); 33 | } 34 | } 35 | } 36 | printf("}\n"); 37 | } 38 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/1-last_digit.c: -------------------------------------------------------------------------------- 1 | /* 2 | * File: 1-last_digit.c 3 | * Auth: Solomon Kassa 4 | */ 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | /** 11 | * main - Prints the last digit of a randomly generated number 12 | * and whether it is greater than 5, less than 6, or 0. 13 | * 14 | * Return: Always 0. 15 | */ 16 | int main(void) 17 | { 18 | int n; 19 | 20 | srand(time(0)); 21 | n = rand() - RAND_MAX / 2; 22 | 23 | if ((n % 10) > 5) 24 | { 25 | printf("Last digit of %d is %d and is greater than 5\n", 26 | n, n % 10); 27 | } 28 | else if ((n % 10) < 6 && (n % 10) != 0) 29 | { 30 | printf("Last digit of %d is %d and is less than 6 and not 0\n", 31 | n, n % 10); 32 | } 33 | else 34 | { 35 | printf("Last digit of %d is %d and is 0\n", 36 | n, n % 10); 37 | } 38 | 39 | return (0); 40 | 41 | } 42 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/101-print_comb4.c: -------------------------------------------------------------------------------- 1 | /* 2 | * File: 101-print_comb4.c 3 | * Auth: Solomon Kassa 4 | */ 5 | 6 | #include 7 | 8 | /** 9 | * main - Prints all possible combinations of three different digits, 10 | * in ascending order, separated by a comma followed by a space. 11 | * 12 | * Return: Always 0. 13 | */ 14 | int main(void) 15 | { 16 | int digit1, digit2, digit3; 17 | 18 | for (digit1 = 0; digit1 < 8; digit1++) 19 | { 20 | for (digit2 = digit1 + 1; digit2 < 9; digit2++) 21 | { 22 | for (digit3 = digit2 + 1; digit3 < 10; digit3++) 23 | { 24 | putchar((digit1 % 10) + '0'); 25 | putchar((digit2 % 10) + '0'); 26 | putchar((digit3 % 10) + '0'); 27 | if (digit1 == 7 && digit2 == 8 && digit3 == 9) 28 | continue; 29 | putchar(','); 30 | putchar(' '); 31 | } 32 | } 33 | } 34 | 35 | putchar('\n'); 36 | 37 | return (0); 38 | } 39 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x1C-makefiles/5-island_perimeter.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | """Module that calculates the perimeter of an island in a grid.""" 3 | 4 | 5 | def num_water_neighbors(grid, i, j): 6 | """Returns the number of water neighbors a cell has in a grid.""" 7 | 8 | num = 0 9 | 10 | if i <= 0 or not grid[i - 1][j]: 11 | num += 1 12 | if j <= 0 or not grid[i][j - 1]: 13 | num += 1 14 | if j >= len(grid[i]) - 1 or not grid[i][j + 1]: 15 | num += 1 16 | if i >= len(grid) - 1 or not grid[i + 1][j]: 17 | num += 1 18 | 19 | return num 20 | 21 | 22 | def island_perimeter(grid): 23 | """Returns the perimeter of the island in grid.""" 24 | 25 | perimeter = 0 26 | for i in range(len(grid)): 27 | for j in range(len(grid[i])): 28 | if grid[i][j]: 29 | perimeter += num_water_neighbors(grid, i, j) 30 | 31 | return perimeter 32 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/1-binary.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * binary_search - searches for a value in a sorted array of integers 5 | * using the binary search algorithm 6 | * @array: array of integers 7 | * @size: size of array 8 | * @value: value to search for 9 | * Return: index of value or -1 if not found 10 | */ 11 | 12 | int binary_search(int *array, size_t size, int value) 13 | { 14 | 15 | size_t i, left, right; 16 | 17 | if (array == NULL) 18 | return (-1); 19 | 20 | for (left = 0, right = size - 1; right >= left;) 21 | { 22 | printf("Searching in array: "); 23 | for (i = left; i < right; i++) 24 | printf("%d, ", array[i]); 25 | printf("%d\n", array[i]); 26 | 27 | i = left + (right - left) / 2; 28 | if (array[i] == value) 29 | return (i); 30 | if (array[i] > value) 31 | right = i - 1; 32 | else 33 | left = i + 1; 34 | } 35 | 36 | return (-1); 37 | } 38 | -------------------------------------------------------------------------------- /0x15-file_io/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/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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x1A-hash_tables/4-hash_table_get.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_get - function to get a value of a key in a hash table 5 | * @ht: type pointer const char of the hash_table 6 | * @key: type pointer char the key of the hash table 7 | * Return: Null if the key is not found or the value of the key 8 | */ 9 | 10 | char *hash_table_get(const hash_table_t *ht, const char *key) 11 | { 12 | unsigned long int idx; 13 | hash_node_t *current_node; 14 | 15 | if (ht == NULL || *key == 48 || key == NULL) 16 | return (NULL); 17 | 18 | idx = key_index((const unsigned char *)key, ht->size); 19 | if (idx >= ht->size) 20 | return (NULL); 21 | 22 | for (current_node = ht->array[idx]; 23 | current_node && strcmp(current_node->key, key) != 0; 24 | current_node = current_node->next) 25 | ; 26 | if (current_node == NULL) 27 | { 28 | return (NULL); 29 | } 30 | else 31 | { 32 | return (current_node->value); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/2-add_node.c: -------------------------------------------------------------------------------- 1 | /* 2 | * File: 2-add_node.c 3 | * Auth: Solomon Kassa 4 | */ 5 | 6 | #include "lists.h" 7 | #include 8 | 9 | /** 10 | * add_node - Adds a new node at the beginning 11 | * of a list_t list. 12 | * @head: A pointer to the head of the list_t list. 13 | * @str: The string to be added to the list_t list. 14 | * 15 | * Return: If the function fails - NULL. 16 | * Otherwise - the address of the new element. 17 | */ 18 | list_t *add_node(list_t **head, const char *str) 19 | { 20 | char *dup; 21 | int len; 22 | list_t *new; 23 | 24 | new = malloc(sizeof(list_t)); 25 | if (new == NULL) 26 | return (NULL); 27 | 28 | dup = strdup(str); 29 | if (dup == NULL) 30 | { 31 | free(new); 32 | return (NULL); 33 | } 34 | 35 | for (len = 0; str[len];) 36 | len++; 37 | 38 | new->str = dup; 39 | new->len = len; 40 | new->next = *head; 41 | 42 | *head = new; 43 | 44 | return (new); 45 | } 46 | 47 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef LISTS_H 2 | #define LISTS_H 3 | 4 | /* 5 | * File: lists.h 6 | * Auth: Solomon Kassa 7 | * Desc: Header file containing prototypes and definitions for all functions 8 | * and types written in the 0x11-singly_linked_lists directory. 9 | */ 10 | 11 | #include 12 | 13 | /** 14 | * struct list_s - singly linked list 15 | * @str: string - (malloc'ed string) 16 | * @len: length of the string 17 | * @next: points to the next node 18 | * 19 | * Description: singly linked list node structure 20 | * for Alx project 21 | */ 22 | typedef struct list_s 23 | { 24 | char *str; 25 | unsigned int len; 26 | struct list_s *next; 27 | } list_t; 28 | 29 | size_t print_list(const list_t *h); 30 | size_t list_len(const list_t *h); 31 | list_t *add_node(list_t **head, const char *str); 32 | list_t *add_node_end(list_t **head, const char *str); 33 | void free_list(list_t *head); 34 | 35 | #endif /* LISTS_H */ 36 | 37 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef LISTS 2 | #define LISTS 3 | #include 4 | #include 5 | #include 6 | 7 | /** 8 | * struct dlistint_s - doubly linked list 9 | * @n: integer 10 | * @prev: points to the previous node 11 | * @next: points to the next node 12 | * 13 | * Description: doubly linked list node structure 14 | * for Alx project 15 | */ 16 | typedef struct dlistint_s 17 | { 18 | int n; 19 | struct dlistint_s *prev; 20 | struct dlistint_s *next; 21 | } dlistint_t; 22 | 23 | size_t print_dlistint(const dlistint_t *h); 24 | size_t dlistint_len(const dlistint_t *h); 25 | dlistint_t *add_dnodeint(dlistint_t **head, const int n); 26 | dlistint_t *add_dnodeint_end(dlistint_t **head, const int n); 27 | void free_dlistint(dlistint_t *head); 28 | dlistint_t *get_dnodeint_at_index(dlistint_t *head, unsigned int index); 29 | int sum_dlistint(dlistint_t *head); 30 | dlistint_t *insert_dnodeint_at_index(dlistint_t **h, unsigned int idx, int n); 31 | int delete_dnodeint_at_index(dlistint_t **head, unsigned int index); 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/3-add_node_end.c: -------------------------------------------------------------------------------- 1 | /* 2 | * File: 3-add_node_end.c 3 | * Auth: Solomon Kassa 4 | */ 5 | 6 | #include "lists.h" 7 | #include 8 | 9 | /** 10 | * add_node_end - Adds a new node at the end 11 | * of a list_t list. 12 | * @head: A pointer the head of the list_t list. 13 | * @str: The string to be added to the list_t list. 14 | * 15 | * Return: If the function fails - NULL. 16 | * Otherwise - the address of the new element. 17 | */ 18 | list_t *add_node_end(list_t **head, const char *str) 19 | { 20 | char *dup; 21 | int len; 22 | list_t *new, *last; 23 | 24 | new = malloc(sizeof(list_t)); 25 | if (new == NULL) 26 | return (NULL); 27 | 28 | dup = strdup(str); 29 | if (str == NULL) 30 | { 31 | free(new); 32 | return (NULL); 33 | } 34 | 35 | for (len = 0; str[len];) 36 | len++; 37 | 38 | new->str = dup; 39 | new->len = len; 40 | new->next = NULL; 41 | 42 | if (*head == NULL) 43 | *head = new; 44 | 45 | else 46 | { 47 | last = *head; 48 | while (last->next != NULL) 49 | last = last->next; 50 | last->next = new; 51 | } 52 | 53 | return (*head); 54 | } 55 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/100-jump.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | #include 3 | 4 | /** 5 | * jump_search - Searches for a value in a sorted array of integers 6 | * @array: A pointer to the first element of the array to search in 7 | * @size: The number of elements in the array 8 | * @value: The value to search for 9 | * 10 | * Return: The first index where the value is located, or -1 if not found 11 | */ 12 | int jump_search(int *array, size_t size, int value) 13 | { 14 | size_t i, step, jump; 15 | 16 | if (array == NULL || size == 0) 17 | return (-1); 18 | 19 | step = sqrt(size); 20 | 21 | for (i = jump = 0; jump < size && array[jump] < value;) 22 | { 23 | printf("Value checked array[%ld] = [%d]\n", jump, array[jump]); 24 | i = jump; 25 | jump += step; 26 | } 27 | 28 | printf("Value found between indexes [%ld] and [%ld]\n", i, jump); 29 | 30 | jump = jump > size - 1 ? jump : size - 1; 31 | for (; i < jump && array[i] < value; i++) 32 | printf("Value checked array[%ld] = [%d]\n", i, array[i]); 33 | printf("Value checked array[%ld] = [%d]\n", i, array[i]); 34 | 35 | return (array[i] == value ? (int)i : -1); 36 | } 37 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/operations.c: -------------------------------------------------------------------------------- 1 | int add(int i, int k); 2 | int sub(int i, int k); 3 | int mul(int i, int k); 4 | int div(int i, int k); 5 | int mod(int i, int k); 6 | 7 | /** 8 | * add - adds two integers 9 | * @i: first integer to add 10 | * @k: second integer to add 11 | * Return: the sum 12 | */ 13 | 14 | int add(int i, int k) 15 | { 16 | return (i + k); 17 | } 18 | 19 | /** 20 | * sub - subtracts two integers 21 | * @i: first integer to add 22 | * @k: second integer to add 23 | * Return: the subtract 24 | */ 25 | 26 | int sub(int i, int k) 27 | { 28 | return (i - k); 29 | } 30 | 31 | /** 32 | * mul - multiplies two integers 33 | * @i: first integer to add 34 | * @k: second integer to add 35 | * Return: the multiple 36 | */ 37 | 38 | int mul(int i, int k) 39 | { 40 | return (i * k); 41 | } 42 | 43 | /** 44 | * div - divideds two integers 45 | * @i: first integer to add 46 | * @k: second integer to add 47 | * Return: the dividend integer 48 | */ 49 | 50 | int div(int i, int k) 51 | { 52 | return (i / k); 53 | } 54 | 55 | /** 56 | mod - finds the modulus of two integers 57 | @i: first integer to add 58 | @k: second integer to add 59 | Return: the modulus 60 | */ 61 | 62 | int mod(int i, int k) 63 | { 64 | return (i % k); 65 | } 66 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/102-interpolation.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * interpolation_search - Searches for a value in a sorted array 5 | * of integers using interpolation search. 6 | * @array: A pointer to the first element of the array to search. 7 | * @size: The number of elements in the array. 8 | * @value: The value to search for. 9 | * 10 | * Return: If the value is not present or the array is NULL, -1. 11 | * Otherwise, the first index where the value is located. 12 | * 13 | * Description: Prints a value every time it is compared in the array.. 14 | */ 15 | int interpolation_search(int *array, size_t size, int value) 16 | { 17 | size_t i, l, r; 18 | 19 | if (array == NULL) 20 | return (-1); 21 | 22 | for (l = 0, r = size - 1; r >= l;) 23 | { 24 | i = l + (((double)(r - l) / (array[r] - array[l])) * (value - array[l])); 25 | if (i < size) 26 | printf("Value checked array[%ld] = [%d]\n", i, array[i]); 27 | else 28 | { 29 | printf("Value checked array[%ld] is out of range\n", i); 30 | break; 31 | } 32 | 33 | if (array[i] == value) 34 | return (i); 35 | if (array[i] > value) 36 | r = i - 1; 37 | else 38 | l = i + 1; 39 | } 40 | 41 | return (-1); 42 | } 43 | -------------------------------------------------------------------------------- /0x1A-hash_tables/3-hash_table_set.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_set - add an element at the hash table 5 | * @ht: Type pointer to the hash table 6 | * @key: Type const char pointer of the key 7 | * @value: Value of the key 8 | * Return: 0 if fail or 1 if success 9 | */ 10 | int hash_table_set(hash_table_t *ht, const char *key, const char *value) 11 | { 12 | unsigned long int i, idx; 13 | char *copy_val; 14 | hash_node_t *new_ht; 15 | 16 | if (ht == NULL || key == NULL || *key == 48 || value == NULL) 17 | return (0); 18 | 19 | copy_val = strdup(value); 20 | if (copy_val == NULL) 21 | return (0); 22 | 23 | idx = key_index((const unsigned char *)key, ht->size); 24 | for (i = idx; ht->array[i]; ++i) 25 | { 26 | if (strcmp(ht->array[i]->key, key) == 0) 27 | { 28 | free(ht->array[i]->value); 29 | ht->array[i]->value = copy_val; 30 | return (1); 31 | } 32 | } 33 | new_ht = malloc(sizeof(hash_node_t)); 34 | if (new_ht == NULL) 35 | { 36 | free(copy_val); 37 | return (0); 38 | } 39 | new_ht->key = strdup(key); 40 | if (new_ht->key == NULL) 41 | { 42 | free(new_ht); 43 | return (0); 44 | } 45 | new_ht->value = copy_val; 46 | new_ht->next = ht->array[idx]; 47 | ht->array[idx] = new_ht; 48 | 49 | return (1); 50 | } 51 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/8-delete_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * delete_dnodeint_at_index - deletes the node at index of a dlistint_t list. 5 | * @head: pointer to the list. 6 | * @index: position of the node to delete. 7 | * Return: 1 if it succeeded, -1 if it failed. 8 | **/ 9 | int delete_dnodeint_at_index(dlistint_t **head, unsigned int index) 10 | { 11 | dlistint_t *aux_node = *head; 12 | dlistint_t *node_to_delete = *head; 13 | unsigned int idx; 14 | unsigned int cont = 0; 15 | 16 | /* border case for empty list */ 17 | if (!(*head)) 18 | return (-1); 19 | 20 | /* border case for delete at the beginning */ 21 | if (index == 0) 22 | { 23 | *head = node_to_delete->next; 24 | free(node_to_delete); 25 | if (*head) 26 | (*head)->prev = NULL; 27 | return (1); 28 | } 29 | 30 | /* search of position to delete */ 31 | idx = index - 1; 32 | while (aux_node && cont != idx) 33 | { 34 | cont++; 35 | aux_node = aux_node->next; 36 | } 37 | 38 | /* general case */ 39 | if (cont == idx && aux_node) 40 | { 41 | node_to_delete = aux_node->next; 42 | if (node_to_delete->next) 43 | node_to_delete->next->prev = aux_node; 44 | aux_node->next = node_to_delete->next; 45 | free(node_to_delete); 46 | return (1); 47 | } 48 | 49 | return (-1); 50 | } 51 | -------------------------------------------------------------------------------- /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/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 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/7-insert_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * insert_dnodeint_at_index - inserts a node node at a given position 5 | * in a dlistint_t list. 6 | * @h: pointer to the list. 7 | * @idx: position to add the node. 8 | * @n: data for the new node. 9 | * Return: the address of the new node, or NULL if it failed 10 | **/ 11 | dlistint_t *insert_dnodeint_at_index(dlistint_t **h, unsigned int idx, int n) 12 | { 13 | dlistint_t *aux_node = *h, *new_node; 14 | unsigned int index, cont = 0; 15 | 16 | /* create node */ 17 | new_node = malloc(sizeof(dlistint_t)); 18 | if (new_node == NULL) 19 | return (NULL); 20 | new_node->n = n; 21 | 22 | /* border case for insert at the beginning */ 23 | if (idx == 0) 24 | { 25 | new_node->prev = NULL; 26 | new_node->next = *h; 27 | if (*h) 28 | (*h)->prev = new_node; 29 | *h = new_node; 30 | return (*h); 31 | } 32 | 33 | /* search of position to insert */ 34 | index = idx - 1; 35 | while (aux_node && cont != index) 36 | { 37 | cont++; 38 | aux_node = aux_node->next; 39 | } 40 | 41 | /* general case */ 42 | if (cont == index && aux_node) 43 | { 44 | new_node->prev = aux_node; 45 | new_node->next = aux_node->next; 46 | if (aux_node->next) 47 | aux_node->next->prev = new_node; 48 | aux_node->next = new_node; 49 | return (new_node); 50 | } 51 | free(new_node); 52 | return (NULL); 53 | } 54 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/106-linear_skip.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * linear_skip - Searches for an algorithm in a sorted singly 5 | * linked list of integers using linear skip. 6 | * @list: A pointer to the head of the linked list to search. 7 | * @value: The value to search for. 8 | * 9 | * Return: If the value is not present or the head of the list is NULL, NULL. 10 | * Otherwise, a pointer to the first node where the value is located. 11 | * 12 | * Description: Prints a value every time it is compared in the list. 13 | * Uses the square root of the list size as the jump step. 14 | */ 15 | skiplist_t *linear_skip(skiplist_t *list, int value) 16 | { 17 | skiplist_t *node, *jump; 18 | 19 | if (list == NULL) 20 | return (NULL); 21 | 22 | for (node = jump = list; jump->next != NULL && jump->n < value;) 23 | { 24 | node = jump; 25 | if (jump->express != NULL) 26 | { 27 | jump = jump->express; 28 | printf("Value checked at index [%ld] = [%d]\n", 29 | jump->index, jump->n); 30 | } 31 | else 32 | { 33 | while (jump->next != NULL) 34 | jump = jump->next; 35 | } 36 | } 37 | 38 | printf("Value found between indexes [%ld] and [%ld]\n", 39 | node->index, jump->index); 40 | 41 | for (; node->index < jump->index && node->n < value; node = node->next) 42 | printf("Value checked at index [%ld] = [%d]\n", node->index, node->n); 43 | printf("Value checked at index [%ld] = [%d]\n", node->index, node->n); 44 | 45 | return (node->n == value ? node : NULL); 46 | } 47 | -------------------------------------------------------------------------------- /0x10-variadic_functions/README.md: -------------------------------------------------------------------------------- 1 | Variadic functions 2 | Variadic functions are functions that can take a variable number of arguments. In C programming, a variadic function adds flexibility to the program. It takes one fixed argument and then any number of arguments can be passed. The variadic function consists of at least one fixed variable and then an ellipsis(…) as the last parameter. 3 | 4 | Syntax: 5 | 6 | int function_name(data_type variable_name, ...); 7 | Values of the passed arguments can be accessed through the header file named as: 8 | 9 | #include 10 | includes the following methods: 11 | 12 | 13 | Methods 14 | 15 | Description 16 | 17 | va_start(va_list ap, argN) 18 | This enables access to variadic function arguments. 19 | 20 | where *va_list* will be the pointer to the last fixed argument in the variadic function 21 | 22 | *argN* is the last fixed argument in the variadic function. 23 | 24 | From the above variadic function (function_name (data_type variable_name, …);), variable_name is the last fixed argument making it the argN. Whereas *va_list ap* will be a pointer to argN (variable_name) 25 | 26 | va_arg(va_list ap, type) 27 | This one accesses the next variadic function argument. 28 | 29 | *va_list ap* is the same as above i.e a pointer to argN 30 | 31 | *type* indicates the data type the *va_list ap* should expect (double, float, int etc.) 32 | 33 | va_copy(va_list dest, va_list src) This makes a copy of the variadic function arguments. 34 | va_end(va_list ap) This ends the traversal of the variadic function arguments. 35 | -------------------------------------------------------------------------------- /0x1A-hash_tables/README.md: -------------------------------------------------------------------------------- 1 | # Hash Table Implementation in C 2 | 3 | This repository contains a simple implementation of a hash table in C programming language. 4 | 5 | ## Usage 6 | 7 | To use the hash table implementation in your code, follow these steps: 8 | 9 | 1. Copy the `hash_table.h` and `hash_table.c` files to your project directory. 10 | 2. Include the `hash_table.h` header file in your code: `#include "hash_table.h"` 11 | 3. Create a new hash table by calling the `create_hash_table()` function and passing in the desired size of the hash table: 12 | 13 | 4. Insert key-value pairs into the hash table using the `insert()` function: 14 | 15 | 5. Retrieve values from the hash table using the `get()` function: 16 | 17 | 6. Delete key-value pairs from the hash table using the `delete()` function: 18 | 19 | 20 | ## Functions 21 | 22 | ### `struct hash_table* create_hash_table(int size)` 23 | 24 | This function creates a new hash table with the specified size and returns a pointer to the new hash table. 25 | 26 | ### `void insert(struct hash_table* ht, char* key, char* value)` 27 | 28 | This function inserts a new key-value pair into the hash table. 29 | 30 | ### `char* get(struct hash_table* ht, char* key)` 31 | 32 | This function retrieves the value associated with the specified key from the hash table. 33 | 34 | ### `void delete(struct hash_table* ht, char* key)` 35 | 36 | This function deletes the key-value pair associated with the specified key from the hash table. 37 | 38 | ## License 39 | 40 | This implementation of a hash table is licensed under the [MIT License](LICENSE). 41 | 42 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/search_algos.h: -------------------------------------------------------------------------------- 1 | #ifndef SEARCH_ALGOS_H 2 | #define SEARCH_ALGOS_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | /** 9 | * struct listint_s - singly linked list 10 | * 11 | * @n: Integer 12 | * @index: Index of the node in the list 13 | * @next: Pointer to the next node 14 | * 15 | * Description: singly linked list node structure 16 | * for Holberton project 17 | */ 18 | typedef struct listint_s 19 | { 20 | int n; 21 | size_t index; 22 | struct listint_s *next; 23 | } listint_t; 24 | 25 | /** 26 | * struct skiplist_s - Singly linked list with an express lane 27 | * 28 | * @n: Integer 29 | * @index: Index of the node in the list 30 | * @next: Pointer to the next node 31 | * @express: Pointer to the next node in the express lane 32 | * 33 | * Description: singly linked list node structure with an express lane 34 | * for Holberton project 35 | */ 36 | typedef struct skiplist_s 37 | { 38 | int n; 39 | size_t index; 40 | struct skiplist_s *next; 41 | struct skiplist_s *express; 42 | } skiplist_t; 43 | 44 | int linear_search(int *array, size_t size, int value); 45 | int binary_search(int *array, size_t size, int value); 46 | int jump_search(int *array, size_t size, int value); 47 | int interpolation_search(int *array, size_t size, int value); 48 | int exponential_search(int *array, size_t size, int value); 49 | int advanced_binary(int *array, size_t size, int value); 50 | listint_t *jump_list(listint_t *list, size_t size, int value); 51 | skiplist_t *linear_skip(skiplist_t *list, int value); 52 | 53 | 54 | #endif 55 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/105-jump_list.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * jump_list - Searches for an algorithm in a sorted singly 5 | * linked list of integers using jump search. 6 | * @list: A pointer to the head of the linked list to search. 7 | * @size: The number of nodes in the list. 8 | * @value: The value to search for. 9 | * 10 | * Return: If the value is not present or the head of the list is NULL, NULL. 11 | * Otherwise, a pointer to the first node where the value is located. 12 | * 13 | * Description: Prints a value every time it is compared in the list. 14 | * Uses the square root of the list size as the jump step. 15 | */ 16 | listint_t *jump_list(listint_t *list, size_t size, int value) 17 | { 18 | size_t step, step_size; 19 | listint_t *node, *jump; 20 | 21 | if (list == NULL || size == 0) 22 | return (NULL); 23 | 24 | step = 0; 25 | step_size = sqrt(size); 26 | for (node = jump = list; jump->index + 1 < size && jump->n < value;) 27 | { 28 | node = jump; 29 | for (step += step_size; jump->index < step; jump = jump->next) 30 | { 31 | if (jump->index + 1 == size) 32 | break; 33 | } 34 | printf("Value checked at index [%ld] = [%d]\n", jump->index, jump->n); 35 | } 36 | 37 | printf("Value found between indexes [%ld] and [%ld]\n", 38 | node->index, jump->index); 39 | 40 | for (; node->index < jump->index && node->n < value; node = node->next) 41 | printf("Value checked at index [%ld] = [%d]\n", node->index, node->n); 42 | printf("Value checked at index [%ld] = [%d]\n", node->index, node->n); 43 | 44 | return (node->n == value ? node : NULL); 45 | } 46 | -------------------------------------------------------------------------------- /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 | 74 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/104-advanced_binary.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | 4 | /** 5 | * advanced_binary_recursive - Searches recursively for a value in a sorted 6 | * array of integers using binary search. 7 | * @array: A pointer to the first element of the [sub]array to search. 8 | * @left: The starting index of the [sub]array to search. 9 | * @right: The ending index of the [sub]array to search. 10 | * @value: The value to search for. 11 | * 12 | * Return: If the value is not present, -1. 13 | * Otherwise, the index where the value is located. 14 | * 15 | * Description: Prints the [sub]array being searched after each change. 16 | */ 17 | int advanced_binary_recursive(int *array, size_t left, size_t right, int value) 18 | { 19 | size_t i; 20 | 21 | if (right < left) 22 | return (-1); 23 | 24 | printf("Searching in array: "); 25 | for (i = left; i < right; i++) 26 | printf("%d, ", array[i]); 27 | printf("%d\n", array[i]); 28 | 29 | i = left + (right - left) / 2; 30 | if (array[i] == value && (i == left || array[i - 1] != value)) 31 | return (i); 32 | if (array[i] >= value) 33 | return (advanced_binary_recursive(array, left, i, value)); 34 | return (advanced_binary_recursive(array, i + 1, right, value)); 35 | } 36 | 37 | 38 | 39 | /** 40 | *advanced_binary - a function that searches for a 41 | * value in a sorted array of integers. 42 | * 43 | * @array: pointer to the first element in the array 44 | * @size: Size of the array to search in 45 | * @value: Element to be searched 46 | * Return: return index of searched element 47 | */ 48 | 49 | int advanced_binary(int *array, size_t size, int value) 50 | { 51 | if (array == NULL || size == 0) 52 | return (-1); 53 | 54 | return (advanced_binary_recursive(array, 0, size - 1, value)); 55 | } 56 | --------------------------------------------------------------------------------