├── 0x1A-hash_tables ├── main.c ├── README.md ├── 2-key_index.c ├── 1-djb2.c ├── 0-hash_table_create.c ├── 5-hash_table_print.c ├── 6-hash_table_delete.c ├── 4-hash_table_get.c ├── 3-hash_table_set.c ├── hash_tables.h └── 100-sorted_hash_table.c ├── 0x1E-search_algorithms ├── 107-O ├── 2-O ├── 3-O ├── 5-O ├── 6-O ├── 101-O ├── 108-O ├── 4-O ├── .5-O.swp ├── README.md ├── test_file │ └── 0-linear ├── test │ └── 0-main.c ├── 0-linear.c ├── 100-jump.c ├── 102-interpolation.c ├── 1-binary.c ├── 106-linear_skip.c ├── 105-jump_list.c ├── search_algos.h ├── 104-advanced_binary.c └── 103-exponential.c ├── 0x14-bit_manipulation ├── 101-password ├── 1-print_binary.c ├── 100-get_endianness.c ├── README.md ├── 3-set_bit.c ├── 4-clear_bit.c ├── 2-get_bit.c ├── 5-flip_bits.c ├── main.h └── 0-binary_to_uint.c ├── README.md ├── 0x08-recursion ├── README.md ├── main.c ├── 2-strlen_recursion.c ├── 1-print_rev_recursion.c ├── 3-factorial.c ├── 0-puts_recursion.c ├── 4-pow_recursion.c ├── main.h ├── 5-sqrt_recursion.c ├── 6-is_prime_number.c ├── 101-wildcmp.c └── 100-is_palindrome.c ├── 0x17-doubly_linked_lists ├── 102-result ├── 100-password ├── README.md ├── 1-dlistint_len.c ├── 4-free_dlistint.c ├── 0-print_dlistint.c ├── 6-sum_dlistint.c ├── 5-get_dnodeint.c ├── 2-add_dnodeint.c ├── 3-add_dnodeint_end.c ├── 8-delete_dnodeint.c ├── lists.h ├── 7-insert_dnodeint.c └── 103-keygen.c ├── 0x0A-argc_argv ├── README.md ├── 0-whatsmyname.c ├── 1-args.c ├── 2-args.c ├── 3-mul.c ├── 4-add.c └── 100-change.c ├── 0x0B-malloc_free ├── README.md ├── main.h ├── 4-free_grid.c ├── 0-create_array.c ├── 3-alloc_grid.c ├── 2-str_concat.c ├── 100-argstostr.c ├── 1-strdup.c ├── main.c └── 101-strtow.c ├── 0x00-hello_world ├── README.md ├── 0-preprocessor ├── 3-name ├── 1-compiler ├── 2-assembler ├── 100-intel ├── 4-puts.c ├── 5-printf.c ├── 101-quote.c └── 6-size.c ├── 0x07-pointers_arrays_strings ├── 101-crackme_password ├── README.md ├── 100-set_string.c ├── 2-strchr.c ├── 7-print_chessboard.c ├── 4-strpbrk.c ├── 1-memcpy.c ├── 8-print_diagsums.c ├── 0-memset.c ├── 5-strstr.c ├── main.h ├── 3-strspn.c └── main.c ├── 0x09-static_libraries ├── README.md ├── liball.a ├── 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 ├── 0-isupper.o ├── 1-isdigit.o ├── 1-strncat.o ├── 2-strncpy.o ├── 3-islower.o ├── 4-isalpha.o ├── 4-strpbrk.o ├── libschool.a ├── create_static_lib.sh ├── 6-abs.c ├── 3-islower.c ├── 0-isupper.c ├── 4-isalpha.c ├── 3-puts.c ├── _putchar.c ├── 2-strchr.c ├── 2-strlen.c ├── 1-isdigit.c ├── 9-strcpy.c ├── 3-strcmp.c ├── 4-strpbrk.c ├── 0-memset.c ├── 1-memcpy.c ├── 0-strcat.c ├── 5-strstr.c ├── 3-strspn.c ├── 2-strncpy.c ├── 1-strncat.c ├── 100-atoi.c └── main.h ├── 0x0D-preprocessor ├── README.md ├── 1-pi.h ├── 0-object_like_macro.h ├── 4-sum.h ├── main.h ├── 3-function_like_macro.h └── 2-main.c ├── 0x0C-more_malloc_free ├── README.md ├── main.h ├── 0-malloc_checked.c ├── 3-array_range.c ├── 2-calloc.c ├── 1-string_nconcat.c ├── 100-realloc.c └── 101-mul.c ├── 0x12-singly_linked_lists ├── README.md ├── 101-hello_holberton.asm ├── 100-first.c ├── 4-free_list.c ├── 1-list_len.c ├── 0-print_list.c ├── 2-add_node.c ├── lists.h └── 3-add_node_end.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 ├── function_pointers.h ├── 0-print_name.c ├── 3-calc.h ├── 1-array_iterator.c ├── 2-int_index.c ├── 3-get_op_func.c ├── 3-main.c ├── 100-main_opcodes.c └── 3-op_functions.c ├── 0x10-variadic_functions ├── README.md ├── variadic_functions.h ├── 0-sum_them_all.c ├── 1-print_numbers.c ├── 2-print_strings.c └── 3-print_all.c ├── 0x18-dynamic_libraries ├── README.md ├── 1-create_dynamic_lib.sh ├── libdynamic.so ├── 100-tests.py └── main.h ├── 0x02-functions_nested_loops ├── README.md ├── 6-abs.c ├── 10-add.c ├── 3-islower.c ├── 4-isalpha.c ├── 1-alphabet.c ├── 0-putchar.c ├── 2-print_alphabet_x10.c ├── 11-print_to_98.c ├── 5-sign.c ├── 7-print_last_digit.c ├── main.h ├── 102-fibonacci.c ├── 103-fibonacci.c ├── 101-natural.c ├── 9-times_table.c ├── 104-fibonacci.c ├── 8-24_hours.c └── 100-times_table.c ├── 0x03-debugging ├── README.md ├── 0-main.c ├── main.h ├── 1-main.c ├── 2-largest_number.c └── 3-print_remaining_days.c ├── 0x05-pointers_arrays_strings ├── README.md ├── 0-reset_to_98.c ├── 1-swap.c ├── main.c ├── 3-puts.c ├── 2-strlen.c ├── 4-print_rev.c ├── main.h ├── 9-strcpy.c ├── 6-puts2.c ├── 101-keygen.c ├── 5-rev_string.c ├── 8-print_array.c ├── 7-puts_half.c └── 100-atoi.c ├── 0x01-variables_if_else_while ├── README.md ├── .2-print_alphabet.c.swo ├── .2-print_alphabet.c.swp ├── 2-print_alphabet.c ├── 5-print_numbers.c ├── 7-print_tebahpla.c ├── 6-print_numberz.c ├── 9-print_comb.c ├── 3-print_alphabets.c ├── 4-print_alphabt.c ├── 8-print_base16.c ├── 0-positive_or_negative.c ├── 100-print_comb3.c ├── 1-last_digit.c ├── 101-print_comb4.c └── 102-print_comb5.c ├── 0x06-pointers_arrays_strings ├── README.md ├── .README.md.swp ├── 101-print_number.c ├── 3-strcmp.c ├── 4-rev_array.c ├── 5-string_toupper.c ├── 0-strcat.c ├── 7-leet.c ├── 1-strncat.c ├── 2-strncpy.c ├── 102-magic.c ├── main.h ├── 100-rot13.c ├── 6-cap_string.c ├── 104-print_buffer.c └── 103-infinite_add.c ├── 0x04-more_functions_nested_loops ├── README.md ├── 2-mul.c ├── 0-isupper.c ├── 3-print_numbers.c ├── 100-prime_factor.c ├── 1-isdigit.c ├── main.h ├── 6-print_line.c ├── 4-print_most_numbers.c ├── 5-more_numbers.c ├── 7-print_diagonal.c ├── 9-fizz_buzz.c ├── 8-print_square.c ├── 101-print_number.c └── 10-print_triangle.c ├── 0x1C-makefiles ├── 0-Makefile ├── 1-Makefile ├── .m.h.swp ├── 2-Makefile ├── README.md ├── m.h ├── __pycache__ │ └── 5-island_perimeter.cpython-38.pyc ├── main.c ├── 3-Makefile ├── 4-Makefile ├── 100-Makefile ├── 5-main.py ├── 5-island_perimeter.py └── school.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 ├── README.md ├── 102-free_listint_safe.c ├── 101-print_listint_safe.c └── lists.h └── 0x15-file_io ├── main.h ├── README.md ├── 0-read_textfile.c ├── 1-create_file.c ├── 2-append_text_to_file.c ├── 3-cp.c └── 100-elf_header.c /0x1A-hash_tables/main.c: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # alx-low_level_programming 2 | -------------------------------------------------------------------------------- /0x08-recursion/README.md: -------------------------------------------------------------------------------- 1 | 0x08. C - Recursion 2 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/102-result: -------------------------------------------------------------------------------- 1 | 906609 2 | -------------------------------------------------------------------------------- /0x0A-argc_argv/README.md: -------------------------------------------------------------------------------- 1 | 0x0A. C - argc, argv 2 | -------------------------------------------------------------------------------- /0x0B-malloc_free/README.md: -------------------------------------------------------------------------------- 1 | 0x0B. C - malloc, free 2 | -------------------------------------------------------------------------------- /0x00-hello_world/README.md: -------------------------------------------------------------------------------- 1 | #ALX Low-Level Programing 2 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/101-crackme_password: -------------------------------------------------------------------------------- 1 | abc123 -------------------------------------------------------------------------------- /0x09-static_libraries/README.md: -------------------------------------------------------------------------------- 1 | C - Static libraries 2 | -------------------------------------------------------------------------------- /0x09-static_libraries/liball.a: -------------------------------------------------------------------------------- 1 | $ ar -rc libname.a *.o 2 | -------------------------------------------------------------------------------- /0x0D-preprocessor/README.md: -------------------------------------------------------------------------------- 1 | 0x0D. C - Preprocessor 2 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/100-password: -------------------------------------------------------------------------------- 1 | en C Pyfo neZ 2 | -------------------------------------------------------------------------------- /0x1A-hash_tables/README.md: -------------------------------------------------------------------------------- 1 | # 0x1A. C - Hash tables 2 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/README.md: -------------------------------------------------------------------------------- 1 | 0x0C. C - More malloc, free 2 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/README.md: -------------------------------------------------------------------------------- 1 | C - Singly linked lists 2 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/README.md: -------------------------------------------------------------------------------- 1 | 0x0E. C - Structures, typedef 2 | -------------------------------------------------------------------------------- /0x0F-function_pointers/README.md: -------------------------------------------------------------------------------- 1 | 0x0F. C - Function pointers 2 | -------------------------------------------------------------------------------- /0x10-variadic_functions/README.md: -------------------------------------------------------------------------------- 1 | 0x10. C - Variadic functions 2 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/README.md: -------------------------------------------------------------------------------- 1 | # 0x18. C - Dynamic libraries 2 | -------------------------------------------------------------------------------- /0x00-hello_world/0-preprocessor: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -E $CFILE -o ./c 3 | -------------------------------------------------------------------------------- /0x00-hello_world/3-name: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -c $CFILE -o ./cisfun 3 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/README.md: -------------------------------------------------------------------------------- 1 | # 0x17. C - Doubly linked lists 2 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/README.md: -------------------------------------------------------------------------------- 1 | #0x02. C - Functions, nested loops 2 | -------------------------------------------------------------------------------- /0x03-debugging/README.md: -------------------------------------------------------------------------------- 1 | #0x03. C - Debugging 2 | #0x03. C - Debugging 3 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | #C - Pointers, arrays and strings 2 | -------------------------------------------------------------------------------- /0x00-hello_world/1-compiler: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -c $CFILE -o ./${CFILE//.c/.o} 3 | -------------------------------------------------------------------------------- /0x00-hello_world/2-assembler: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -S $CFILE -o ./${CFILE//.c/.s} 3 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/README.md: -------------------------------------------------------------------------------- 1 | 0x01. C - Variables, if, else, while 2 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | C - More pointers, arrays and strings 2 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/README.md: -------------------------------------------------------------------------------- 1 | #C - More functions, more nested loops 2 | -------------------------------------------------------------------------------- /0x1C-makefiles/0-Makefile: -------------------------------------------------------------------------------- 1 | all: main.c school.c 2 | gcc main.c school.c -o school 3 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | 0x07. C - Even more pointers, arrays and strings 2 | -------------------------------------------------------------------------------- /0x00-hello_world/100-intel: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -S -masm=intel $CFILE -o ${CFILE//'.c'/'.s'} 3 | -------------------------------------------------------------------------------- /0x1C-makefiles/1-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | 4 | all: $(SRC) 5 | $(CC) $(SRC) -o school 6 | -------------------------------------------------------------------------------- /0x0D-preprocessor/1-pi.h: -------------------------------------------------------------------------------- 1 | #ifndef MACRO_H 2 | #define MACRO_H 3 | 4 | #define PI 3.14159265359 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /0x0D-preprocessor/0-object_like_macro.h: -------------------------------------------------------------------------------- 1 | #ifndef MACRO_H 2 | #define MACRO_H 3 | 4 | #define SIZE 1024 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /0x1C-makefiles/.m.h.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FelicityTech/alx-low_level_programming/HEAD/0x1C-makefiles/.m.h.swp -------------------------------------------------------------------------------- /0x0D-preprocessor/4-sum.h: -------------------------------------------------------------------------------- 1 | #ifndef LIKEMACRO_H 2 | #define LIKEMACRO_H 3 | 4 | #define SUM(x, y) (x + y) 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /0x09-static_libraries/6-abs.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FelicityTech/alx-low_level_programming/HEAD/0x09-static_libraries/6-abs.o -------------------------------------------------------------------------------- /0x09-static_libraries/libmy.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FelicityTech/alx-low_level_programming/HEAD/0x09-static_libraries/libmy.a -------------------------------------------------------------------------------- /0x0D-preprocessor/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | int _putchar(char c); 5 | 6 | #endif /*BRUCE OYUGI NYAINDA*/ 7 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/1-create_dynamic_lib.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc *.o -fPIC -shared -o liball.so 3 | LD_PRELOAD=$WPD/liball.so 4 | -------------------------------------------------------------------------------- /0x09-static_libraries/0-memset.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FelicityTech/alx-low_level_programming/HEAD/0x09-static_libraries/0-memset.o -------------------------------------------------------------------------------- /0x09-static_libraries/0-strcat.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FelicityTech/alx-low_level_programming/HEAD/0x09-static_libraries/0-strcat.o -------------------------------------------------------------------------------- /0x09-static_libraries/1-memcpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FelicityTech/alx-low_level_programming/HEAD/0x09-static_libraries/1-memcpy.o -------------------------------------------------------------------------------- /0x09-static_libraries/100-atoi.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FelicityTech/alx-low_level_programming/HEAD/0x09-static_libraries/100-atoi.o -------------------------------------------------------------------------------- /0x09-static_libraries/2-strchr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FelicityTech/alx-low_level_programming/HEAD/0x09-static_libraries/2-strchr.o -------------------------------------------------------------------------------- /0x09-static_libraries/2-strlen.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FelicityTech/alx-low_level_programming/HEAD/0x09-static_libraries/2-strlen.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-puts.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FelicityTech/alx-low_level_programming/HEAD/0x09-static_libraries/3-puts.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-strcmp.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FelicityTech/alx-low_level_programming/HEAD/0x09-static_libraries/3-strcmp.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-strspn.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FelicityTech/alx-low_level_programming/HEAD/0x09-static_libraries/3-strspn.o -------------------------------------------------------------------------------- /0x09-static_libraries/5-strstr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FelicityTech/alx-low_level_programming/HEAD/0x09-static_libraries/5-strstr.o -------------------------------------------------------------------------------- /0x09-static_libraries/9-strcpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FelicityTech/alx-low_level_programming/HEAD/0x09-static_libraries/9-strcpy.o -------------------------------------------------------------------------------- /0x09-static_libraries/_putchar.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FelicityTech/alx-low_level_programming/HEAD/0x09-static_libraries/_putchar.o -------------------------------------------------------------------------------- /0x1E-search_algorithms/.5-O.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FelicityTech/alx-low_level_programming/HEAD/0x1E-search_algorithms/.5-O.swp -------------------------------------------------------------------------------- /0x09-static_libraries/0-isupper.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FelicityTech/alx-low_level_programming/HEAD/0x09-static_libraries/0-isupper.o -------------------------------------------------------------------------------- /0x09-static_libraries/1-isdigit.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FelicityTech/alx-low_level_programming/HEAD/0x09-static_libraries/1-isdigit.o -------------------------------------------------------------------------------- /0x09-static_libraries/1-strncat.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FelicityTech/alx-low_level_programming/HEAD/0x09-static_libraries/1-strncat.o -------------------------------------------------------------------------------- /0x09-static_libraries/2-strncpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FelicityTech/alx-low_level_programming/HEAD/0x09-static_libraries/2-strncpy.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-islower.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FelicityTech/alx-low_level_programming/HEAD/0x09-static_libraries/3-islower.o -------------------------------------------------------------------------------- /0x09-static_libraries/4-isalpha.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FelicityTech/alx-low_level_programming/HEAD/0x09-static_libraries/4-isalpha.o -------------------------------------------------------------------------------- /0x09-static_libraries/4-strpbrk.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FelicityTech/alx-low_level_programming/HEAD/0x09-static_libraries/4-strpbrk.o -------------------------------------------------------------------------------- /0x09-static_libraries/libschool.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FelicityTech/alx-low_level_programming/HEAD/0x09-static_libraries/libschool.a -------------------------------------------------------------------------------- /0x18-dynamic_libraries/libdynamic.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FelicityTech/alx-low_level_programming/HEAD/0x18-dynamic_libraries/libdynamic.so -------------------------------------------------------------------------------- /0x09-static_libraries/create_static_lib.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -Wall -pedantic -Werror -Wextra -c *.c 3 | ar -rc liball.a *.o 4 | ranlib liball.a 5 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/README.md: -------------------------------------------------------------------------------- 1 | # C - Search Algorithms 2 | 3 | ## C Algorithm and data sructure 4 | 5 | ### comming back to update this file. 6 | -------------------------------------------------------------------------------- /0x0D-preprocessor/3-function_like_macro.h: -------------------------------------------------------------------------------- 1 | #ifndef LIKEMACRO_H 2 | #define LIKEMACRO_H 3 | 4 | #define ABS(X) ((X) < 0 ? -(1) * (X) : (X)) 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /0x1C-makefiles/2-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | OBJ = $(SRC:.c=.o) 4 | NAME = school 5 | 6 | all: $(OBJ) 7 | $(CC) $(OBJ) -o $(NAME) 8 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/test_file/0-linear: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FelicityTech/alx-low_level_programming/HEAD/0x1E-search_algorithms/test_file/0-linear -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/.README.md.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FelicityTech/alx-low_level_programming/HEAD/0x06-pointers_arrays_strings/.README.md.swp -------------------------------------------------------------------------------- /0x1C-makefiles/README.md: -------------------------------------------------------------------------------- 1 | # 0x1C. C - Makefiles 2 | 3 | This Project is an important tools in C. it makes compilation easy with low effort with maxima results. 4 | 5 | -------------------------------------------------------------------------------- /0x1C-makefiles/m.h: -------------------------------------------------------------------------------- 1 | #ifndef __M_H__ 2 | #define __M_H__ 3 | 4 | #include 5 | #include 6 | 7 | void print_school(void); 8 | 9 | #endif 10 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/.2-print_alphabet.c.swo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FelicityTech/alx-low_level_programming/HEAD/0x01-variables_if_else_while/.2-print_alphabet.c.swo -------------------------------------------------------------------------------- /0x01-variables_if_else_while/.2-print_alphabet.c.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FelicityTech/alx-low_level_programming/HEAD/0x01-variables_if_else_while/.2-print_alphabet.c.swp -------------------------------------------------------------------------------- /0x1C-makefiles/__pycache__/5-island_perimeter.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FelicityTech/alx-low_level_programming/HEAD/0x1C-makefiles/__pycache__/5-island_perimeter.cpython-38.pyc -------------------------------------------------------------------------------- /0x1C-makefiles/main.c: -------------------------------------------------------------------------------- 1 | #include "m.h" 2 | 3 | /* 4 | * main - Entry point 5 | * 6 | * Return: Always 7 | */ 8 | int main(void) 9 | { 10 | print_school(); 11 | return(EXIT_SUCCESS); 12 | } 13 | -------------------------------------------------------------------------------- /0x08-recursion/main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - check the code 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | _puts_recursion("Puts with recursion"); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x09-static_libraries/6-abs.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _abs - return abs value 5 | * @n : number to check 6 | * Return:0 or 1 7 | */ 8 | 9 | int _abs(int n) 10 | { 11 | 12 | return (n >= 0 ? n : n * -1); 13 | 14 | } 15 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/6-abs.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _abs - return abs value 5 | * @n : number to check 6 | * Return:0 or 1 7 | */ 8 | 9 | int _abs(int n) 10 | { 11 | 12 | return (n >= 0 ? n : n * -1); 13 | 14 | } 15 | -------------------------------------------------------------------------------- /0x00-hello_world/4-puts.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Entry point 5 | * 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | puts("\"Programming is like building a multilingual puzzle"); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x00-hello_world/5-printf.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - Entry point 4 | * 5 | * Return: Always 0 (Success) 6 | */ 7 | int main(void) 8 | { 9 | printf("with proper grammar, but the outcome is a piece of art,\n"); 10 | return (0); 11 | } 12 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/10-add.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * add - returns the sum of its parameters 4 | * @a: int type number 5 | * @b: int type number 6 | * Return:0 7 | */ 8 | 9 | 10 | int add(int a, int b) 11 | { 12 | return (a + b); 13 | } 14 | -------------------------------------------------------------------------------- /0x03-debugging/0-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - Test function for 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 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/0-reset_to_98.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * reset_to_98 - updates the value it points 4 | *@n: pointer of the direcction at the variable n 5 | *Return: 0 (success) 6 | */ 7 | void reset_to_98(int *n) 8 | { 9 | *n = 98; 10 | } 11 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-islower.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _islower - check for lower case letter 5 | * @c : character to check the case 6 | * Return:0 or 1 7 | */ 8 | 9 | int _islower(int c) 10 | { 11 | return (c >= 97 && c <= 122); 12 | 13 | } 14 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/3-islower.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _islower - check for lower case letter 5 | * @c : character to check the case 6 | * Return:0 or 1 7 | */ 8 | 9 | int _islower(int c) 10 | { 11 | return (c >= 97 && c <= 122); 12 | 13 | } 14 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/100-set_string.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * set_string - sets the value of a pointer to a char 5 | * @s: pointer to pointer 6 | * @to: pointer char 7 | */ 8 | 9 | void set_string(char **s, char *to) 10 | { 11 | *s = to; 12 | } 13 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/1-swap.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * swap_int - swaps the values 4 | *@a: pointer to a 5 | *@b: ponter to b 6 | *Return: Nothing 7 | */ 8 | void swap_int(int *a, int *b) 9 | { 10 | int aux; 11 | 12 | aux = *a; 13 | *a = *b; 14 | *b = aux; 15 | } 16 | -------------------------------------------------------------------------------- /0x09-static_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 | -------------------------------------------------------------------------------- /0x09-static_libraries/4-isalpha.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isalpha - check for lower case letter 5 | * @c : character to check 6 | * Return:0 or 1 7 | */ 8 | 9 | int _isalpha(int c) 10 | { 11 | return ((c >= 97 && c <= 122) || (c >= 65 && c <= 90)); 12 | 13 | } 14 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/4-isalpha.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isalpha - check for lower case letter 5 | * @c : character to check 6 | * Return:0 or 1 7 | */ 8 | 9 | int _isalpha(int c) 10 | { 11 | return ((c >= 97 && c <= 122) || (c >= 65 && c <= 90)); 12 | 13 | } 14 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/2-mul.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *mul - multiplies two ints. 4 | *@a: integer. 5 | *@b: integer. 6 | * 7 | *Return: product. 8 | */ 9 | int mul(int a, int b) 10 | { 11 | int product; 12 | 13 | product = a * b; 14 | return (product); 15 | } 16 | -------------------------------------------------------------------------------- /0x0F-function_pointers/function_pointers.h: -------------------------------------------------------------------------------- 1 | #ifndef _FUNCTION_POINTERS_H_ 2 | #define _FUNCTION_POINTERS_H_ 3 | void print_name(char *name, void (*f)(char *)); 4 | void array_iterator(int *array, size_t size, void (*action)(int)); 5 | int int_index(int *array, int size, int (*cmp)(int)); 6 | #endif 7 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/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 | -------------------------------------------------------------------------------- /0x00-hello_world/101-quote.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * main - Entry point 6 | * 7 | * Return: Always 0 (Success) 8 | */ 9 | int main(void) 10 | { 11 | write(1, "and that piece of art is useful\" - Dora Korpar, 2015-10-19\n", 59); 12 | return (1); 13 | } 14 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/101-hello_holberton.asm: -------------------------------------------------------------------------------- 1 | SECTION .data 2 | msg: db "Hello, Holberton", 0 3 | fmt: db "%s", 10, 0 4 | 5 | SECTION .text 6 | extern printf 7 | global main 8 | main: 9 | mov esi, msg 10 | mov edi, fmt 11 | mov eax, 0 12 | call printf 13 | 14 | mov eax, 0 15 | ret 16 | -------------------------------------------------------------------------------- /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 | clean: 10 | $(RM) *~ $(NAME) 11 | 12 | oclean: 13 | $(RM) $(OBJ) 14 | 15 | fclean: clean oclean 16 | 17 | re: fclean all 18 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/1-alphabet.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_alphabet - Entry point 5 | * Return:0 6 | */ 7 | 8 | void print_alphabet(void) 9 | { 10 | char c = 'a'; 11 | 12 | while (c <= 'z') 13 | { 14 | _putchar(c); 15 | c++; 16 | 17 | } 18 | _putchar('\n'); 19 | 20 | } 21 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/3-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_numbers - prints numbers 0-9 5 | * 6 | * Return: void 7 | */ 8 | 9 | void print_numbers(void) 10 | { 11 | int c; 12 | 13 | for (c = '0'; c <= '9'; c++) 14 | { 15 | _putchar(c); 16 | } 17 | _putchar('\n'); 18 | } 19 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /* 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int n; 12 | 13 | n = 402; 14 | printf("n=%d\n", n); 15 | reset_to_98(&n); 16 | printf("n=%d\n", n); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/100-first.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * bmain - function executed before main 5 | * Return: no return. 6 | */ 7 | 8 | void __attribute__ ((constructor)) bmain() 9 | { 10 | printf("You're beat! and yet, you must allow"); 11 | printf(",\nI bore my house upon my back!\n"); 12 | } 13 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-puts.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _puts - print a string followed by a new line 4 | *@str: string to print 5 | *Return: Nothing 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 | -------------------------------------------------------------------------------- /0x0D-preprocessor/2-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - Write a program that prints the name of the file it was compiled 6 | * from, followed by a new line. 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(void) 11 | { 12 | printf("%s\n", __FILE__); 13 | return (0); 14 | } 15 | -------------------------------------------------------------------------------- /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 | void print_remaining_days(int month, int day, int year); 9 | int convert_day(int month, int day); 10 | void print_times_table(int n); 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /0x08-recursion/2-strlen_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strlen_recursion - main - check the code 5 | * @s: character of string 6 | * Return: Always 0. 7 | */ 8 | int _strlen_recursion(char *s) 9 | { 10 | if (*s != '\0') 11 | { 12 | return (1 + _strlen_recursion(s + 1)); 13 | } 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/3-puts.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _puts - print a string followed by a new line 4 | *@str: string to print 5 | *Return: Nothing 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 | -------------------------------------------------------------------------------- /0x08-recursion/1-print_rev_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _print_rev_recursion - main - check the code 5 | * @s: character of a string 6 | * Return: Always 0. 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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/1-print_binary.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_binary - Prints the binary representation of a number. 5 | * @n: The number to be printed in binary. 6 | */ 7 | void print_binary(unsigned long int n) 8 | { 9 | if (n > 1) 10 | print_binary(n >> 1); 11 | 12 | _putchar((n & 1) + '0'); 13 | } 14 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/5-free_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | 4 | /** 5 | * free_dog - function to free memory 6 | * @d: is pointer to dog_t 7 | * 8 | * Return: nothing. 9 | **/ 10 | 11 | void free_dog(dog_t *d) 12 | { 13 | if (d != NULL) 14 | { 15 | free(d->name); 16 | free(d->owner); 17 | free(d); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x1A-hash_tables/2-key_index.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * key_index - generates the key using hash_djb2 5 | * @key: pointer the key 6 | * @size: size table 7 | * Return: index 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 | -------------------------------------------------------------------------------- /0x09-static_libraries/2-strchr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strchr - Entry point 4 | * @s: input 5 | * @c: input 6 | * Return: Always 0 (Success) 7 | */ 8 | char *_strchr(char *s, char c) 9 | { 10 | int i = 0; 11 | 12 | for (; s[i] >= '\0'; i++) 13 | { 14 | if (s[i] == c) 15 | return (&s[i]); 16 | } 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x1C-makefiles/4-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | OBJ = $(SRC:.c=.o) 4 | NAME = school 5 | RM = rm -f 6 | CFLAGS = -Wall -Werror -Wextra -pedantic 7 | 8 | all: $(OBJ) 9 | $(CC) $(OBJ) -o $(NAME) 10 | 11 | clean: 12 | $(RM) *~ $(NAME) 13 | 14 | oclean: 15 | $(RM) $(OBJ) 16 | 17 | fclean: clean oclean 18 | 19 | re: fclean all 20 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/2-strchr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strchr - Entry point 4 | * @s: input 5 | * @c: input 6 | * Return: Always 0 (Success) 7 | */ 8 | char *_strchr(char *s, char c) 9 | { 10 | int i = 0; 11 | 12 | for (; s[i] >= '\0'; i++) 13 | { 14 | if (s[i] == c) 15 | return (&s[i]); 16 | } 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x08-recursion/3-factorial.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * factorial - main - check the code 5 | * @n: number of factorial 6 | * Return: Always 0. 7 | */ 8 | int factorial(int n) 9 | { 10 | if (n == 0) 11 | { 12 | return (1); 13 | } 14 | else if (n < 0) 15 | { 16 | return (-1); 17 | } 18 | return (n * factorial(n - 1)); 19 | } 20 | -------------------------------------------------------------------------------- /0x09-static_libraries/2-strlen.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * * _strlen - checks the length of a string 6 | * * @s:input 7 | * * Return: Always 0. 8 | */ 9 | int _strlen(char *s) 10 | { 11 | int length = 0; 12 | 13 | while (*s != 0) 14 | { 15 | length++; 16 | s++; 17 | } 18 | 19 | return (length); 20 | } 21 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/2-strlen.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * * _strlen - checks the length of a string 6 | * * @s:input 7 | * * Return: Always 0. 8 | */ 9 | int _strlen(char *s) 10 | { 11 | int length = 0; 12 | 13 | while (*s != 0) 14 | { 15 | length++; 16 | s++; 17 | } 18 | 19 | return (length); 20 | } 21 | -------------------------------------------------------------------------------- /0x08-recursion/0-puts_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _puts_recursion - main - check the code 5 | * @s: character of string 6 | * Return: Always 0. 7 | */ 8 | void _puts_recursion(char *s) 9 | { 10 | if (*(s) != '\0') 11 | { 12 | _putchar(*s); 13 | _puts_recursion(s + 1); 14 | } 15 | if (*(s) == '\0') 16 | _putchar('\n'); 17 | } 18 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/2-print_alphabet.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints the alphabet in lowercase, 5 | * followed by a new line 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | char ch; 11 | 12 | for (ch = 'a'; ch <= 'z'; ch++) 13 | { 14 | putchar(ch); 15 | } 16 | putchar('\n'); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/5-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints all single digit numbers of base 5 | * 10 starting from 0, followed by a new line 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | int n; 11 | 12 | for (n = 0; n < 10; n++) 13 | printf("%d", n); 14 | printf("\n"); 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/4-print_rev.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * print_rev - prints a string in reverse 4 | *@s: string to reverse 5 | *Return: Nothing 6 | */ 7 | void print_rev(char *s) 8 | { 9 | int l; 10 | 11 | l = 0; 12 | while (*(s + l) != '\0') 13 | l++; 14 | l--; 15 | for (; l >= 0; l--) 16 | _putchar(*(s + l)); 17 | _putchar('\n'); 18 | } 19 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/7-print_tebahpla.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints the lowercase alphabet in reverse, 5 | * followed by a new line 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | char ch; 11 | 12 | for (ch = 'z'; ch >= 'a'; ch--) 13 | { 14 | putchar(ch); 15 | } 16 | putchar('\n'); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/100-prime_factor.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Entry point 5 | * Return: Always 0 6 | */ 7 | int main(void) 8 | { 9 | unsigned long int i = 3, n = 612852475143; 10 | 11 | for (; i < 12057; i += 2) 12 | { 13 | while (n % i == 0 && n != i) 14 | n /= i; 15 | } 16 | printf("%lu\n", n); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | void reset_to_98(int *n); 2 | void swap_int(int *a, int *b); 3 | void _puts(char *str); 4 | void print_rev(char *s); 5 | void rev_string(char *s); 6 | void puts2(char *str); 7 | void puts_half(char *str); 8 | void print_array(int *a, int n); 9 | char *_strcpy(char *dest, char *src); 10 | int _atoi(char *s); 11 | int _putchar(char c); 12 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/100-get_endianness.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * get_endianness - Checks the endianness. 5 | * 6 | * Return: If big-endian - 0. 7 | * If little-endian - 1. 8 | */ 9 | int get_endianness(void) 10 | { 11 | int num = 1; 12 | char *endian = (char *)# 13 | 14 | if (*endian == 1) 15 | return (1); 16 | 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/1-isdigit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isdigit - checks if parameter is a number between 0 to 9. 5 | * @c: input number. 6 | * Return: 1 if is a number (0 to 9), 0 in other case. 7 | */ 8 | int _isdigit(int c) 9 | { 10 | if (c >= 48 && c <= 57) 11 | { 12 | return (1); 13 | } 14 | else 15 | { 16 | return (0); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /0x0A-argc_argv/0-whatsmyname.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - is a function that prints the name. 5 | *@argc: is an argument counter for char. 6 | *@argv: is an agument value for char. 7 | *Return: Returns always success. 8 | **/ 9 | 10 | int main(int argc, char *argv[]) 11 | { 12 | int i = argc - argc; 13 | 14 | printf("%s\n", argv[i]); 15 | 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/4-free_list.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_list - frees a list 5 | * @head: head of the linked list. 6 | * Return: no return. 7 | */ 8 | 9 | void free_list(list_t *head) 10 | { 11 | list_t *current; 12 | 13 | while ((current = head) != NULL) 14 | { 15 | head = head->next; 16 | free(current->str); 17 | free(current); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x1C-makefiles/100-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | OBJ = $(SRC:.c=.o) 4 | NAME = school 5 | CFLAGS = -Wall -Werror -Wextra -pedantic 6 | 7 | .PHONY: all clean oclean fclean re 8 | 9 | all: m.h $(OBJ) 10 | $(CC) $(OBJ) -o $(NAME) 11 | 12 | clean: 13 | $(RM) *~ $(NAME) 14 | 15 | oclean: 16 | $(RM) $(OBJ) 17 | 18 | fclean: clean oclean 19 | 20 | re: fclean all 21 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/main.h: -------------------------------------------------------------------------------- 1 | int _putchar(char c); 2 | int _isupper(int c); 3 | int _isdigit(int c); 4 | int mul(int a, int b); 5 | void print_numbers(void); 6 | void print_most_numbers(void); 7 | void more_numbers(void); 8 | void print_line(int n); 9 | void print_diagonal(int n); 10 | void print_square(int size); 11 | void print_triangle(int size); 12 | void print_number(int n); 13 | -------------------------------------------------------------------------------- /0x08-recursion/4-pow_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _pow_recursion - main - check the code 5 | * @x: number 6 | * @y: number of power 7 | * Return: Always 0. 8 | */ 9 | int _pow_recursion(int x, int y) 10 | { 11 | if (y < 0) 12 | { 13 | return (-1); 14 | } 15 | else if (y == 0) 16 | { 17 | return (1); 18 | } 19 | return (x * _pow_recursion(x, y - 1)); 20 | } 21 | -------------------------------------------------------------------------------- /0x0B-malloc_free/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAINN_H 3 | 4 | int _putchar(char c); 5 | char *create_array(unsigned int size, char c); 6 | char *_strdup(char *str); 7 | char *str_concat(char *s1, char *s2); 8 | int **alloc_grid(int width, int height); 9 | void free_grid(int **grid, int height); 10 | char *argstostr(int ac, char **av); 11 | char **strtow(char *str); 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/6-print_numberz.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints all single digit numbers of base 10 starting from 0, 5 | * followed by a new line, using putchar 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | int n; 11 | 12 | for (n = 48; n < 58; n++) 13 | { 14 | putchar(n); 15 | } 16 | putchar('\n'); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/0-putchar.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | 4 | /** 5 | * main - Entry point 6 | * 7 | * Return: nothing, return void 8 | */ 9 | 10 | int main(void) 11 | { 12 | _putchar('_'); 13 | _putchar('p'); 14 | _putchar('u'); 15 | _putchar('t'); 16 | _putchar('c'); 17 | _putchar('h'); 18 | _putchar('a'); 19 | _putchar('r'); 20 | _putchar('\n'); 21 | 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x0A-argc_argv/1-args.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints the for the program. 5 | * @argc: is a counter of argument for int. 6 | * @argv: is a counter for value of the argument to char 7 | * Return: Returns always success. 8 | **/ 9 | 10 | int main(int argc, char *argv[]) 11 | { 12 | (void)argv; 13 | 14 | printf("%d\n", (argc - 1)); 15 | 16 | return (0); 17 | } 18 | 19 | -------------------------------------------------------------------------------- /0x0F-function_pointers/0-print_name.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "function_pointers.h" 3 | 4 | /** 5 | * print_name - prints a name as is 6 | * @name: name of the person 7 | * @f: is pointer 8 | * Return: Nothing. 9 | **/ 10 | void print_name(char *name, void (*f)(char *)) 11 | { 12 | if (name == NULL || f == NULL) 13 | { 14 | return; 15 | } 16 | else 17 | { 18 | f(name); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/1-dlistint_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * dlistint_len - Count number of elements in the list 5 | * @h: Head of the linked list 6 | * 7 | * Return: Number of nodes (size_t) 8 | */ 9 | size_t dlistint_len(const dlistint_t *h) 10 | { 11 | size_t i = 0; 12 | 13 | while (h != NULL) 14 | { 15 | i++; 16 | h = h->next; 17 | } 18 | return (i); 19 | } 20 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/7-print_chessboard.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_chessboard - prints chessboard. 5 | * 6 | * @a: rows. 7 | * Return: nothing. 8 | */ 9 | void print_chessboard(char (*a)[8]) 10 | { 11 | int i, j; 12 | 13 | for (i = 0; i < 8; i++) 14 | { 15 | for (j = 0; j < 8; j++) 16 | { 17 | _putchar(a[i][j]); 18 | } 19 | _putchar('\n'); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /0x09-static_libraries/9-strcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * *_strcpy - copies the string pointed 4 | *@dest: copie of the string 5 | *@src: origin of the string 6 | *Return: dest destine copie 7 | */ 8 | char *_strcpy(char *dest, char *src) 9 | { 10 | int i; 11 | 12 | for (i = 0; src[i] != '\0'; i++) 13 | { 14 | dest[i] = src[i]; 15 | } 16 | dest[i] = '\0'; 17 | return (dest); 18 | } 19 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/9-strcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * *_strcpy - copies the string pointed 4 | *@dest: copie of the string 5 | *@src: origin of the string 6 | *Return: dest destine copie 7 | */ 8 | char *_strcpy(char *dest, char *src) 9 | { 10 | int i; 11 | 12 | for (i = 0; src[i] != '\0'; i++) 13 | { 14 | dest[i] = src[i]; 15 | } 16 | dest[i] = '\0'; 17 | return (dest); 18 | } 19 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/101-print_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * print_number - prints an integer 4 | * @n:.input integer parameter 5 | * 6 | * 7 | */ 8 | void print_number(int n) 9 | { 10 | unsigned int i = n; 11 | 12 | if (n < 0) 13 | { 14 | _putchar(45); 15 | i = -i; 16 | } 17 | if (i / 10) 18 | { 19 | print_number(i / 10); 20 | } 21 | _putchar(i % 10 + '0'); 22 | } 23 | -------------------------------------------------------------------------------- /0x0A-argc_argv/2-args.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints the function. 5 | * @argc: is argument counter to an int. 6 | * @argv: is an agument value determiner for char. 7 | * 8 | * Return: Returns always success. 9 | **/ 10 | 11 | int main(int argc, char *argv[]) 12 | { 13 | int i; 14 | for (i = 0; i < argc; i++) 15 | printf("%s\n", argv[i]); 16 | 17 | return (0); 18 | 19 | } 20 | -------------------------------------------------------------------------------- /0x15-file_io/main.h: -------------------------------------------------------------------------------- 1 | #ifndef _MAIN_ 2 | #define _MAIN_ 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | ssize_t read_textfile(const char *filename, size_t letters); 11 | int create_file(const char *filename, char *text_content); 12 | int append_text_to_file(const char *filename, char *text_content); 13 | 14 | #endif 15 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/6-print_line.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_line - a function that draws a straight line in the terminal. 5 | * followed by a new line. 6 | * @n: An input integer 7 | * Return: Always 0 8 | */ 9 | void print_line(int n) 10 | { 11 | int i = 0; 12 | 13 | if (n > 0) 14 | { 15 | for (; i < n; i++) 16 | _putchar('_'); 17 | } 18 | _putchar('\n'); 19 | } 20 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-strcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strcmp - compares two strings 4 | * @s1: input parameter 5 | * @s2: input parameter 6 | * 7 | * Return: 0 if equal or b if is different 8 | */ 9 | int _strcmp(char *s1, char *s2) 10 | { 11 | int a = 0; 12 | int b = 0; 13 | 14 | while (s1[a] != '\0' && b == 0) 15 | { 16 | b = s1[a] - s2[a]; 17 | a++; 18 | } 19 | return (b); 20 | } 21 | -------------------------------------------------------------------------------- /0x09-static_libraries/4-strpbrk.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strpbrk - Entry point 4 | * @s: input 5 | * @accept: input 6 | * Return: Always 0 (Success) 7 | */ 8 | char *_strpbrk(char *s, char *accept) 9 | { 10 | int k; 11 | 12 | while (*s) 13 | { 14 | for (k = 0; accept[k]; k++) 15 | { 16 | if (*s == accept[k]) 17 | return (s); 18 | } 19 | s++; 20 | } 21 | 22 | return ('\0'); 23 | } 24 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/6-puts2.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * puts2 - print one char out of 2 of a string 4 | *@str: string to print 5 | *Return: Nothing 6 | */ 7 | void puts2(char *str) 8 | { 9 | int len; 10 | char l; 11 | 12 | for (len = 0; *(str + len) != 0; len++) 13 | { 14 | if (len % 2 == 0) 15 | { 16 | l = *(str + len); 17 | _putchar(l); 18 | } 19 | } 20 | _putchar('\n'); 21 | } 22 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | int _putchar(char c); 5 | void *malloc_checked(unsigned int b); 6 | char *string_nconcat(char *s1, char *s2, unsigned int n); 7 | void *_calloc(unsigned int nmemb, unsigned int size); 8 | int *array_range(int min, int max); 9 | void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size); 10 | 11 | #endif /* BRUCE OYUGI NYAINDA*/ 12 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/README.md: -------------------------------------------------------------------------------- 1 | ## ALX -ENGINEERING 2 | ** ALL THE TASKS HERE ARE JUST TO MAKE YOU GHOST ** 3 | 4 | ## 0X14. C - BIT MANIPULATION 5 | The project contains the following files as a structure 6 | 7 | - 0-binary_to_unit 8 | - 1-print-binary 9 | - 2-get_bit 10 | - 3-set_bits 11 | - 4-clear_bit 12 | - 5 -flip_bits.c 13 | - 100-get_endianness 14 | - 101-password 15 | - main.h 16 | -README.md 17 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/4-free_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_dlistint - Free the list 5 | * @head: Head of the list 6 | * 7 | * Return: nothing 8 | */ 9 | void free_dlistint(dlistint_t *head) 10 | { 11 | dlistint_t *temp; 12 | 13 | if (head == NULL) 14 | return; 15 | 16 | while (head != NULL) 17 | { 18 | temp = head; 19 | head = temp->next; 20 | free(temp); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /0x1A-hash_tables/1-djb2.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_djb2 - hash function generates a djb2 5 | * @str: pointer to chars a hash 6 | * Return: hash key 7 | */ 8 | unsigned long int hash_djb2(const unsigned char *str) 9 | { 10 | unsigned long hash = 5381; 11 | int c; 12 | 13 | while ((c = *str++)) 14 | hash = ((hash << 5) + hash) + c; /* hash * 33 + c */ 15 | 16 | return (hash); 17 | } 18 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/2-print_alphabet_x10.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_alphabet_x10 - prints alphabet 10 times 5 | * Return:void 6 | */ 7 | 8 | void print_alphabet_x10(void) 9 | { 10 | char c; 11 | int i = 0; 12 | 13 | while (i < 10) 14 | { 15 | c = 'a'; 16 | while (c <= 'z') 17 | { 18 | _putchar(c); 19 | c++; 20 | } 21 | _putchar('\n'); 22 | i++; 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/3-strcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strcmp - compares two strings 4 | * @s1: input parameter 5 | * @s2: input parameter 6 | * 7 | * Return: 0 if equal or b if is different 8 | */ 9 | int _strcmp(char *s1, char *s2) 10 | { 11 | int a = 0; 12 | int b = 0; 13 | 14 | while (s1[a] != '\0' && b == 0) 15 | { 16 | b = s1[a] - s2[a]; 17 | a++; 18 | } 19 | return (b); 20 | } 21 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/4-strpbrk.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strpbrk - Entry point 4 | * @s: input 5 | * @accept: input 6 | * Return: Always 0 (Success) 7 | */ 8 | char *_strpbrk(char *s, char *accept) 9 | { 10 | int k; 11 | 12 | while (*s) 13 | { 14 | for (k = 0; accept[k]; k++) 15 | { 16 | if (*s == accept[k]) 17 | return (s); 18 | } 19 | s++; 20 | } 21 | 22 | return ('\0'); 23 | } 24 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/9-print_comb.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints all possible combinations of single-digit numbers 5 | * Return: Always 0 (Success) 6 | */ 7 | int main(void) 8 | { 9 | int n; 10 | 11 | for (n = 48; n < 58; n++) 12 | { 13 | putchar(n); 14 | if (n != 57) 15 | { 16 | putchar(','); 17 | putchar(' '); 18 | } 19 | } 20 | putchar('\n'); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/11-print_to_98.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * print_to_98 - print to 98 5 | * @n : number to start from 6 | * Return:0 or 1 7 | */ 8 | 9 | void print_to_98(int n) 10 | { 11 | 12 | while (n < 98) 13 | { 14 | printf("%i, ", n); 15 | n++; 16 | } 17 | while (n > 98) 18 | { 19 | printf("%i, ", n); 20 | n--; 21 | } 22 | 23 | printf("98"); 24 | putchar('\n'); 25 | } 26 | -------------------------------------------------------------------------------- /0x08-recursion/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | int _putchar(char c); 5 | void _puts_recursion(char *s); 6 | void _print_rev_recursion(char *s); 7 | int _strlen_recursion(char *s); 8 | int factorial(int n); 9 | int _pow_recursion(int x, int y); 10 | int _sqrt_recursion(int n); 11 | int is_prime_number(int n); 12 | int is_palindrome(char *s); 13 | int wildcmp(char *s1, char *s2); 14 | 15 | #endif /*MAIN_H*/ 16 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/1-list_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * list_len - Calculate the number of elements. 5 | * @h: Pointer to a list. 6 | * Return: Integer. 7 | **/ 8 | 9 | size_t list_len(const list_t *h) 10 | { 11 | const list_t *temp; 12 | unsigned int counter = 0; 13 | 14 | temp = h; 15 | while (temp) 16 | { 17 | counter++; 18 | temp = temp->next; 19 | } 20 | return (counter); 21 | } 22 | 23 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/3-print_alphabets.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints the alphabet in lowercase, and then in uppercase, 5 | * followed by a new line 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | int ch; 11 | 12 | for (ch = 'a'; ch <= 'z'; ch++) 13 | putchar(ch); 14 | for (ch = 'A'; ch <= 'Z'; ch++) 15 | putchar(ch); 16 | putchar('\n'); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/0-malloc_checked.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * malloc_checked - allocates memory using malloc 6 | * @b: memory to allocate 7 | * 8 | * Return: pointer to allocated memory or normal process termination if error 9 | */ 10 | void *malloc_checked(unsigned int b) 11 | { 12 | void *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 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/4-print_alphabt.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints the alphabet in lowercase, 5 | * followed by a new line, except q and e 6 | * Return: Aways 0 (Sucess) 7 | */ 8 | int main(void) 9 | { 10 | char ch = 'a'; 11 | 12 | while (ch <= 'z') 13 | { 14 | if (ch != 'e' && ch != 'q') 15 | { 16 | putchar(ch); 17 | } 18 | ch++; 19 | } 20 | putchar('\n'); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/4-rev_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * reverse_array - reverses an array 4 | * @a: an array of integers 5 | * @n: number of elements of an array 6 | * 7 | */ 8 | void reverse_array(int *a, int n) 9 | { 10 | int i; 11 | int temp; 12 | 13 | i = 0; 14 | n = n - 1; 15 | 16 | while (i < n) 17 | { 18 | temp = a[i]; 19 | a[i] = a[n]; 20 | a[n] = temp; 21 | i++; 22 | n--; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /0x1C-makefiles/5-main.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | """ 3 | 5-main 4 | """ 5 | island_perimeter = __import__('5-island_perimeter').island_perimeter 6 | 7 | if __name__ == "__main__": 8 | grid = [ 9 | [0, 0, 0, 0, 0, 0], 10 | [0, 1, 0, 0, 0, 0], 11 | [0, 1, 0, 0, 0, 0], 12 | [0, 1, 1, 1, 0, 0], 13 | [0, 0, 0, 0, 0, 0] 14 | ] 15 | 16 | print(island_perimeter(grid)) 17 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/4-print_most_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_most_numbers - a function that prints the numbers, from 0 to 9. 5 | * Do not print 2 and 4. 6 | * followed by a new line. 7 | * Return: Always 0 8 | */ 9 | void print_most_numbers(void) 10 | { 11 | int i = '0'; 12 | 13 | for (; i <= '9'; i++) 14 | { 15 | if (i != '2' && i != '4') 16 | _putchar(i); 17 | } 18 | _putchar('\n'); 19 | } 20 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/101-keygen.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /** 6 | * main - generates random passwords for 101-crackme 7 | * Return: zero 8 | */ 9 | int main(void) 10 | { 11 | int sum; 12 | char c; 13 | 14 | srand(time(NULL)); 15 | while (sum <= 2645) 16 | { 17 | c = rand() % 128; 18 | sum += c; 19 | putchar(c); 20 | } 21 | putchar(2772 - sum); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/0-print_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * print_dlistint - Prints all the elements of a double linked list 5 | * @h: Head of the linked list 6 | * 7 | * Return: Number of nodes (size_t) 8 | */ 9 | size_t print_dlistint(const dlistint_t *h) 10 | { 11 | size_t i = 0; 12 | 13 | while (h != NULL) 14 | { 15 | printf("%d\n", h->n); 16 | h = h->next; 17 | i++; 18 | } 19 | 20 | return (i); 21 | } 22 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/5-sign.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_sign - check numers 5 | * @n : number to check 6 | * Return:0 or 1 7 | */ 8 | 9 | int print_sign(int n) 10 | { 11 | int test; 12 | 13 | if (n > 0) 14 | { 15 | test = 1; 16 | _putchar('+'); 17 | } 18 | else if (n == 0) 19 | { 20 | test = 0; 21 | _putchar('0'); 22 | } 23 | else 24 | { 25 | test = -1; 26 | _putchar('-'); 27 | } 28 | 29 | return (test); 30 | } 31 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/7-print_last_digit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_last_digit - prints the last digit of a number 5 | * @n: n is an integer 6 | * 7 | * Description: prints the last digit of a number 8 | * 9 | * Return: integer 10 | */ 11 | 12 | int print_last_digit(int n) 13 | { 14 | int last = n % 10; 15 | 16 | if (n < 0) 17 | last = last * -1; 18 | 19 | _putchar(last + '0'); 20 | 21 | return (last); 22 | } 23 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/5-rev_string.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * rev_string - reverse a string 4 | *@s: string to reverse 5 | *Return: Nothing 6 | */ 7 | void rev_string(char *s) 8 | { 9 | int len, i; 10 | char l; 11 | 12 | len = 0; 13 | while (*(s + len) != '\0') 14 | len++; 15 | len--; 16 | for (i = 0; i < len; i++) 17 | { 18 | l = *(s + len); 19 | *(s + len) = *(s + i); 20 | *(s + i) = l; 21 | len--; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/8-print_base16.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints all the numbers of base 16 in lowercase, 5 | * followed by a new line 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | int n; 11 | char ch; 12 | 13 | for (n = 48; n < 58; n++) 14 | { 15 | putchar(n); 16 | } 17 | for (ch = 'a'; ch <= 'f'; ch++) 18 | { 19 | putchar(ch); 20 | } 21 | putchar('\n'); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/5-more_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * more_numbers - prints numbers between 0 to 14 5 | * 10 times. 6 | * Return: no return. 7 | */ 8 | void more_numbers(void) 9 | { 10 | int i, ch; 11 | 12 | for (i = 0; i < 10; i++) 13 | { 14 | for (ch = 0; ch < 15; ch++) 15 | { 16 | if (ch >= 10) 17 | _putchar((ch / 10) + 48); 18 | _putchar((ch % 10) + 48); 19 | } 20 | _putchar('\n'); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/100-tests.py: -------------------------------------------------------------------------------- 1 | import random 2 | import ctypes 3 | 4 | cops = ctypes.CDLL('./100-operations.so') 5 | a = random.randint(-111, 111) 6 | b = random.randint(-111, 111) 7 | print("{} + {} = {}".format(a, b, cops.add(a, b))) 8 | print("{} - {} = {}".format(a, b, cops.sub(a, b))) 9 | print("{} x {} = {}".format(a, b, cops.mul(a, b))) 10 | print("{} / {} = {}".format(a, b, cops.div(a, b))) 11 | print("{} % {} = {}".format(a, b, cops.mod(a, b))) 12 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/6-sum_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * sum_dlistint - Sum the data of the list 5 | * @head: Head of the linked list 6 | * 7 | * Return: sum of all the data (int) 8 | */ 9 | int sum_dlistint(dlistint_t *head) 10 | { 11 | dlistint_t *t = head; 12 | int ans = 0; 13 | 14 | while (t == NULL) 15 | return (ans); 16 | 17 | while (t != NULL) 18 | { 19 | ans += t->n; 20 | t = t->next; 21 | } 22 | return (ans); 23 | } 24 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H_INCLUDED 2 | #define MAIN_H_INCLUDED 3 | 4 | int _putchar(char c); 5 | void print_alphabet(void); 6 | void print_alphabet_x10(void); 7 | int _islower(int c); 8 | int _isalpha(int c); 9 | int print_sign(int n); 10 | int _abs(int n); 11 | int print_last_digit(int n); 12 | void jack_bauer(void); 13 | void times_table(void); 14 | int add(int a, int b); 15 | void print_to_98(int n); 16 | void print_times_table(int n); 17 | #endif 18 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/8-print_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * print_array - prints the n elements of an array 5 | *@a: array to print 6 | *@n: number of elements in the aray 7 | *Return: Nothing 8 | */ 9 | void print_array(int *a, int n) 10 | { 11 | int i; 12 | 13 | for (i = 0; i < n; i++) 14 | { 15 | if (i == (n - 1)) 16 | printf("%d", *(a + i)); 17 | else 18 | printf("%d, ", *(a + i)); 19 | } 20 | printf("\n"); 21 | } 22 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/5-string_toupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * string_toupper - changes lowercase to uppercase 4 | * @str: the string to change 5 | * 6 | * Return: the string that change for uppercase 7 | */ 8 | char *string_toupper(char *str) 9 | { 10 | int i; 11 | 12 | i = 0; 13 | 14 | while (str[i] != '\0') 15 | { 16 | if (str[i] >= 97 && str[i] <= 122) 17 | { 18 | str[i] = str[i] - 32; 19 | } 20 | i++; 21 | } 22 | return (str); 23 | } 24 | -------------------------------------------------------------------------------- /0x09-static_libraries/0-memset.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_memset - fill a block of memory with a specific value 4 | *@s: starting address of memory to be filled 5 | *@b: the desired value 6 | *@n: number of bytes to be changed 7 | * 8 | *Return: changed array with new value for n bytes 9 | */ 10 | char *_memset(char *s, char b, unsigned int n) 11 | { 12 | int i = 0; 13 | 14 | for (; n > 0; i++) 15 | { 16 | s[i] = b; 17 | n--; 18 | } 19 | return (s); 20 | } 21 | -------------------------------------------------------------------------------- /0x09-static_libraries/1-memcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_memcpy - a function that copies memory area 4 | *@dest: memory where is stored 5 | *@src: memory where is copied 6 | *@n: number of bytes 7 | * 8 | *Return: copied memory with n byted changed 9 | */ 10 | char *_memcpy(char *dest, char *src, unsigned int n) 11 | { 12 | int r = 0; 13 | int i = n; 14 | 15 | for (; r < i; r++) 16 | { 17 | dest[r] = src[r]; 18 | n--; 19 | } 20 | return (dest); 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 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/1-memcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_memcpy - a function that copies memory area 4 | *@dest: memory where is stored 5 | *@src: memory where is copied 6 | *@n: number of bytes 7 | * 8 | *Return: copied memory with n byted changed 9 | */ 10 | char *_memcpy(char *dest, char *src, unsigned int n) 11 | { 12 | int r = 0; 13 | int i = n; 14 | 15 | for (; r < i; r++) 16 | { 17 | dest[r] = src[r]; 18 | n--; 19 | } 20 | return (dest); 21 | } 22 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/8-print_diagsums.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * print_diagsums - description 5 | * @a: 2d array of int types 6 | * @size: size of array (square) 7 | */ 8 | 9 | void print_diagsums(int *a, int size) 10 | { 11 | int i, sum1 = 0, sum2 = 0; 12 | 13 | for (i = 0; i < size; i++) 14 | { 15 | sum1 += a[i]; 16 | sum2 += a[size - i - 1]; 17 | a += size; 18 | } 19 | printf("%d, ", sum1); 20 | printf("%d\n", sum2); 21 | } 22 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/1-init_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | 4 | /** 5 | *init_dog - initializes a variable type 6 | * @d: pointer to struct 7 | * @name: parameter of name 8 | * @age: parameter of age 9 | * @owner: parameter of owner 10 | * 11 | * Return: nothing. 12 | **/ 13 | 14 | void init_dog(struct dog *d, char *name, float age, char *owner) 15 | { 16 | if (d == NULL) 17 | return; 18 | d->name = name; 19 | d->age = age; 20 | d->owner = owner; 21 | } 22 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/0-memset.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_memset - fill a block of memory with a specific value 4 | *@s: starting address of memory to be filled 5 | *@b: the desired value 6 | *@n: number of bytes to be changed 7 | * 8 | *Return: changed array with new value for n bytes 9 | */ 10 | char *_memset(char *s, char b, unsigned int n) 11 | { 12 | int i = 0; 13 | 14 | for (; n > 0; i++) 15 | { 16 | s[i] = b; 17 | n--; 18 | } 19 | return (s); 20 | } 21 | -------------------------------------------------------------------------------- /0x0B-malloc_free/4-free_grid.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | /** 5 | * free_grid - use free function on previous grid 6 | * @grid: 2 dim. array to free 7 | * @height: input height (size) 8 | * Return: free'd grid 9 | */ 10 | void free_grid(int **grid, int height) 11 | { 12 | int i; 13 | 14 | if (grid != NULL || height != 0) 15 | { 16 | for (i = 0; i < height; i++) 17 | { 18 | free(grid[i]); 19 | } 20 | free(grid); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-calc.h: -------------------------------------------------------------------------------- 1 | #ifndef _CALC_H_ 2 | #define _CALC_H_ 3 | int op_add(int a, int b); 4 | int op_sub(int a, int b); 5 | int op_mul(int a, int b); 6 | int op_div(int a, int b); 7 | int op_mod(int a, int b); 8 | int (*get_op_func(char *s))(int, int); 9 | 10 | /** 11 | * struct op - Struct op 12 | * 13 | * @op: The operator 14 | * @f: The function associated 15 | **/ 16 | 17 | typedef struct op 18 | { 19 | char *op; 20 | int (*f)(int a, int b); 21 | } op_t; 22 | #endif 23 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/7-print_diagonal.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_diagonal - a function that draws a diagonal line on the terminal. 5 | * @n: An input integer 6 | * Return: Always 0 7 | */ 8 | void print_diagonal(int n) 9 | { 10 | int i = 0, j; 11 | 12 | if (n > 0) 13 | { 14 | for (; i < n; i++) 15 | { 16 | for (j = 0; j < i; j++) 17 | _putchar(' '); 18 | _putchar(92); 19 | _putchar('\n'); 20 | } 21 | } 22 | else 23 | _putchar('\n'); 24 | } 25 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/7-puts_half.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * puts_half - Prints half of a string 4 | *@str: string to print 5 | *Return: 0 (success) 6 | */ 7 | void puts_half(char *str) 8 | { 9 | int i, len, l; 10 | 11 | len = 0; 12 | while (*(str + len) != '\0') 13 | len++; 14 | l = len; 15 | len = len / 2; 16 | if (l % 2 != 0) 17 | { 18 | len++; 19 | } 20 | for (i = 0; i < l; i++) 21 | { 22 | if (i >= len) 23 | _putchar(*(str + i)); 24 | } 25 | _putchar('\n'); 26 | } 27 | -------------------------------------------------------------------------------- /0x09-static_libraries/0-strcat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strcat - concatenates two strings 4 | * @dest: input parameter string 5 | * @src: input parameter string 6 | * 7 | * Return: dest 8 | */ 9 | char *_strcat(char *dest, char *src) 10 | { 11 | int a; 12 | int b; 13 | 14 | a = 0; 15 | 16 | while (dest[a] != 0) 17 | { 18 | a++; 19 | } 20 | 21 | b = 0; 22 | 23 | while (src[b] != 0) 24 | { 25 | dest[a] = src[b]; 26 | a++; 27 | b++; 28 | } 29 | return (dest); 30 | } 31 | -------------------------------------------------------------------------------- /0x00-hello_world/6-size.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Entry point 5 | * 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | printf("Size of a char: %zu byte(s)\n", sizeof(char)); 11 | printf("Size of an int: %zu byte(s)\n", sizeof(int)); 12 | printf("Size of a long int: %zu byte(s)\n", sizeof(long int)); 13 | printf("Size of a long long int: %zu byte(s)\n", sizeof(long long int)); 14 | printf("Size of a float: %zu byte(s)\n", sizeof(float)); 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/0-strcat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strcat - concatenates two strings 4 | * @dest: input parameter string 5 | * @src: input parameter string 6 | * 7 | * Return: dest 8 | */ 9 | char *_strcat(char *dest, char *src) 10 | { 11 | int a; 12 | int b; 13 | 14 | a = 0; 15 | 16 | while (dest[a] != 0) 17 | { 18 | a++; 19 | } 20 | 21 | b = 0; 22 | 23 | while (src[b] != 0) 24 | { 25 | dest[a] = src[b]; 26 | a++; 27 | b++; 28 | } 29 | return (dest); 30 | } 31 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/9-fizz_buzz.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - Entry piont 6 | * Return: Always 0 7 | */ 8 | int main(void) 9 | { 10 | int i = 1; 11 | 12 | for (; i < 100 ; i++) 13 | { 14 | if (i % 3 == 0 && i % 5 == 0) 15 | printf("FizzBuzz "); 16 | else if (i % 3 == 0) 17 | printf("Fizz "); 18 | else if (i % 5 == 0) 19 | printf("Buzz "); 20 | else 21 | printf("%d ", i); 22 | } 23 | printf("Buzz\n"); 24 | 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /0x09-static_libraries/5-strstr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strstr - Entry point 4 | * @haystack: input 5 | * @needle: input 6 | * Return: Always 0 (Success) 7 | */ 8 | char *_strstr(char *haystack, char *needle) 9 | { 10 | for (; *haystack != '\0'; haystack++) 11 | { 12 | char *l = haystack; 13 | char *p = needle; 14 | 15 | while (*l == *p && *p != '\0') 16 | { 17 | l++; 18 | p++; 19 | } 20 | 21 | if (*p == '\0') 22 | return (haystack); 23 | } 24 | 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/3-set_bit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * set_bit - Sets the value of a bit at a given index to 1. 5 | * @n: A pointer to the bit. 6 | * @index: The index to set the value at - indices start at 0. 7 | * 8 | * Return: If an error occurs - -1. 9 | * Otherwise - 1. 10 | */ 11 | int set_bit(unsigned long int *n, unsigned int index) 12 | { 13 | if (index >= (sizeof(unsigned long int) * 8)) 14 | return (-1); 15 | 16 | *n ^= (1 << index); 17 | 18 | return (1); 19 | } 20 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/5-strstr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strstr - Entry point 4 | * @haystack: input 5 | * @needle: input 6 | * Return: Always 0 (Success) 7 | */ 8 | char *_strstr(char *haystack, char *needle) 9 | { 10 | for (; *haystack != '\0'; haystack++) 11 | { 12 | char *l = haystack; 13 | char *p = needle; 14 | 15 | while (*l == *p && *p != '\0') 16 | { 17 | l++; 18 | p++; 19 | } 20 | 21 | if (*p == '\0') 22 | return (haystack); 23 | } 24 | 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /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/4-clear_bit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * clear_bit - Sets the value of a bit at a given index to 0. 5 | * @n: A pointer to the bit. 6 | * @index: The index to set the value at - indices start at 0. 7 | * 8 | * Return: If an error occurs - -1. 9 | * Otherwise - 1. 10 | */ 11 | int clear_bit(unsigned long int *n, unsigned int index) 12 | { 13 | if (index >= (sizeof(unsigned long int) * 8)) 14 | return (-1); 15 | 16 | *n &= ~(1 << index); 17 | 18 | return (1); 19 | } 20 | -------------------------------------------------------------------------------- /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 | int _putchar(char c); 6 | char *_memcpy(char *dest, char *src, unsigned int n); 7 | char *_strchr(char *s, char c); 8 | unsigned int _strspn(char *s, char *accept); 9 | char *_strpbrk(char *s, char *accept); 10 | char *_strstr(char *haystack, char *needle); 11 | void print_chessboard(char (*a)[8]); 12 | void print_diagsums(int *a, int size); 13 | void set_string(char **s, char *to); 14 | #endif 15 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/7-leet.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * leet - encodes a string 4 | * @str: the string to encode 5 | * 6 | * Return: the encode string 7 | */ 8 | char *leet(char *str) 9 | { 10 | int i, j; 11 | char c[] = "aAeEoOtTlL"; 12 | char n[] = "4433007711"; 13 | 14 | i = 0; 15 | while (str[i] != '\0') 16 | { 17 | j = 0; 18 | while (c[j] != '\0') 19 | { 20 | if (str[i] == c[j]) 21 | { 22 | str[i] = n[j]; 23 | } 24 | j++; 25 | } 26 | i++; 27 | } 28 | return (str); 29 | } 30 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-strspn.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strspn - Entry point 4 | * @s: input 5 | * @accept: input 6 | * Return: Always 0 (Success) 7 | */ 8 | unsigned int _strspn(char *s, char *accept) 9 | { 10 | unsigned int n = 0; 11 | int r; 12 | 13 | while (*s) 14 | { 15 | for (r = 0; accept[r]; r++) 16 | { 17 | if (*s == accept[r]) 18 | { 19 | n++; 20 | break; 21 | } 22 | else if (accept[r + 1] == '\0') 23 | return (n); 24 | } 25 | s++; 26 | } 27 | return (n); 28 | } 29 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/3-strspn.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strspn - Entry point 4 | * @s: input 5 | * @accept: input 6 | * Return: Always 0 (Success) 7 | */ 8 | unsigned int _strspn(char *s, char *accept) 9 | { 10 | unsigned int n = 0; 11 | int r; 12 | 13 | while (*s) 14 | { 15 | for (r = 0; accept[r]; r++) 16 | { 17 | if (*s == accept[r]) 18 | { 19 | n++; 20 | break; 21 | } 22 | else if (accept[r + 1] == '\0') 23 | return (n); 24 | } 25 | s++; 26 | } 27 | return (n); 28 | } 29 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/0-positive_or_negative.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /** 6 | * main - Prints if number is positive, zero or negative 7 | * 8 | * Return: Always (Success) 9 | */ 10 | int main(void) 11 | { 12 | int n; 13 | 14 | srand(time(0)); 15 | n = rand() - RAND_MAX / 2; 16 | 17 | if (n > 0) 18 | printf("%d is positive\n", n); 19 | else if (n == 0) 20 | printf("%d is zero\n", n); 21 | else 22 | printf("%d is negative\n", n); 23 | 24 | return (0); 25 | 26 | } 27 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/0-print_list.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "lists.h" 3 | 4 | /** 5 | * print_list - prints all the elements of a linked list 6 | * @h: pointer to the list_t list to print 7 | * 8 | * Return: the number of nodes printed 9 | */ 10 | size_t print_list(const list_t *h) 11 | { 12 | size_t n = 0; 13 | 14 | while (h) 15 | { 16 | if (!h->str) 17 | printf("[0] (nil)\n"); 18 | else 19 | printf("[%u] %s\n", h->len, h->str); 20 | h = h->next; 21 | n++; 22 | } 23 | 24 | return (n); 25 | } 26 | 27 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/8-print_square.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_square - draws a square 5 | * @size: length and width of square 6 | * 7 | * Return: void 8 | */ 9 | 10 | void print_square(int size) 11 | { 12 | int row; 13 | int column; 14 | 15 | if (size > 0) 16 | { 17 | for (row = 0; row < size; row++) 18 | { 19 | for (column = 0; column < size; column++) 20 | { 21 | _putchar('#'); 22 | } 23 | _putchar('\n'); 24 | } 25 | } 26 | else 27 | { 28 | _putchar('\n'); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /0x0F-function_pointers/1-array_iterator.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "function_pointers.h" 3 | 4 | /** 5 | * array_iterator - prints an integer 6 | * @array: is pointer to int 7 | * @action: is a pointer to the function 8 | * @size: is the size of the array 9 | * 10 | * Return: Nothing. 11 | **/ 12 | void array_iterator(int *array, size_t size, void (*action)(int)) 13 | { 14 | size_t i; 15 | if (array == NULL || action == NULL) 16 | { 17 | return; 18 | } 19 | for (i = 0; i < size; i++) 20 | { 21 | action(array[i]); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/2-get_bit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * get_bit - Gets the value of a bit at a given index. 5 | * @n: The bit. 6 | * @index: The index to get the value at - indices start at 0. 7 | * 8 | * Return: If an error occurs - -1. 9 | * Otherwise - The value of bit at index. 10 | */ 11 | int get_bit(unsigned long int n, unsigned int index) 12 | { 13 | if (index >= (sizeof(unsigned long int) * 8)) 14 | return (-1); 15 | 16 | if ((n & (1 << index)) == 0) 17 | return (0); 18 | 19 | return (1); 20 | } 21 | -------------------------------------------------------------------------------- /0x09-static_libraries/2-strncpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strncpy - copie the string 4 | * @dest: array that returns 5 | * @src: the pointer that receive the string 6 | * @n: times to interate 7 | * 8 | * Return: the parameter dest. 9 | */ 10 | char *_strncpy(char *dest, char *src, int n) 11 | { 12 | int i; 13 | 14 | i = 0; 15 | while (i < n && src[i] != '\0') 16 | { 17 | dest[i] = src[i]; 18 | i++; 19 | } 20 | 21 | i = i; 22 | while (i < n) 23 | { 24 | dest[i] = '\0'; 25 | i++; 26 | } 27 | return (dest); 28 | } 29 | -------------------------------------------------------------------------------- /0x09-static_libraries/1-strncat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strncat - concatenates two strings 4 | * @dest: input parameter string 5 | * @src: input parameter string 6 | * @n: times to iterations 7 | * 8 | * Return: dest 9 | */ 10 | char *_strncat(char *dest, char *src, int n) 11 | { 12 | int a; 13 | int b; 14 | 15 | a = 0; 16 | 17 | while (dest[a] != 0) 18 | { 19 | a++; 20 | } 21 | 22 | b = 0; 23 | 24 | while (src[b] != 0 && b < n) 25 | { 26 | dest[a] = src[b]; 27 | a++; 28 | b++; 29 | } 30 | return (dest); 31 | } 32 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/1-strncat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strncat - concatenates two strings 4 | * @dest: input parameter string 5 | * @src: input parameter string 6 | * @n: times to iterations 7 | * 8 | * Return: dest 9 | */ 10 | char *_strncat(char *dest, char *src, int n) 11 | { 12 | int a; 13 | int b; 14 | 15 | a = 0; 16 | 17 | while (dest[a] != 0) 18 | { 19 | a++; 20 | } 21 | 22 | b = 0; 23 | 24 | while (src[b] != 0 && b < n) 25 | { 26 | dest[a] = src[b]; 27 | a++; 28 | b++; 29 | } 30 | return (dest); 31 | } 32 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/2-strncpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strncpy - copie the string 4 | * @dest: array that returns 5 | * @src: the pointer that receive the string 6 | * @n: times to interate 7 | * 8 | * Return: the parameter dest. 9 | */ 10 | char *_strncpy(char *dest, char *src, int n) 11 | { 12 | int i; 13 | 14 | i = 0; 15 | while (i < n && src[i] != '\0') 16 | { 17 | dest[i] = src[i]; 18 | i++; 19 | } 20 | 21 | i = i; 22 | while (i < n) 23 | { 24 | dest[i] = '\0'; 25 | i++; 26 | } 27 | return (dest); 28 | } 29 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/5-get_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * get_dnodeint_at_index - Get the nth node of the list 5 | * @head: Head of the list 6 | * @index: Index of the list, starting with 0 7 | * 8 | * Return: Node in the current index 9 | */ 10 | dlistint_t *get_dnodeint_at_index(dlistint_t *head, unsigned int index) 11 | { 12 | dlistint_t *h = head; 13 | unsigned int i = 0; 14 | 15 | while (h != NULL) 16 | { 17 | if (i == index) 18 | return (h); 19 | i++; 20 | h = h->next; 21 | } 22 | 23 | return (NULL); 24 | } 25 | -------------------------------------------------------------------------------- /0x03-debugging/2-largest_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * largest_number - returns the largest of 3 numbers 5 | * @a: first integer 6 | * @b: second integer 7 | * @c: third integer 8 | * Return: largest number 9 | */ 10 | 11 | int largest_number(int a, int b, int c) 12 | { 13 | int largest; 14 | 15 | if (a > b && a > c) 16 | { 17 | largest = a; 18 | } 19 | else if (b > c && b > a) 20 | { 21 | largest = b; 22 | } 23 | else if (c > b) 24 | { 25 | largest = c; 26 | } 27 | else 28 | { 29 | largest = b; 30 | } 31 | 32 | return (largest); 33 | } 34 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/102-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints the first 50 Fibonacci numbers, starting with 1 and 2 5 | * followed by a new line 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | long int i, j, k, next; 11 | 12 | j = 1; 13 | 14 | k = 2; 15 | 16 | for (i = 1; i <= 50; ++i) 17 | { 18 | if (j != 20365011074) 19 | { 20 | printf("%ld, ", j); 21 | } else 22 | { 23 | printf("%ld\n", j); 24 | } 25 | next = j + k; 26 | j = k; 27 | k = next; 28 | } 29 | 30 | return (0); 31 | } 32 | -------------------------------------------------------------------------------- /0x09-static_libraries/100-atoi.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _atoi - convert a string to an integer. 5 | * @s: the string to be comverted. 6 | * 7 | *Return: The integer value of the comverted string. 8 | */ 9 | 10 | int _atoi(char *s) 11 | { 12 | 13 | int sign = 1; 14 | unsigned int num = 0; 15 | 16 | do { 17 | 18 | if (*s == '-') 19 | sign *= -1; 20 | 21 | else if (*s >= '0' && *s <= '9') 22 | num = (num * 10) + (*s - '0'); 23 | 24 | else if (num > 0) 25 | break; 26 | 27 | } while (*s++); 28 | 29 | return (num * sign); 30 | } 31 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/5-flip_bits.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * flip_bits - Counts the number of bits needed to be 5 | * flipped to get from one number to another. 6 | * @n: The number. 7 | * @m: The number to flip n to. 8 | * 9 | * Return: The necessary number of bits to flip to get from n to m. 10 | */ 11 | unsigned int flip_bits(unsigned long int n, unsigned long int m) 12 | { 13 | unsigned long int xor = n ^ m, bits = 0; 14 | 15 | while (xor > 0) 16 | { 17 | bits += (xor & 1); 18 | xor >>= 1; 19 | } 20 | 21 | return (bits); 22 | } 23 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/100-atoi.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _atoi - convert a string to an integer. 5 | * @s: the string to be comverted. 6 | * 7 | *Return: The integer value of the comverted string. 8 | */ 9 | 10 | int _atoi(char *s) 11 | { 12 | 13 | int sign = 1; 14 | unsigned int num = 0; 15 | 16 | do { 17 | 18 | if (*s == '-') 19 | sign *= -1; 20 | 21 | else if (*s >= '0' && *s <= '9') 22 | num = (num * 10) + (*s - '0'); 23 | 24 | else if (num > 0) 25 | break; 26 | 27 | } while (*s++); 28 | 29 | return (num * sign); 30 | } 31 | -------------------------------------------------------------------------------- /0x08-recursion/5-sqrt_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * sqrt_a - main - check the code 5 | * @a: number 6 | * @b: number of power 7 | * Return: Always 0. 8 | */ 9 | 10 | int sqrt_a(int a, int b) 11 | { 12 | if (b * b == a) 13 | { 14 | return (b); 15 | } 16 | else if (b * b > a) 17 | { 18 | return (-1); 19 | } 20 | return (sqrt_a(a, b + 1)); 21 | } 22 | 23 | /** 24 | * _sqrt_recursion - main - check the code 25 | * @n: number 26 | * 27 | * Return: Always 0. 28 | */ 29 | int _sqrt_recursion(int n) 30 | { 31 | return (sqrt_a(n, 0)); 32 | } 33 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/103-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - finds and prints the sum of the even-valued terms 5 | * followed by a new line 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | int i; 11 | unsigned long int j, k, next, sum; 12 | 13 | j = 1; 14 | k = 2; 15 | sum = 0; 16 | 17 | for (i = 1; i <= 33; ++i) 18 | { 19 | if (j < 4000000 && (j % 2) == 0) 20 | { 21 | sum = sum + j; 22 | } 23 | next = j + k; 24 | j = k; 25 | k = next; 26 | } 27 | 28 | printf("%lu\n", sum); 29 | 30 | return (0); 31 | } 32 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x0A-argc_argv/3-mul.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * main - prints the function. 6 | * @argc: counts agruments passed to the function for int. 7 | * @argv: gives the value passed to the function for char. 8 | * 9 | * Return: Returns always success. 10 | **/ 11 | 12 | int main(int argc, char *argv[]) 13 | { 14 | int i, res = 1; 15 | 16 | if (argc != 3) 17 | { 18 | printf("%s\n", "Error"); 19 | return (1); 20 | } 21 | else 22 | { 23 | for (i = 1; i < argc; i++) 24 | { 25 | res *= atoi(argv[i]); 26 | } 27 | printf("%d\n", res); 28 | } 29 | return (0); 30 | } 31 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | char *_strcat(char *dest, char *src); 4 | char *_strncat(char *dest, char *src, int n); 5 | char *_strncpy(char *dest, char *src, int n); 6 | int _strcmp(char *s1, char *s2); 7 | void reverse_array(int *a, int n); 8 | char *string_toupper(char *str); 9 | char *cap_string(char *str); 10 | char *leet(char *str); 11 | char *rot13(char *str); 12 | void print_number(int n); 13 | int _putchar(char c); 14 | void print_buffer(char *b, int size); 15 | char *infinite_add(char *n1, char *n2, char *r, int size_r); 16 | #endif /*MAIN_H*/ 17 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/101-natural.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - computes and prints the sum of all the multiples 5 | * of 3 or 5 below 1024 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | unsigned long int sum3, sum5, sum; 11 | int i; 12 | 13 | sum3 = 0; 14 | sum5 = 0; 15 | sum = 0; 16 | 17 | for (i = 0; i < 1024; ++i) 18 | { 19 | if ((i % 3) == 0) 20 | { 21 | sum3 = sum3 + i; 22 | } else if ((i % 5) == 0) 23 | { 24 | sum5 = sum5 + i; 25 | } 26 | } 27 | sum = sum3 + sum5; 28 | printf("%lu\n", sum); 29 | return (0); 30 | } 31 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/10-print_triangle.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_triangle - a function that prints a triangle, followed by a new line. 5 | * @size: An input integer 6 | * Return: Always 0 7 | */ 8 | void print_triangle(int size) 9 | { 10 | int i = 0, j, n = size - 1; 11 | 12 | if (size > 0) 13 | { 14 | for (; i < size; i++) 15 | { 16 | for (j = 0; j < size; j++) 17 | { 18 | if (j < n) 19 | _putchar(' '); 20 | else 21 | _putchar('#'); 22 | } 23 | n--; 24 | _putchar('\n'); 25 | } 26 | } 27 | else 28 | _putchar('\n'); 29 | } 30 | -------------------------------------------------------------------------------- /0x08-recursion/6-is_prime_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * prime_a - main - check the code 5 | * @a: number 6 | * @b: number of power 7 | * Return: Always 0. 8 | */ 9 | 10 | int prime_a(int a, int b) 11 | { 12 | if (a <= 1 || (a != b && a % b == 0)) 13 | { 14 | return (0); 15 | } 16 | else if (a == b) 17 | { 18 | return (1); 19 | } 20 | return (prime_a(a, b + 1)); 21 | } 22 | 23 | 24 | /** 25 | * is_prime_number - main - check the code 26 | * @n: number 27 | * 28 | * Return: Always 0. 29 | */ 30 | int is_prime_number(int n) 31 | { 32 | return (prime_a(n, 2)); 33 | } 34 | -------------------------------------------------------------------------------- /0x08-recursion/101-wildcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * wildcmp - Compare strings 5 | * @s1: pointer to string params 6 | * @s2: pointer to string params 7 | * Return: 0 8 | */ 9 | 10 | int wildcmp(char *s1, char *s2) 11 | { 12 | if (*s1 == '\0') 13 | { 14 | if (*s2 != '\0' && *s2 == '*') 15 | { 16 | return (wildcmp(s1, s2 + 1)); 17 | } 18 | return (*s2 == '\0'); 19 | } 20 | 21 | if (*s2 == '*') 22 | { 23 | return (wildcmp(s1 + 1, s2) || wildcmp(s1, s2 + 1)); 24 | } 25 | else if (*s1 == *s2) 26 | { 27 | return (wildcmp(s1 + 1, s2 + 1)); 28 | } 29 | return (0); 30 | } 31 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/3-array_range.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * array_range - creates an array of integers. 6 | * @min: first integer number 7 | * @max: last integer number 8 | * 9 | * Return: pointer to newly allocated memory or NULL if fails 10 | */ 11 | int *array_range(int min, int max) 12 | { 13 | int i, l; 14 | int *a; 15 | 16 | if (min > max) 17 | return (NULL); 18 | l = max - min + 1; 19 | a = malloc(sizeof(int) * l); 20 | if (a == NULL) 21 | return (NULL); 22 | for (i = 0; i < l; i++, min++) 23 | { 24 | a[i] = min; 25 | } 26 | return (a); 27 | } 28 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/100-print_comb3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Prints numbers between 00 to 89. 5 | * 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | int i, e; 11 | 12 | i = 48; 13 | e = 48; 14 | 15 | while (e < 58) 16 | { 17 | i = 48; 18 | while (i < 58) 19 | { 20 | if (e != i && e < i) 21 | { 22 | putchar(e); 23 | putchar(i); 24 | if (i == 57 && e == 56) 25 | { 26 | break; 27 | } 28 | putchar(','); 29 | putchar(' '); 30 | } 31 | i++; 32 | } 33 | e++; 34 | } 35 | putchar('\n'); 36 | return (0); 37 | } 38 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/2-calloc.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _calloc - allocates memory for an array, using malloc. 6 | * @nmemb: number of elements in the array 7 | * @size: size in bytes of the elements 8 | * 9 | * Return: void pointer to allocated memory 10 | */ 11 | void *_calloc(unsigned int nmemb, unsigned int size) 12 | { 13 | char *p; 14 | unsigned int i; 15 | 16 | if (nmemb == 0 || size == 0) 17 | return (NULL); 18 | p = malloc(nmemb * size); 19 | if (p == NULL) 20 | return (NULL); 21 | for (i = 0; i < (nmemb * size); i++) 22 | p[i] = 0; 23 | return (p); 24 | } 25 | -------------------------------------------------------------------------------- /0x1A-hash_tables/0-hash_table_create.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_create - creattes a hash table. 5 | * @size: sizeof the hash table. 6 | * 7 | * Return: pointer to has table type. 8 | */ 9 | hash_table_t *hash_table_create(unsigned long int size) 10 | { 11 | hash_table_t *ht; 12 | 13 | if (!size) 14 | return (NULL); 15 | 16 | ht = calloc(1, sizeof(hash_table_t)); 17 | if (!ht) 18 | return (NULL); 19 | ht->size = size; 20 | ht->array = calloc(size, sizeof(hash_node_t *)); 21 | if (!ht->array) 22 | { 23 | free(ht); 24 | return (NULL); 25 | } 26 | return (ht); 27 | } 28 | -------------------------------------------------------------------------------- /0x1A-hash_tables/5-hash_table_print.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | /** 3 | *hash_table_print - function that prints a hash table 4 | * 5 | *@ht: is the hash table 6 | */ 7 | void hash_table_print(const hash_table_t *ht) 8 | { 9 | hash_node_t *node; 10 | unsigned long int cont = 0, i = 0; 11 | 12 | if (ht != NULL) 13 | { 14 | printf("{"); 15 | for (cont = 0; cont < ht->size; cont++) 16 | { 17 | node = ht->array[cont]; 18 | while (node != NULL) 19 | { 20 | if (i != 0) 21 | printf(", "); 22 | i = 1; 23 | printf("'%s': '%s'", node->key, node->value); 24 | node = node->next; 25 | } 26 | } 27 | printf("}\n"); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/2-print_dog.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | #include 4 | /** 5 | * print_dog - Prints name, age and owner if not null. 6 | * @d: is pointer to the struct dog. 7 | * 8 | * Return: nothing. 9 | **/ 10 | 11 | void print_dog(struct dog *d) 12 | { 13 | if (d == NULL) 14 | { 15 | return; 16 | } 17 | if (d->name == NULL) 18 | { 19 | printf("Name: (nil)\n"); 20 | } 21 | else 22 | { 23 | printf("Name: %s\n", d->name); 24 | } 25 | printf("Age: %f\n", d->age); 26 | if (d->owner == NULL) 27 | { 28 | printf("Owner: (nil)\n"); 29 | } 30 | else 31 | printf("Owner: %s\n", d->owner); 32 | } 33 | -------------------------------------------------------------------------------- /0x0F-function_pointers/2-int_index.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "function_pointers.h" 3 | 4 | /** 5 | * int_index - searches for an integer. 6 | * @array: Name passed 7 | * @size: Number of iterations 8 | * @cmp: Points to a function 9 | * Return: Returns nothing 10 | **/ 11 | 12 | int int_index(int *array, int size, int (*cmp)(int)) 13 | { 14 | int i; 15 | 16 | if (array == NULL || cmp == NULL) 17 | { 18 | return (-1); 19 | } 20 | 21 | if (size <= 0) 22 | { 23 | return (-1); 24 | } 25 | 26 | for (i = 0; i < size; i++) 27 | { 28 | if (cmp(array[i])) 29 | { 30 | return (i); 31 | } 32 | } 33 | 34 | return (-1); 35 | } 36 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/1-last_digit.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /** 6 | * main - prints the last digit of the random 7 | * number stored in the variable n 8 | * Return: Always 0 (Success) 9 | */ 10 | int main(void) 11 | { 12 | int n; 13 | 14 | srand(time(0)); 15 | n = rand() - RAND_MAX / 2; 16 | printf("Last digit of %d is %d ", n, n % 10); 17 | if (n % 10 > 5) 18 | printf("and is greater than 5\n"); 19 | else if (n % 10 == 0) 20 | printf("and is 0\n"); 21 | else if (n % 10 < 6 && n % 10 != 0) 22 | printf("and is less than 6 and not 0\n"); 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/test/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 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/dog.h: -------------------------------------------------------------------------------- 1 | #ifndef _DOG_H_ 2 | #define _DOG_H_ 3 | /** 4 | * struct dog -Dog data. 5 | * @name: Name of the dog 6 | * @age: age of the dog. 7 | * @owner: owner of the dog. 8 | * Desctiption: a structure with the name, age and owner of a dog. 9 | **/ 10 | 11 | typedef struct dog 12 | { 13 | char *name; 14 | char *owner; 15 | float age; 16 | } dog_t; 17 | void init_dog(struct dog *d, char *name, float age, char *owner); 18 | void print_dog(struct dog *d); 19 | dog_t *new_dog(char *name, float age, char *owner); 20 | void free_dog(dog_t *d); 21 | char *_strncpy(char *dest, char *src, int n); 22 | int _strlen(char *s); 23 | #endif 24 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-get_op_func.c: -------------------------------------------------------------------------------- 1 | #include "3-calc.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * get_op_func - Compares function with pointer and returns the equal. 7 | * @s: points a char 8 | * Return: Returns the function and the operator when equal 9 | **/ 10 | 11 | int (*get_op_func(char *s))(int, int) 12 | { 13 | op_t ops[] = { 14 | {"+", op_add}, 15 | {"-", op_sub}, 16 | {"*", op_mul}, 17 | {"/", op_div}, 18 | {"%", op_mod}, 19 | {NULL, NULL} 20 | }; 21 | 22 | int i = 0; 23 | 24 | while (ops[i].op != NULL) 25 | { 26 | if (!strcmp(ops[i].op, s)) 27 | return (ops[i].f); 28 | i++; 29 | } 30 | return (NULL); 31 | } 32 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/101-print_comb4.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints all possible different combinations of three digits 5 | * Return: Always 0 (Success) 6 | */ 7 | int main(void) 8 | { 9 | int n, m, l; 10 | 11 | for (n = 48; n < 58; n++) 12 | { 13 | for (m = 49; m < 58; m++) 14 | { 15 | for (l = 50; l < 58; l++) 16 | { 17 | if (l > m && m > n) 18 | { 19 | putchar(n); 20 | putchar(m); 21 | putchar(l); 22 | if (n != 55 || m != 56) 23 | { 24 | putchar(','); 25 | putchar(' '); 26 | } 27 | } 28 | } 29 | } 30 | } 31 | putchar('\n'); 32 | return (0); 33 | } 34 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/100-rot13.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * rot13 - encodes a string using rot13 4 | * @str: the string to encode 5 | * 6 | * Return: encode string 7 | */ 8 | char *rot13(char *str) 9 | { 10 | int i, j; 11 | 12 | char alpha[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; 13 | char rot[] = "NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm"; 14 | 15 | i = 0; 16 | while (str[i] != '\0') 17 | { 18 | j = 0; 19 | while (alpha[j] != '\0') 20 | { 21 | if (str[i] == alpha[j]) 22 | { 23 | str[i] = rot[j]; 24 | break; 25 | } 26 | j++; 27 | } 28 | i++; 29 | } 30 | return (str); 31 | } 32 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/2-add_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_dnodeint - Add a node in the head of the list 5 | * @head: Memory of the Head of the linked list 6 | * @n: Integer data 7 | * 8 | * Return: Head of the list 9 | */ 10 | dlistint_t *add_dnodeint(dlistint_t **head, const int n) 11 | { 12 | dlistint_t *new = malloc(sizeof(dlistint_t)); 13 | 14 | if (head == NULL || new == NULL) 15 | return (NULL); 16 | 17 | new->n = n; 18 | new->prev = NULL; 19 | new->next = NULL; 20 | 21 | if (*head != NULL) 22 | { 23 | new->next = *head; 24 | (*head)->prev = new; 25 | } 26 | 27 | *head = new; 28 | 29 | return (*head); 30 | } 31 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | /* 5 | * File: main.h 6 | * 7 | * Desc: Header file containing prototypes for all functions 8 | * written in the 0x13-bit_manipulation directory. 9 | */ 10 | 11 | int _putchar(char c); 12 | unsigned int binary_to_uint(const char *b); 13 | void print_binary(unsigned long int n); 14 | int get_bit(unsigned long int n, unsigned int index); 15 | int set_bit(unsigned long int *n, unsigned int index); 16 | int clear_bit(unsigned long int *n, unsigned int index); 17 | unsigned int flip_bits(unsigned long int n, unsigned long int m); 18 | int get_endianness(void); 19 | 20 | #endif /* MAIN_H */ 21 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/102-print_comb5.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints all possible combinations of two two-digit numbers 5 | * Return: Always 0 (Success) 6 | */ 7 | int main(void) 8 | { 9 | int i, j; 10 | 11 | for (i = 0; i < 100; i++) 12 | { 13 | for (j = 0; j < 100; j++) 14 | { 15 | if (i < j) 16 | { 17 | putchar((i / 10) + 48); 18 | putchar((i % 10) + 48); 19 | putchar(' '); 20 | putchar((j / 10) + 48); 21 | putchar((j % 10) + 48); 22 | if (i != 98 || j != 99) 23 | { 24 | putchar(','); 25 | putchar(' '); 26 | } 27 | } 28 | 29 | } 30 | } 31 | putchar('\n'); 32 | return (0); 33 | } 34 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/9-times_table.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * times_table - prints times table 5 | * Return:void 6 | */ 7 | 8 | void times_table(void) 9 | { 10 | 11 | int a = 0; 12 | int b; 13 | int rep; 14 | while (a <= 9) 15 | { 16 | b = 0; 17 | while (b <= 9) 18 | { 19 | rep = a * b; 20 | if (b == 0) 21 | { 22 | _putchar('0' + rep); 23 | } 24 | else if (rep < 10) 25 | { 26 | _putchar(' '); 27 | _putchar('0' + rep); 28 | } 29 | else 30 | { 31 | _putchar('0' + rep / 10); 32 | _putchar('0' + rep % 10); 33 | } 34 | 35 | if (b < 9) 36 | { 37 | _putchar(','); 38 | _putchar(' '); 39 | } 40 | b++; 41 | } 42 | _putchar('\n'); 43 | a++; 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/2-add_node.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "lists.h" 4 | 5 | /** 6 | * add_node - adds a new node at the beginning of a linked list 7 | * @head: double pointer to the list_t list 8 | * @str: new string to add in the node 9 | * 10 | * Return: the address of the new element, or NULL if it fails 11 | */ 12 | list_t *add_node(list_t **head, const char *str) 13 | { 14 | list_t *new; 15 | unsigned int len = 0; 16 | 17 | while (str[len]) 18 | len++; 19 | 20 | new = malloc(sizeof(list_t)); 21 | if (!new) 22 | return (NULL); 23 | 24 | new->str = strdup(str); 25 | new->len = len; 26 | new->next = (*head); 27 | (*head) = new; 28 | 29 | return (*head); 30 | } 31 | 32 | -------------------------------------------------------------------------------- /0x1A-hash_tables/6-hash_table_delete.c: -------------------------------------------------------------------------------- 1 | include "hash_tables.h" 2 | 3 | /** 4 | * free_list - frees a linked list 5 | * @head: hash_node_t list to be freed 6 | */ 7 | void free_list(hash_node_t *head) 8 | { 9 | hash_node_t *temp; 10 | 11 | while (head) 12 | { 13 | temp = head->next; 14 | free(head->key); 15 | free(head->value); 16 | free(head); 17 | head = temp; 18 | } 19 | } 20 | 21 | /** 22 | * hash_table_delete - deletes a hash table 23 | * @ht: hash table to be deleted 24 | */ 25 | void hash_table_delete(hash_table_t *ht) 26 | { 27 | unsigned long int i; 28 | 29 | if (!ht) 30 | return; 31 | 32 | for (i = 0; i < ht->size; i++) 33 | free_list(ht->array[i]); 34 | free(ht->array); 35 | free(ht); 36 | } 37 | -------------------------------------------------------------------------------- /0x15-file_io/README.md: -------------------------------------------------------------------------------- 1 | ## THE LAST TASK 2 | ### Adegoke Solomon Eniola| Lagos, NIGERIA | 2 JUNE 2022 3 | 4 | --Finishing the ALX Low_level_programming with C 5 | --C files are compiled using gcc 4.8.4 6 | --C files are written according to the C90 standard 7 | 8 | Tested on Ubuntu 14.04 LTS 9 | Files 10 | All of the following files are programs written in C: 11 | 12 | ### Filename Description 13 | 0-read_textfile.c Reads a text file and prints it to the POSIX standard output 14 | 1-create_file.c Creates a file 15 | 2-append_text_to_file.c Appends text at the end of a file 16 | 3-cp.c Copies the content of a file to another file 17 | 100-elf_header.c 18 | Displays the information contained in the ELF header at the start of an ELF file 19 | -------------------------------------------------------------------------------- /0x0B-malloc_free/0-create_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | *create_array - creates an array of chars and initializes it with 5 | *a specific char. 6 | *@size: size of array. 7 | *@c: char to initialize array with. 8 | * 9 | *Return: NULL if it fails or 10 | *Pointer to array. 11 | */ 12 | char *create_array(unsigned int size, char c) 13 | { 14 | char *arr; 15 | unsigned int i; 16 | 17 | if (size == 0) 18 | { 19 | return (NULL); 20 | } 21 | 22 | arr = malloc(sizeof(char) * size); 23 | 24 | /* check if malloc was successful */ 25 | 26 | if (arr == NULL) 27 | return (NULL); 28 | 29 | for (i = 0; i < size; i++) 30 | { 31 | arr[i] = c; 32 | } 33 | 34 | return (arr); 35 | 36 | } 37 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef LISTS_H 2 | #define LISTS_H 3 | #include 4 | #include 5 | /** 6 | * struct list_s - singly linked list 7 | * @str: string - (malloc'ed string) 8 | * @len: length of the string 9 | * @next: points to the next node 10 | * 11 | * Description: singly linked list node structure 12 | * for Holberton project 13 | */ 14 | typedef struct list_s 15 | { 16 | char *str; 17 | unsigned int len; 18 | struct list_s *next; 19 | } list_t; 20 | 21 | size_t print_list(const list_t *h); 22 | size_t list_len(const list_t *h); 23 | list_t *add_node(list_t **head, const char *str); 24 | list_t *add_node_end(list_t **head, const char *str); 25 | void free_list(list_t *head); 26 | 27 | #endif 28 | 29 | -------------------------------------------------------------------------------- /0x1A-hash_tables/4-hash_table_get.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | /** 3 | *hash_table_get - function that retrieves a value associated with a key 4 | * 5 | *@ht: hash table you want to look into 6 | *@key: the key you are looking for 7 | * 8 | *Return: the value associated with the element else NULL 9 | */ 10 | char *hash_table_get(const hash_table_t *ht, const char *key) 11 | { 12 | hash_node_t *nodo; 13 | unsigned long int index; 14 | 15 | if (key == NULL || ht == NULL) 16 | return (NULL); 17 | 18 | index = key_index((const unsigned char *)key, ht->size); 19 | nodo = ht->array[index]; 20 | 21 | for (; nodo != NULL; nodo = nodo->next) 22 | { 23 | if (strcmp(nodo->key, key) == 0) 24 | return (nodo->value); 25 | } 26 | return (NULL); 27 | } 28 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/3-add_dnodeint_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_dnodeint_end - Add node in the end of the list 5 | * @head: Memory address of the head of the list 6 | * @n: Integer data 7 | * 8 | * Return: Head of the list 9 | */ 10 | dlistint_t *add_dnodeint_end(dlistint_t **head, const int n) 11 | { 12 | dlistint_t *new = malloc(sizeof(dlistint_t)); 13 | dlistint_t *last = *head; 14 | 15 | if (new == NULL) 16 | return (NULL); 17 | 18 | new->n = n; 19 | new->next = NULL; 20 | new->prev = NULL; 21 | 22 | if (*head == NULL) 23 | { 24 | *head = new; 25 | return (new); 26 | } 27 | 28 | while (last->next != NULL) 29 | last = last->next; 30 | 31 | last->next = new; 32 | new->prev = last; 33 | return (new); 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 | * @str: the string to change the first letter of a word in uppercase 5 | * 6 | * Return: capitalizes letters 7 | */ 8 | char *cap_string(char *str) 9 | { 10 | int i; 11 | int j; 12 | char c[] = {44, 59, 46, 33, 63, 34, 40, 41, 123, 125, 32, 10, 9}; 13 | 14 | i = 0; 15 | 16 | while (str[i] != '\0') 17 | { 18 | if (i == 0 && str[i] >= 97 && str[i] <= 122) 19 | { 20 | str[i] = str[i] - 32; 21 | } 22 | j = 0; 23 | while (c[j] != '\0') 24 | { 25 | if (c[j] == str[i] && (str[i + 1] >= 97 && str[i + 1] <= 122)) 26 | { 27 | str[i + 1] = str[i + 1] - 32; 28 | } 29 | j++; 30 | } 31 | i++; 32 | } 33 | return (str); 34 | } 35 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x0A-argc_argv/4-add.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /** 6 | * main - cheks the fuction. 7 | * @argc: couts the arguments passed to the function. 8 | * @argv: gives the value for the argument passed to the function. 9 | * 10 | * Return: Returns always success. 11 | */ 12 | 13 | int main(int argc, char *argv[]) 14 | { 15 | int sum; 16 | int count; 17 | int i; 18 | 19 | count = 1; 20 | sum = 0; 21 | if (argc == 1) 22 | { 23 | printf("0\n"); 24 | return (0); 25 | } 26 | while (count < argc) 27 | { 28 | for (i = 0; argv[count][i] != '\0'; i++) 29 | { 30 | if (!(isdigit(argv[count][i]))) 31 | { 32 | printf("Error\n"); 33 | return (1); 34 | } 35 | } 36 | sum += atoi(argv[count]); 37 | count++; 38 | } 39 | printf("%d\n", sum); 40 | return (0); 41 | } 42 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/main.h: -------------------------------------------------------------------------------- 1 | #ifndef _MAIN_H_ 2 | #define _MAIN_H_ 3 | 4 | int _putchar(char c); 5 | int _islower(int c); 6 | int _isalpha(int c); 7 | int _abs(int n); 8 | int _isupper(int c); 9 | int _isdigit(int c); 10 | int _strlen(char *s); 11 | void _puts(char *s); 12 | char *_strcpy(char *dest, char *src); 13 | int _atoi(char *s); 14 | char *_strcat(char *dest, char *src); 15 | char *_strncat(char *dest, char *src, int n); 16 | char *_strncpy(char *dest, char *src, int n); 17 | int _strcmp(char *s1, char *s2); 18 | char *_memset(char *s, char b, unsigned int n); 19 | char *_memcpy(char *dest, char *src, unsigned int n); 20 | char *_strchr(char *s, char c); 21 | unsigned int _strspn(char *s, char *accept); 22 | char *_strpbrk(char *s, char *accept); 23 | char *_strstr(char *haystack, char *needle); 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /0x1C-makefiles/5-island_perimeter.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | """Defines an island perimeter measuring function.""" 3 | 4 | def island_perimeter(grid): 5 | """Return the perimeter of an island. 6 | Args: 7 | grid (list): A list of list of integers representing an island. 8 | Returns: 9 | The perimeter of the island defined in grid. 10 | """ 11 | width = len(grid[0]) 12 | height = len(grid) 13 | edges = 0 14 | size = 0 15 | 16 | for i in range(height): 17 | for j in range(width): 18 | if grid[i][j] == 1: 19 | size += 1 20 | if (j > 0 and grid[i][j - 1] == 1): 21 | edges += 1 22 | if (i > 0 and grid[i - 1][j] == 1): 23 | edges += 1 24 | return size * 4 - edges * 2 25 | -------------------------------------------------------------------------------- /0x0B-malloc_free/3-alloc_grid.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | /** 5 | * alloc_grid - nested loop to make grid 6 | * @width: width input 7 | * @height: height input 8 | * Return: pointer to 2 dim. array 9 | */ 10 | int **alloc_grid(int width, int height) 11 | { 12 | int **grid; 13 | int i, j; 14 | 15 | if (width <= 0 || height <= 0) 16 | return (NULL); 17 | 18 | grid = malloc(height * sizeof(int *)); 19 | if (grid == NULL) 20 | return (NULL); 21 | 22 | for (i = 0; i < height; i++) 23 | { 24 | grid[i] = malloc(width * sizeof(int)); 25 | if (grid[i] == NULL) 26 | { 27 | while (i >= 0) 28 | free(grid[i--]); 29 | free(grid); 30 | return (NULL); 31 | } 32 | 33 | 34 | for (j = 0; j < width; j++) 35 | grid[i][j] = 0; 36 | } 37 | 38 | return (grid); 39 | } 40 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/3-add_node_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_node_end - adds a new node at the end 5 | * of a list_t list. 6 | * @head: head of the linked list. 7 | * @str: string to store in the list. 8 | * Return: address of the head. 9 | */ 10 | 11 | list_t *add_node_end(list_t **head, const char *str) 12 | { 13 | list_t *new, *temp; 14 | size_t nchar; 15 | 16 | new = malloc(sizeof(list_t)); 17 | if (new == NULL) 18 | return (NULL); 19 | 20 | new->str = strdup(str); 21 | 22 | for (nchar = 0; str[nchar]; nchar++) 23 | ; 24 | 25 | new->len = nchar; 26 | new->next = NULL; 27 | temp = *head; 28 | 29 | if (temp == NULL) 30 | { 31 | *head = new; 32 | } 33 | else 34 | { 35 | while (temp->next != NULL) 36 | temp = temp->next; 37 | temp->next = new; 38 | } 39 | 40 | return (*head); 41 | } 42 | -------------------------------------------------------------------------------- /0x0B-malloc_free/2-str_concat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | /** 5 | * str_concat - concatenates 2 strings 6 | * 7 | * @s1: first string 8 | * @s2: string to add to end of of first string 9 | * 10 | * Return: pointer to newly allocated string concatenation 11 | */ 12 | char *str_concat(char *s1, char *s2) 13 | { 14 | unsigned int size1 = 0, size2 = 0; 15 | char *ptr, *ret; 16 | 17 | ptr = s1; 18 | if (s1) 19 | while (*ptr++) 20 | size1++; 21 | else 22 | s1 = ""; 23 | 24 | ptr = s2; 25 | if (s2) 26 | while (*ptr++) 27 | size2++; 28 | else 29 | s2 = ""; 30 | 31 | ret = malloc(size1 + size2 + 1); 32 | if (!ret) 33 | return (NULL); 34 | 35 | ptr = ret; 36 | while (*s1) 37 | *ptr++ = *s1++; 38 | while (*s2) 39 | *ptr++ = *s2++; 40 | *ptr = 0; 41 | 42 | return (ret); 43 | } 44 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/104-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - finds and prints the first 98 Fibonacci numbers, 5 | * starting with 1 and 2 6 | * followed by a new line 7 | * Return: ALways 0 (Success) 8 | */ 9 | int main(void) 10 | { 11 | unsigned long int i, j, k, j1, j2, k1, k2; 12 | 13 | j = 1; 14 | k = 2; 15 | 16 | printf("%lu", j); 17 | 18 | for (i = 1; i < 91; i++) 19 | { 20 | printf(", %lu", k); 21 | k = k + j; 22 | j = k - j; 23 | } 24 | 25 | j1 = j / 1000000000; 26 | j2 = j % 1000000000; 27 | k1 = k / 1000000000; 28 | k2 = k % 1000000000; 29 | 30 | for (i = 92; i < 99; ++i) 31 | { 32 | printf(", %lu", k1 + (k2 / 1000000000)); 33 | printf("%lu", k2 % 1000000000); 34 | k1 = k1 + j1; 35 | j1 = k1 - j1; 36 | k2 = k2 + j2; 37 | j2 = k2 - j2; 38 | } 39 | 40 | printf("\n"); 41 | 42 | return (0); 43 | } 44 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-main.c: -------------------------------------------------------------------------------- 1 | #include "3-calc.h" 2 | #include 3 | #include 4 | #include 5 | 6 | /** 7 | * main - Prints result of an operation 8 | * @argc: Number of arguments 9 | * @argv: Value of arguments 10 | * Return: Returns an int. 11 | **/ 12 | 13 | int main(int argc, char *argv[]) 14 | { 15 | 16 | if (argc != 4) 17 | { 18 | printf("Error\n"); 19 | exit(98); 20 | } 21 | 22 | if ( 23 | (strcmp(argv[2], "+") && 24 | strcmp(argv[2], "-") && 25 | strcmp(argv[2], "*") && 26 | strcmp(argv[2], "/") && 27 | strcmp(argv[2], "%")) 28 | ) 29 | { 30 | printf("Error\n"); 31 | exit(99); 32 | } 33 | 34 | if ((strcmp(argv[2], "/") == 0 || strcmp(argv[2], "%") == 0) 35 | && atoi(argv[3]) == 0) 36 | { 37 | printf("Error\n"); 38 | exit(100); 39 | } 40 | 41 | printf("%d\n", (*get_op_func(argv[2]))(atoi(argv[1]), atoi(argv[3]))); 42 | 43 | return (0); 44 | } 45 | -------------------------------------------------------------------------------- /0x08-recursion/100-is_palindrome.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * long_1 - main - check the code 5 | * @s: number 6 | * 7 | * Return: Always 0. 8 | */ 9 | 10 | int long_1(char *s) 11 | { 12 | if (*s != '\0') 13 | { 14 | return (1 + long_1(s + 1)); 15 | } 16 | return (0); 17 | 18 | } 19 | /** 20 | * compare - main - check the code 21 | * @s: number 22 | * @l: lenght 23 | * 24 | * Return: Always 0. 25 | */ 26 | int compare(char *s, int l) 27 | { 28 | if (l <= 0) 29 | { 30 | return (1); 31 | } 32 | if (*s == *(s + (l - 1))) 33 | { 34 | return (compare(s + 1, l - 2)); 35 | } 36 | else 37 | { 38 | return (0); 39 | } 40 | } 41 | 42 | /** 43 | * is_palindrome - main - check the code 44 | * @s: character 45 | * 46 | * Return: Always 0. 47 | */ 48 | int is_palindrome(char *s) 49 | { 50 | int l; 51 | 52 | l = long_1(s); 53 | return (compare(s, l)); 54 | } 55 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/104-print_buffer.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * print_buffer - prints buffer 6 | * @b: buffer 7 | * @size: size 8 | * Return: void 9 | */ 10 | 11 | void print_buffer(char *b, int size) 12 | { 13 | int o, j, i; 14 | 15 | o = 0; 16 | 17 | if (size <= 0) 18 | { 19 | printf("\n"); 20 | return; 21 | } 22 | while (o < size) 23 | { 24 | j = size - o < 10 ? size - o : 10; 25 | printf("%08x: ", o); 26 | for (i = 0; i < 10; i++) 27 | { 28 | if (i < j) 29 | printf("%02x", *(b + o + i)); 30 | else 31 | printf(" "); 32 | if (i % 2) 33 | { 34 | printf(" "); 35 | } 36 | } 37 | for (i = 0; i < j; i++) 38 | { 39 | int c = *(b + o + i); 40 | 41 | if (c < 32 || c > 132) 42 | { 43 | c = '.'; 44 | } 45 | printf("%c", c); 46 | } 47 | printf("\n"); 48 | o += 10; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /0x0B-malloc_free/100-argstostr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | *argstostr - concatenates all arguments of the program. 5 | *@ac: argument count. 6 | *@av: pointer to array of size ac. 7 | *Return: NULL if ac == 0 or av == null, Pointer to new string. 8 | *NULL on fail. 9 | */ 10 | char *argstostr(int ac, char **av) 11 | { 12 | int i, n, k = 0, len = 0; 13 | char *str; 14 | 15 | if (ac == 0 || av == NULL) 16 | return (NULL); 17 | 18 | for (i = 0; i < ac; i++) 19 | { 20 | for (n = 0; av[i][n]; n++) 21 | len++; 22 | } 23 | len += ac; 24 | 25 | str = malloc(sizeof(char) * len + 1); 26 | if (str == NULL) 27 | return (NULL); 28 | 29 | for (i = 0; i < ac; i++) 30 | { 31 | for (n = 0; av[i][n]; n++) 32 | { 33 | str[k] = av[i][n]; 34 | k++; 35 | } 36 | if (str[k] == '\0') 37 | { 38 | str[k++] = '\n'; 39 | } 40 | } 41 | return (str); 42 | } 43 | -------------------------------------------------------------------------------- /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 to copy 7 | * @s2: second string to copy 8 | * @n: number of bytes of s2 to copy 9 | * 10 | * Return: char pointer to newly allocated place in memory 11 | */ 12 | char *string_nconcat(char *s1, char *s2, unsigned int n) 13 | { 14 | unsigned int i, j, k; 15 | char *s; 16 | 17 | if (s1 == NULL) 18 | i = 0; 19 | else 20 | { 21 | for (i = 0; s1[i]; i++) 22 | ; 23 | } 24 | if (s2 == NULL) 25 | j = 0; 26 | else 27 | { 28 | for (j = 0; s2[j]; j++) 29 | ; 30 | } 31 | if (j > n) 32 | j = n; 33 | s = malloc(sizeof(char) * (i + j + 1)); 34 | if (s == NULL) 35 | return (NULL); 36 | for (k = 0; k < i; k++) 37 | s[k] = s1[k]; 38 | for (k = 0; k < j; k++) 39 | s[k + i] = s2[k]; 40 | s[i + j] = '\0'; 41 | return (s); 42 | } 43 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/0-linear.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * linear_search - Searches for a value in an array 5 | * of integers using linear 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 linear_search(int *array, size_t size, int value) 16 | { 17 | size_t i; 18 | 19 | if (array == NULL) 20 | return (-1); 21 | 22 | for (i = 0; i < size; i++) 23 | { 24 | printf("Value checked array[%ld] = [%d]\n", i, array[i]); 25 | if (array[i] == value) 26 | return (i); 27 | } 28 | 29 | return (-1); 30 | } 31 | -------------------------------------------------------------------------------- /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 % 100 == 0 && year % 400 == 0) || (year % 4 == 0)) 16 | { 17 | if (month > 2 && day >= 60) 18 | { 19 | day++; 20 | } 21 | printf("Day of the year: %d\n", day); 22 | printf("Remaining days: %d\n", 366 - day); 23 | } 24 | else 25 | { 26 | if (month == 2 && day == 60) 27 | { 28 | printf("Invalid date: %02d/%02d/%04d\n", month, day - 31, year); 29 | } 30 | else 31 | { 32 | printf("Day of the year: %d\n", day); 33 | printf("Remaining days: %d\n", 365 - day); 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /0x0B-malloc_free/1-strdup.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | *_strdup - returns a pointer to a newly alloctaed 5 | *space in memory which contains a copy of the string 6 | *passed. 7 | *@str: pointer to string being duplicated. 8 | * 9 | *Return: NULL if str is NULL. 10 | *pointer to duplicated string on success. 11 | *NULL if memory was insufficient. 12 | */ 13 | char *_strdup(char *str) 14 | { 15 | char *nstr; 16 | unsigned int len, i; 17 | 18 | /* check is str is null */ 19 | if (str == NULL) 20 | { 21 | return (NULL); 22 | } 23 | 24 | len = 0; 25 | while (str[len] != '\0') 26 | { 27 | len++; 28 | } 29 | 30 | nstr = malloc(sizeof(char) * (len + 1)); 31 | 32 | /*check if malloc was successful*/ 33 | if (nstr == NULL) 34 | { 35 | return (NULL); 36 | } 37 | 38 | for (i = 0; i < len; i++) 39 | { 40 | nstr[i] = str[i]; 41 | } 42 | nstr[len] = '\0'; 43 | return (nstr); 44 | 45 | } 46 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x0F-function_pointers/100-main_opcodes.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * print_opcodes - print the opcodes of this program 6 | * @a: address of the main function 7 | * @n: number of bytes to print 8 | * 9 | * Return: void 10 | **/ 11 | void print_opcodes(char *a, int n) 12 | { 13 | int i; 14 | 15 | for (i = 0; i < n; i++) 16 | { 17 | printf("%.2hhx", a[i]); 18 | if (i < n - 1) 19 | printf(" "); 20 | } 21 | printf("\n"); 22 | 23 | } 24 | 25 | /** 26 | * main - prints the opcodes of its own main function 27 | * @argc: number of arguments passed to the function 28 | * @argv: array of pointers to arguments 29 | * 30 | * Return: always O 31 | */ 32 | int main(int argc, char **argv) 33 | { 34 | int n; 35 | 36 | if (argc != 2) 37 | { 38 | printf("Error\n"); 39 | exit(1); 40 | } 41 | n = atoi(argv[1]); 42 | if (n < 0) 43 | { 44 | printf("Error\n"); 45 | exit(2); 46 | } 47 | print_opcodes((char *)&main, n); 48 | return (0); 49 | } 50 | -------------------------------------------------------------------------------- /0x0A-argc_argv/100-change.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * main - prints the function. 6 | * @argc: counts the arguments of the function. 7 | * @argv: gives the value of the arguments passed to the function. 8 | * 9 | * Return: Return always success. 10 | **/ 11 | 12 | int main(int argc, char *argv[]) 13 | { 14 | int cents, coins = 0; 15 | 16 | if (argc == 2) 17 | { 18 | cents = atoi(*(argv + 1)); 19 | while (cents > 0) 20 | { 21 | if (cents % 25 < cents) 22 | { 23 | cents -= 25; 24 | coins++; 25 | } 26 | else if (cents % 10 < cents) 27 | { 28 | cents -= 10; 29 | coins++; 30 | } 31 | else if (cents % 5 < cents) 32 | { 33 | cents -= 5; 34 | coins++; 35 | } 36 | else if (cents % 2 < cents) 37 | { 38 | cents -= 2; 39 | coins++; } 40 | else if (cents % 1 < cents) 41 | { 42 | cents -= 1; 43 | coins++; 44 | } 45 | } 46 | } 47 | else 48 | { 49 | printf("Error\n"); 50 | return (1); 51 | } 52 | printf("%d\n", coins); 53 | return (0); 54 | } 55 | 56 | 57 | 58 | -------------------------------------------------------------------------------- /0x1A-hash_tables/3-hash_table_set.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | /** 3 | *hash_table_set - adds an element to the hash table 4 | *@ht: pointer to the hash 5 | *@key: pointer to the key 6 | *@value: value to add 7 | *Return: 1 if is success or 0 8 | */ 9 | int hash_table_set(hash_table_t *ht, const char *key, const char *value) 10 | { 11 | unsigned long index, size; 12 | hash_node_t *new_node; 13 | 14 | if (ht == NULL || key == NULL || value == NULL) 15 | return (0); 16 | 17 | size = ht->size; 18 | index = key_index((const unsigned char *)key, size); 19 | 20 | if (ht->array[index] != NULL && strcmp(ht->array[index]->key, key) == 0) 21 | { 22 | ht->array[index]->value = strdup(value); 23 | return (1); 24 | } 25 | 26 | new_node = malloc(sizeof(hash_node_t)); 27 | 28 | if (new_node == NULL) 29 | return (0); 30 | 31 | new_node->key = strdup(key); 32 | new_node->value = strdup(value); 33 | new_node->next = ht->array[index]; 34 | ht->array[index] = new_node; 35 | return (1); 36 | } 37 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-op_functions.c: -------------------------------------------------------------------------------- 1 | /** 2 | * op_add - Adds two numbers 3 | * @a: int 1 4 | * @b: int 2 5 | * Return: Returns result 6 | **/ 7 | int op_add(int a, int b) 8 | { 9 | return (a + b); 10 | } 11 | 12 | /** 13 | * op_sub - subtracts two numbers 14 | * @a: int 1 15 | * @b: int 2 16 | * Return: Returns result 17 | **/ 18 | int op_sub(int a, int b) 19 | { 20 | return (a - b); 21 | } 22 | 23 | /** 24 | * op_mul - Multiplies two numbers 25 | * @a: int 1 26 | * @b: int 2 27 | * Return: Returns result 28 | **/ 29 | 30 | int op_mul(int a, int b) 31 | { 32 | return (a * b); 33 | } 34 | 35 | /** 36 | * op_div - Divides two numbers 37 | * @a: int 1 38 | * @b: int 2 39 | * Return: Returns result 40 | **/ 41 | 42 | int op_div(int a, int b) 43 | { 44 | return (a / b); 45 | } 46 | 47 | /** 48 | * op_mod - Modules two numbers 49 | * @a: int 1 50 | * @b: int 2 51 | * Return: Returns result 52 | **/ 53 | 54 | int op_mod(int a, int b) 55 | { 56 | return (a % b); 57 | } 58 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/8-delete_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * delete_dnodeint_at_index - Deletes the node at given index of a list 5 | * @head: Memory address of the linked list 6 | * @index: Index of the list to search 7 | * 8 | * Return: 1 on success, -1 on failure 9 | */ 10 | int delete_dnodeint_at_index(dlistint_t **head, unsigned int index) 11 | { 12 | dlistint_t *temp = *head, *prev = NULL; 13 | unsigned int i = 0; 14 | 15 | if (head == NULL || temp == NULL) 16 | return (-1); 17 | 18 | if (index == 0) 19 | { 20 | if (temp->next != NULL) 21 | temp->next->prev = NULL; 22 | *head = temp->next; 23 | free(temp); 24 | return (1); 25 | } 26 | 27 | while (temp != NULL && i != index) 28 | { 29 | prev = temp; 30 | temp = temp->next; 31 | i++; 32 | } 33 | if (temp == NULL) 34 | return (-1); 35 | 36 | prev->next = temp->next; 37 | if (temp->next != NULL) 38 | temp->next->prev = prev; 39 | free(temp); 40 | 41 | return (1); 42 | } 43 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/100-realloc.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _realloc - reallocates a memory block using malloc and free. 6 | * @ptr: pointer to previously allocated memory 7 | * @old_size: size of allocated space for ptr 8 | * @new_size: size of newly allocated space 9 | * 10 | * Return: pointer to newly allocated memory, or NULL if failure 11 | */ 12 | void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size) 13 | { 14 | char *p; 15 | unsigned int i, max = new_size; 16 | char *oldp = ptr; 17 | 18 | if (ptr == NULL) 19 | { 20 | p = malloc(new_size); 21 | return (p); 22 | } 23 | else if (new_size == 0) 24 | { 25 | free(ptr); 26 | return (NULL); 27 | } 28 | else if (new_size == old_size) 29 | return (ptr); 30 | 31 | p = malloc(new_size); 32 | if (p == NULL) 33 | return (NULL); 34 | if (new_size > old_size) 35 | max = old_size; 36 | for (i = 0; i < max; i++) 37 | p[i] = oldp[i]; 38 | free(ptr); 39 | return (p); 40 | } 41 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 using 6 | * the Jump search algorithm 7 | * @array: is a pointer to the first element of the array to search 8 | * @size: is the number of elements in array 9 | * @value: is the value to search for 10 | * 11 | * Return: the first index where value is located 12 | */ 13 | int jump_search(int *array, size_t size, int value) 14 | { 15 | size_t step = 0; 16 | size_t i = 0, j = 0; 17 | 18 | if (!array || size == 0) 19 | return (-1); 20 | 21 | step = sqrt(size); 22 | while (j < size && value > array[j]) 23 | { 24 | i = j; 25 | j = i + step; 26 | printf("Value checked array[%ld] = [%d]\n", i, array[i]); 27 | } 28 | printf("Value found between indexes [%ld] and [%ld]\n", i, j); 29 | while (i < size && i <= j) 30 | { 31 | printf("Value checked array[%ld] = [%d]\n", i, array[i]); 32 | if (value == array[i]) 33 | return (i); 34 | i += 1; 35 | } 36 | return (-1); 37 | } 38 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/4-new_dog.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | 4 | /** 5 | * new_dog - creates a new dog 6 | * @name: name of dog 7 | * @age: age of dog 8 | * @owner: owner of dog 9 | * 10 | * Return: pointer to new dog 11 | */ 12 | dog_t *new_dog(char *name, float age, char *owner) 13 | { 14 | unsigned int nl, ol, i; 15 | dog_t *dog; 16 | 17 | if (name == NULL || owner == NULL) 18 | return (NULL); 19 | dog = malloc(sizeof(dog_t)); 20 | if (dog == NULL) 21 | return (NULL); 22 | for (nl = 0; name[nl]; nl++) 23 | ; 24 | nl++; 25 | dog->name = malloc(nl * sizeof(char)); 26 | if (dog->name == NULL) 27 | { 28 | free(dog); 29 | return (NULL); 30 | } 31 | for (i = 0; i < nl; i++) 32 | dog->name[i] = name[i]; 33 | dog->age = age; 34 | for (ol = 0; owner[ol]; ol++) 35 | ; 36 | ol++; 37 | dog->owner = malloc(ol * sizeof(char)); 38 | if (dog->owner == NULL) 39 | { 40 | free(dog->name); 41 | free(dog); 42 | return (NULL); 43 | } 44 | for (i = 0; i < ol; i++) 45 | dog->owner[i] = owner[i]; 46 | return (dog); 47 | } 48 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef _LISTS_H 2 | #define _LISTS_H 3 | 4 | #include 5 | #include 6 | 7 | /** 8 | * struct dlistint_s - doubly linked list 9 | * @n: integer 10 | * @prev: points to the previous node 11 | * @next: points to the next node 12 | * 13 | * Description: doubly linked list node structure 14 | * for Holberton project 15 | */ 16 | typedef struct dlistint_s 17 | { 18 | int n; 19 | struct dlistint_s *prev; 20 | struct dlistint_s *next; 21 | } dlistint_t; 22 | 23 | size_t print_dlistint(const dlistint_t *h); 24 | size_t dlistint_len(const dlistint_t *h); 25 | dlistint_t *add_dnodeint(dlistint_t **head, const int n); 26 | dlistint_t *add_dnodeint_end(dlistint_t **head, const int n); 27 | void free_dlistint(dlistint_t *head); 28 | dlistint_t *get_dnodeint_at_index(dlistint_t *head, unsigned int index); 29 | int sum_dlistint(dlistint_t *head); 30 | dlistint_t *insert_dnodeint_at_index(dlistint_t **h, unsigned int idx, int n); 31 | int delete_dnodeint_at_index(dlistint_t **head, unsigned int index); 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/8-24_hours.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * jack_bauer - prints every minute of the day 5 | * 6 | * Description: prints every minute of the day 7 | * 8 | * Return: void 9 | */ 10 | 11 | void jack_bauer(void) 12 | { 13 | int hours_tens, hours_ones, minutes_tens, minutes_ones, hours_max; 14 | 15 | hours_max = 58; 16 | hours_tens = '0'; 17 | while (hours_tens < '3') 18 | { 19 | if (hours_tens == '2') 20 | { 21 | hours_max = '4'; 22 | } 23 | hours_ones = '0'; 24 | while (hours_ones < hours_max) 25 | { 26 | minutes_tens = '0'; 27 | while (minutes_tens < '6') 28 | { 29 | minutes_ones = '0'; 30 | while (minutes_ones < 58) 31 | { 32 | _putchar(hours_tens); 33 | _putchar(hours_ones); 34 | _putchar(':'); 35 | _putchar(minutes_tens); 36 | _putchar(minutes_ones); 37 | _putchar('\n'); 38 | minutes_ones++; 39 | } 40 | minutes_ones = '0'; 41 | minutes_tens++; 42 | } 43 | minutes_tens = '0'; 44 | hours_ones++; 45 | } 46 | hours_ones = '0'; 47 | hours_tens++; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/102-interpolation.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * interpolation_search - pointer to the first element of the array to search 5 | * @array: is a pointer to the first element of the array to search 6 | * @size: is the number of elements in array 7 | * @value: is the value to search for 8 | * 9 | * Return: the first index where value is located 10 | */ 11 | int interpolation_search(int *array, size_t size, int value) 12 | { 13 | size_t high = size - 1, low = 0, pos = 0; 14 | 15 | if (array) 16 | { 17 | while (array[low] != array[high]) 18 | { 19 | pos = low + (((double)(high - low) 20 | / (array[high] - array[low])) 21 | * (value - array[low])); 22 | if (pos < low || pos > high) 23 | { 24 | printf("Value checked array[%lu] is out of range\n", pos); 25 | break; 26 | } 27 | printf("Value checked array[%lu] = [%d]\n", pos, array[pos]); 28 | if (array[pos] < value) 29 | low = pos + 1; 30 | else if (array[pos] > value) 31 | high = pos - 1; 32 | else 33 | return (pos); 34 | } 35 | } 36 | return (-1); 37 | } 38 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/main.c: -------------------------------------------------------------------------------- 1 | /** 2 | * simple_print_buffer - prints buffer in hexa 3 | * @buffer: the address of memory to print 4 | * @size: the size of the memory to print 5 | * 6 | * Return: Nothing. 7 | */ 8 | void simple_print_buffer(char *buffer, unsigned int size) 9 | { 10 | unsigned int i; 11 | 12 | i = 0; 13 | while (i < size) 14 | { 15 | if (i % 10) 16 | { 17 | printf(" "); 18 | } 19 | if (!(i % 10) && i) 20 | { 21 | printf("\n"); 22 | } 23 | printf("0x%02x", buffer[i]); 24 | i++; 25 | } 26 | printf("\n"); 27 | } 28 | 29 | /** 30 | * main - check the code 31 | * 32 | * Return: Always 0. 33 | */ 34 | int main(void) 35 | { 36 | char buffer[98] = {0x00}; 37 | 38 | simple_print_buffer(buffer, 98); 39 | _memset(buffer, 0x01, 95); 40 | printf("-------------------------------------------------\n"); 41 | simple_print_buffer(buffer, 98); 42 | return (0); 43 | } 44 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/7-insert_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * insert_dnodeint_at_index - Insert a new node in the given index 5 | * @h: Memory address of the head list 6 | * @idx: Index of the list 7 | * @n: New data (int) 8 | * 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 *new = NULL; 14 | dlistint_t *head = *h, *prev = *h; 15 | unsigned int i; 16 | 17 | if (h == NULL || (head == NULL && idx > 0)) 18 | return (NULL); 19 | if (idx == 0) 20 | return (add_dnodeint(h, n)); 21 | 22 | head = head->next; 23 | for (i = 1; i < idx; i++) 24 | { 25 | if (prev->next == NULL) 26 | return (NULL); 27 | head = head->next; 28 | prev = prev->next; 29 | } 30 | if (head == NULL) 31 | return (add_dnodeint_end(h, n)); 32 | 33 | new = malloc(sizeof(dlistint_t)); 34 | if (new == NULL) 35 | return (NULL); 36 | new->n = n; 37 | new->next = head; 38 | new->prev = prev; 39 | prev->next = new; 40 | if (head != NULL) 41 | head->prev = new; 42 | return (new); 43 | } 44 | -------------------------------------------------------------------------------- /0x0B-malloc_free/main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * simple_print_buffer - prints buffer in hexa 7 | * @buffer: the address of memory to print 8 | * @size: the size of the memory to print 9 | * 10 | * Return: Nothing. 11 | */ 12 | void simple_print_buffer(char *buffer, unsigned int size) 13 | { 14 | unsigned int i; 15 | 16 | i = 0; 17 | while (i < size) 18 | { 19 | if (i % 10) 20 | { 21 | printf(" "); 22 | } 23 | if (!(i % 10) && i) 24 | { 25 | printf("\n"); 26 | } 27 | printf("0x%02x", buffer[i]); 28 | i++; 29 | } 30 | printf("\n"); 31 | } 32 | 33 | /** 34 | * main - check the code for ALX School students. 35 | * 36 | * Return: Always 0. 37 | */ 38 | int main(void) 39 | { 40 | char *buffer; 41 | 42 | buffer = create_array(98, 'H'); 43 | if (buffer == NULL) 44 | { 45 | printf("failed to allocate memory\n"); 46 | return (1); 47 | } 48 | simple_print_buffer(buffer, 98); 49 | free(buffer); 50 | return (0); 51 | } 52 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/1-binary.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * binary_search - Searches for a value in a sorted array 5 | * of integers using binary search. 6 | * @array: A pointer to the first element of the array to search. 7 | * @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 index where the value is located. 12 | * 13 | * Description: Prints the [sub]array being searched after each change. 14 | */ 15 | int binary_search(int *array, size_t size, int value) 16 | { 17 | size_t i, left, right; 18 | 19 | if (array == NULL) 20 | return (-1); 21 | 22 | for (left = 0, right = size - 1; right >= left;) 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) 31 | return (i); 32 | if (array[i] > value) 33 | right = i - 1; 34 | else 35 | left = i + 1; 36 | } 37 | 38 | return (-1); 39 | } 40 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/106-linear_skip.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * linear_skip - searches for a value in a sorted skip list of integers. 5 | * @list: is a pointer to the head of the skip list to search 6 | * @value: is the value to search for 7 | * 8 | * Return: a pointer on the first node where value is located 9 | */ 10 | skiplist_t *linear_skip(skiplist_t *list, int value) 11 | { 12 | skiplist_t *tmp = NULL; 13 | 14 | if (list) 15 | { 16 | while (value > list->n && list->next) 17 | { 18 | tmp = list; 19 | list = list->express; 20 | if (!list) 21 | { 22 | list = tmp; 23 | while (list->next) 24 | list = list->next; 25 | } 26 | if (list != tmp->express) 27 | break; 28 | printf("Value checked at index [%ld] = [%d]\n", list->index, 29 | list->n); 30 | } 31 | printf("Value found between indexes [%ld] and [%ld]\n", 32 | tmp->index, list->index); 33 | while (tmp && tmp->index <= list->index) 34 | { 35 | printf("Value checked at index [%ld] = [%d]\n", tmp->index, tmp->n); 36 | if (value == tmp->n) 37 | return (tmp); 38 | tmp = tmp->next; 39 | } 40 | } 41 | return (NULL); 42 | } 43 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/105-jump_list.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | #include 3 | 4 | /** 5 | * jump_list - searches for a value in a sorted list of integers using 6 | * the Jump search algorithm 7 | * @list: is a pointer to the first element of the array to search 8 | * @size: is the number of elements in array 9 | * @value: is the value to search for 10 | * 11 | * Return: the first index where value is located 12 | */ 13 | listint_t *jump_list(listint_t *list, size_t size, int value) 14 | { 15 | int step = 0, i = 0; 16 | listint_t *tmp = NULL; 17 | 18 | if (list && size > 0) 19 | { 20 | step = sqrt(size); 21 | while (value > list->n && list->next) 22 | { 23 | tmp = list; 24 | for (i = 0; i < step && list->next; i++) 25 | list = list->next; 26 | printf("Value checked at index [%ld] = [%d]\n", list->index, 27 | list->n); 28 | } 29 | printf("Value found between indexes [%ld] and [%ld]\n", 30 | tmp->index, list->index); 31 | while (tmp && tmp->index <= list->index) 32 | { 33 | printf("Value checked at index [%ld] = [%d]\n", tmp->index, tmp->n); 34 | if (value == tmp->n) 35 | return (tmp); 36 | tmp = tmp->next; 37 | } 38 | } 39 | return (NULL); 40 | } 41 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/103-keygen.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /** 6 | * main - generate a key depending on a username for crackme5 7 | * @argc: number of arguments passed 8 | * @argv: arguments passed to main 9 | * 10 | * Return: 0 on success, 1 on error 11 | */ 12 | int main(int argc, char *argv[]) 13 | { 14 | unsigned int i, b; 15 | size_t len, add; 16 | char *l = "A-CHRDw87lNS0E9B2TibgpnMVys5XzvtOGJcYLU+4mjW6fxqZeF3Qa1rPhdKIouk"; 17 | char p[7] = " "; 18 | 19 | if (argc != 2) 20 | { 21 | printf("Correct usage: ./keygen5 username\n"); 22 | return (1); 23 | } 24 | len = strlen(argv[1]); 25 | p[0] = l[(len ^ 59) & 63]; 26 | for (i = 0, add = 0; i < len; i++) 27 | add += argv[1][i]; 28 | p[1] = l[(add ^ 79) & 63]; 29 | for (i = 0, b = 1; i < len; i++) 30 | b *= argv[1][i]; 31 | p[2] = l[(b ^ 85) & 63]; 32 | for (b = argv[1][0], i = 0; i < len; i++) 33 | if ((char)b <= argv[1][i]) 34 | b = argv[1][i]; 35 | srand(b ^ 14); 36 | p[3] = l[rand() & 63]; 37 | for (b = 0, i = 0; i < len; i++) 38 | b += argv[1][i] * argv[1][i]; 39 | p[4] = l[(b ^ 239) & 63]; 40 | for (b = 0, i = 0; (char)i < argv[1][0]; i++) 41 | b = rand(); 42 | p[5] = l[(b ^ 229) & 63]; 43 | printf("%s\n", p); 44 | return (0); 45 | } 46 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/README.md: -------------------------------------------------------------------------------- 1 | 0x13. C - More singly linked list 2 | # Files 3 | All of the following files are programs written in C: 4 | 5 | ## Filename Description 6 | 7 | 0-print_listint.c - Prints all the elements of a listint_t list 8 | 9 | 1-listint_len.c - Returns the number of elements in a linked listint_t list 10 | 11 | 2-add_nodeint.c - Adds a new node at the beginning of a listint_t list 12 | 13 | 3-add_nodeint_end.c - Adds a new node at the end of a listint_t list 14 | 15 | 4-free_listint.c - Frees a listint_t list 16 | 17 | 5-free_listint2.c - Frees a listint_t list 18 | 19 | 6-pop_listint.c - Deletes the head node of a listint_t linked list, and returns the head node's data (n) 20 | 21 | 7-get_nodeint.c - Returns the nth node of a listint_t linked list 22 | 23 | 8-sum_listint.c - Returns the sum of all the data (n) of a listint_t linked list 24 | 25 | 9-insert_nodeint.c - Inserts a new node at a given position 26 | 27 | 10-delete_nodeint.c - Deletes the node at index index of a listint_t linked list 28 | 29 | 100-reverse_listint.c - Reverses a listint_t linked list 30 | 31 | 101-print_listint_safe.c - Prints a listint_t linked list 32 | 33 | 102-free_listint_safe.c - Frees a listint_t list 34 | 35 | 103-find_loop.c - Finds the loop in a linked lists 36 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/100-times_table.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_times_table - prints the times table 5 | * @n: integer for which the times table will be printed 6 | * 7 | * Description: prints the times table 8 | * 9 | * Return: void 10 | */ 11 | 12 | void print_times_table(int n) 13 | { 14 | int row, column, product; 15 | 16 | if (n >= 0 && n < 15) 17 | { 18 | for (row = 0; row <= n; row++) 19 | { 20 | for (column = 0; column <= n; column++) 21 | { 22 | product = row * column; 23 | 24 | if (column == 0) 25 | _putchar('0'); 26 | else if (product < 10) 27 | { 28 | _putchar(','); 29 | _putchar(' '); 30 | _putchar(' '); 31 | _putchar(' '); 32 | _putchar(product % 10 + '0'); 33 | } 34 | else if (product >= 10 && product < 100) 35 | { 36 | _putchar(','); 37 | _putchar(' '); 38 | _putchar(' '); 39 | _putchar((product / 10) % 10 + '0'); 40 | _putchar(product % 10 + '0'); 41 | } 42 | else if (product > 99 && product < 1000) 43 | { 44 | _putchar(','); 45 | _putchar(' '); 46 | _putchar(product / 100 + '0'); 47 | _putchar((product / 10) % 10 + '0'); 48 | _putchar(product % 10 + '0'); 49 | } 50 | } 51 | _putchar('\n'); 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x1A-hash_tables/hash_tables.h: -------------------------------------------------------------------------------- 1 | #ifndef _HASH_TABLES_H_ 2 | #define _HASH_TABLES_H_ 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | /** 9 | * struct hash_node_s - Node of a hash table 10 | * 11 | * @key: the key, string 12 | * The key is unique in the HashTable 13 | * @value: The value corresponding to a key 14 | * @next: A pointer to the next node of the List 15 | */ 16 | typedef struct hash_node_s 17 | { 18 | char *key; 19 | char *value; 20 | struct hash_node_s *next; 21 | } hash_node_t; 22 | 23 | /** 24 | * struct hash_table_s - Hash Table data structure 25 | * 26 | * @size: The size of the array 27 | * @array: An array of size @size 28 | * Each cell of this array is a pointer to the first node of a linked list, 29 | * because we want our HashTable to use a Chaining collision handling 30 | */ 31 | typedef struct hash_table_s 32 | { 33 | unsigned long int size; 34 | hash_node_t **array; 35 | } hash_table_t; 36 | 37 | hash_table_t *hash_table_create(unsigned long int size); 38 | unsigned long int hash_djb2(const unsigned char *str); 39 | unsigned long int key_index(const unsigned char *key, unsigned long int size); 40 | int hash_table_set(hash_table_t *ht, const char *key, const char *value); 41 | char *hash_table_get(const hash_table_t *ht, const char *key); 42 | void hash_table_print(const hash_table_t *ht); 43 | void hash_table_delete(hash_table_t *ht); 44 | 45 | #endif 46 | -------------------------------------------------------------------------------- /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 | * 10 | * @n: Integer 11 | * @index: Index of the node in the list 12 | * @next: Pointer to the next node 13 | * 14 | * Description: singly linked list node structure 15 | */ 16 | typedef struct listint_s 17 | { 18 | int n; 19 | size_t index; 20 | struct listint_s *next; 21 | } listint_t; 22 | 23 | /** 24 | * struct skiplist_s - Singly linked list with an express lane 25 | * 26 | * @n: Integer 27 | * @index: Index of the node in the list 28 | * @next: Pointer to the next node 29 | * @express: Pointer to the next node in the express lane 30 | * 31 | * Description: singly linked list node structure with an express lane 32 | */ 33 | typedef struct skiplist_s 34 | { 35 | int n; 36 | size_t index; 37 | struct skiplist_s *next; 38 | struct skiplist_s *express; 39 | } skiplist_t; 40 | 41 | int linear_search(int *array, size_t size, int value); 42 | int binary_search(int *array, size_t size, int value); 43 | int jump_search(int *array, size_t size, int value); 44 | int interpolation_search(int *array, size_t size, int value); 45 | int exponential_search(int *array, size_t size, int value); 46 | int advanced_binary(int *array, size_t size, int value); 47 | listint_t *jump_list(listint_t *list, size_t size, int value); 48 | skiplist_t *linear_skip(skiplist_t *list, int value); 49 | 50 | 51 | #endif /* SEARCH_ALGO_H */ 52 | -------------------------------------------------------------------------------- /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 Holberton 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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/103-infinite_add.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * rev_string - reverse array 5 | * @n: integer params 6 | * Return: 0 7 | */ 8 | 9 | void rev_string(char *n) 10 | { 11 | int i = 0; 12 | int j = 0; 13 | char temp; 14 | 15 | while (*(n + i) != '\0') 16 | { 17 | i++; 18 | } 19 | i--; 20 | 21 | for (j = 0; j < i; j++, i--) 22 | { 23 | temp = *(n + j); 24 | *(n + j) = *(n + i); 25 | *(n + i) = temp; 26 | } 27 | } 28 | 29 | /** 30 | * infinite_add - add 2 numbers together 31 | * @n1: text representation of 1st number to add 32 | * @n2: text representation of 2nd number to add 33 | * @r: pointer to buffer 34 | * @size_r: buffer size 35 | * Return: pointer to calling function 36 | */ 37 | 38 | char *infinite_add(char *n1, char *n2, char *r, int size_r) 39 | { 40 | int overflow = 0, i = 0, j = 0, digits = 0; 41 | int val1 = 0, val2 = 0, temp_tot = 0; 42 | 43 | while (*(n1 + i) != '\0') 44 | i++; 45 | while (*(n2 + j) != '\0') 46 | j++; 47 | i--; 48 | j--; 49 | if (j >= size_r || i >= size_r) 50 | return (0); 51 | while (j >= 0 || i >= 0 || overflow == 1) 52 | { 53 | if (i < 0) 54 | val1 = 0; 55 | else 56 | val1 = *(n1 + i) - '0'; 57 | if (j < 0) 58 | val2 = 0; 59 | else 60 | val2 = *(n2 + j) - '0'; 61 | temp_tot = val1 + val2 + overflow; 62 | if (temp_tot >= 10) 63 | overflow = 1; 64 | else 65 | overflow = 0; 66 | if (digits >= (size_r - 1)) 67 | return (0); 68 | *(r + digits) = (temp_tot % 10) + '0'; 69 | digits++; 70 | j--; 71 | i--; 72 | } 73 | if (digits == size_r) 74 | return (0); 75 | *(r + digits) = '\0'; 76 | rev_string(r); 77 | return (r); 78 | } 79 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x1C-makefiles/school.c: -------------------------------------------------------------------------------- 1 | #include "m.h" 2 | 3 | /** 4 | * * print_school 5 | * * 6 | * * Return: Nothing 7 | * */ 8 | void print_school(void) 9 | { 10 | printf("j#0000000000000000000000000000000000000\n"); 11 | printf("j#000000000000000000@Q**g00000000000000\n"); 12 | printf("j#0000000000000000*]++]4000000000000000\n"); 13 | printf("j#000000000000000k]++]++*N#000000000000\n"); 14 | printf("j#0000000000000*C+++]++]++]J*0000000000\n"); 15 | printf("j#00000000000@+]++qwwwp=]++++]*00000000\n"); 16 | printf("j#0000000000*+++]q#0000k+]+]++]4#000000\n"); 17 | printf("j#00000000*C+]+]w#0000*]+++]+]++0000000\n"); 18 | printf("j#0000we+]wW000***C++]++]+]++++40000000\n"); 19 | printf("j#000000000*C+]+]]+]++]++]++]+q#0000000\n"); 20 | printf("j#0000000*]+]+++++++]++]+++]+++J0000000\n"); 21 | printf("j#000000C++]=]+]+]+]++]++]+]+]+]=000000\n"); 22 | printf("j#00000k+]++]+++]+]++qwW0000000AgW00000\n"); 23 | printf("j#00000k++]++]+]+++qW#00000000000000000\n"); 24 | printf("j#00000A]++]++]++]++J**0000000000000000\n"); 25 | printf("j#000000e]++]+++]++]++]J000000000000000\n"); 26 | printf("j#0000000A]++]+]++]++]++000000000000000\n"); 27 | printf("j#000000000w]++]+]++]+qW#00000000000000\n"); 28 | printf("j#00000000000w]++++]*0##000000000000000\n"); 29 | printf("j#0000000000000Ag]+]++*0000000000000000\n"); 30 | printf("j#00000000000000000we]+]Q00000000000000\n"); 31 | printf("j#0000000000000@@+wgdA]+J00000000000000\n"); 32 | printf("j#0000000000000k?qwgdC=]4#0000000000000\n"); 33 | printf("j#00000000000000w]+]++qw#00000000000000\n"); 34 | printf("\"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"); 35 | } 36 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/104-advanced_binary.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * printArray - print array 5 | * @array: pointer to the first element of the array 6 | * @start: index to start 7 | * @end: index to end 8 | * 9 | * Return: Nothing 10 | */ 11 | void printArray(int *array, size_t start, size_t end) 12 | { 13 | size_t j = 0; 14 | 15 | printf("Searching in array: "); 16 | for (j = start; j <= end; j++) 17 | { 18 | printf("%d", array[j]); 19 | if (j != end) 20 | { 21 | putchar(44); 22 | putchar(32); 23 | } 24 | } 25 | putchar(10); 26 | } 27 | 28 | /** 29 | * binary_search_v3 - searches for a value in an array of integers using 30 | * the Linear search algorithm with recursion 31 | * @array: pointer to the first element of the array 32 | * @value: value to search for 33 | * @start: index to start 34 | * @end: index to end 35 | * 36 | * Return: the first index where value is located 37 | */ 38 | int binary_search_v3(int *array, int value, size_t start, size_t end) 39 | { 40 | size_t mid = 0; 41 | 42 | if (start < end) 43 | { 44 | mid = (start + end) / 2; 45 | printArray(array, start, end); 46 | if (value == array[mid] && 47 | (array[mid - 1] != value)) 48 | return (mid); 49 | else if (value <= array[mid]) 50 | return (binary_search_v3(array, value, start, mid)); 51 | else if (value > array[mid]) 52 | return (binary_search_v3(array, value, mid + 1, end)); 53 | } 54 | printArray(array, start, end); 55 | if (value == array[end]) 56 | return (end); 57 | return (-1); 58 | } 59 | 60 | /** 61 | * advanced_binary - searches for a value in an array of integers using 62 | * the Corrected Linear search algorithm corrected 63 | * @array: pointer to the first element of the array 64 | * @size: number of elements in array 65 | * @value: value to search for 66 | * 67 | * Return: the first index where value is located 68 | */ 69 | int advanced_binary(int *array, size_t size, int value) 70 | { 71 | if (array && size > 0) 72 | return (binary_search_v3(array, value, 0, size - 1)); 73 | return (-1); 74 | } 75 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/103-exponential.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | #include 3 | 4 | 5 | int binary_search_v2(int *array, int value, size_t start, size_t end); 6 | void printArray(int *array, size_t start, size_t end); 7 | 8 | /** 9 | * printArray - print array 10 | * @array: pointer to the first element of the array 11 | * @start: index to start 12 | * @end: index to end 13 | * 14 | * Return: Nothing 15 | */ 16 | void printArray(int *array, size_t start, size_t end) 17 | { 18 | size_t j = 0; 19 | 20 | printf("Searching in array: "); 21 | for (j = start; j <= end; j++) 22 | { 23 | printf("%d", array[j]); 24 | if (j != end) 25 | { 26 | putchar(44); 27 | putchar(32); 28 | } 29 | } 30 | putchar(10); 31 | } 32 | 33 | /** 34 | * binary_search_v2 - searches for a value in an array of integers using 35 | * the Linear search algorithm 36 | * @array: pointer to the first element of the array 37 | * @value: value to search for 38 | * @start: index to start 39 | * @end: index to end 40 | * 41 | * Return: the first index where value is located 42 | */ 43 | int binary_search_v2(int *array, int value, size_t start, size_t end) 44 | { 45 | size_t mid = 0; 46 | 47 | while (start < end) 48 | { 49 | printArray(array, start, end); 50 | mid = (start + end) / 2; 51 | if (value == array[mid]) 52 | return (mid); 53 | else if (value < array[mid]) 54 | end = mid - 1; 55 | else if (value > array[mid]) 56 | start = mid + 1; 57 | } 58 | printArray(array, start, end); 59 | if (value == array[end]) 60 | return (end); 61 | return (-1); 62 | } 63 | 64 | /** 65 | * exponential_search - Searches for a value in a sorted array of integers 66 | * using the Exponential search algorithm 67 | * @array: is a pointer to the first element of the array to search 68 | * @size: is the number of elements in array 69 | * @value: is the value to search for 70 | * 71 | * Return: the first index where value is located 72 | */ 73 | int exponential_search(int *array, size_t size, int value) 74 | { 75 | size_t tmp = 1, start = 0, end = size, save = 0; 76 | 77 | if (array && size > 0) 78 | { 79 | if (array[0] == value) 80 | return (0); 81 | while (tmp < end && array[tmp] <= value) 82 | { 83 | start = tmp; 84 | printf("Value checked array[%ld] = [%d]\n", start, array[tmp]); 85 | tmp *= 2; 86 | } 87 | save = tmp < end ? tmp : end - 1; 88 | printf("Value found between indexes [%ld] and [%ld]\n", start, save); 89 | if (array[start] == value) 90 | return (start); 91 | if (array[save] == value) 92 | return (save); 93 | return (binary_search_v2(array, value, start, save)); 94 | } 95 | return (-1); 96 | } 97 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/101-mul.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _print - moves a string one place to the left and prints the string 6 | * @str: string to move 7 | * @l: size of string 8 | * 9 | * Return: void 10 | */ 11 | void _print(char *str, int l) 12 | { 13 | int i, j; 14 | 15 | i = j = 0; 16 | while (i < l) 17 | { 18 | if (str[i] != '0') 19 | j = 1; 20 | if (j || i == l - 1) 21 | _putchar(str[i]); 22 | i++; 23 | } 24 | 25 | _putchar('\n'); 26 | free(str); 27 | } 28 | 29 | /** 30 | * mul - multiplies a char with a string and places the answer into dest 31 | * @n: char to multiply 32 | * @num: string to multiply 33 | * @num_index: last non NULL index of num 34 | * @dest: destination of multiplication 35 | * @dest_index: highest index to start addition 36 | * 37 | * Return: pointer to dest, or NULL on failure 38 | */ 39 | char *mul(char n, char *num, int num_index, char *dest, int dest_index) 40 | { 41 | int j, k, mul, mulrem, add, addrem; 42 | 43 | mulrem = addrem = 0; 44 | for (j = num_index, k = dest_index; j >= 0; j--, k--) 45 | { 46 | mul = (n - '0') * (num[j] - '0') + mulrem; 47 | mulrem = mul / 10; 48 | add = (dest[k] - '0') + (mul % 10) + addrem; 49 | addrem = add / 10; 50 | dest[k] = add % 10 + '0'; 51 | } 52 | for (addrem += mulrem; k >= 0 && addrem; k--) 53 | { 54 | add = (dest[k] - '0') + addrem; 55 | addrem = add / 10; 56 | dest[k] = add % 10 + '0'; 57 | } 58 | if (addrem) 59 | { 60 | return (NULL); 61 | } 62 | return (dest); 63 | } 64 | /** 65 | * check_for_digits - checks the arguments to ensure they are digits 66 | * @av: pointer to arguments 67 | * 68 | * Return: 0 if digits, 1 if not 69 | */ 70 | int check_for_digits(char **av) 71 | { 72 | int i, j; 73 | 74 | for (i = 1; i < 3; i++) 75 | { 76 | for (j = 0; av[i][j]; j++) 77 | { 78 | if (av[i][j] < '0' || av[i][j] > '9') 79 | return (1); 80 | } 81 | } 82 | return (0); 83 | } 84 | 85 | /** 86 | * init - initializes a string 87 | * @str: sting to initialize 88 | * @l: length of strinf 89 | * 90 | * Return: void 91 | */ 92 | void init(char *str, int l) 93 | { 94 | int i; 95 | 96 | for (i = 0; i < l; i++) 97 | str[i] = '0'; 98 | str[i] = '\0'; 99 | } 100 | 101 | /** 102 | * main - multiply two numbers 103 | * @argc: number of arguments 104 | * @argv: argument vector 105 | * 106 | * Return: zero, or exit status of 98 if failure 107 | */ 108 | int main(int argc, char *argv[]) 109 | { 110 | int l1, l2, ln, ti, i; 111 | char *a; 112 | char *t; 113 | char e[] = "Error\n"; 114 | 115 | if (argc != 3 || check_for_digits(argv)) 116 | { 117 | for (ti = 0; e[ti]; ti++) 118 | _putchar(e[ti]); 119 | exit(98); 120 | } 121 | for (l1 = 0; argv[1][l1]; l1++) 122 | ; 123 | for (l2 = 0; argv[2][l2]; l2++) 124 | ; 125 | ln = l1 + l2 + 1; 126 | a = malloc(ln * sizeof(char)); 127 | if (a == NULL) 128 | { 129 | for (ti = 0; e[ti]; ti++) 130 | _putchar(e[ti]); 131 | exit(98); 132 | } 133 | init(a, ln - 1); 134 | for (ti = l2 - 1, i = 0; ti >= 0; ti--, i++) 135 | { 136 | t = mul(argv[2][ti], argv[1], l1 - 1, a, (ln - 2) - i); 137 | if (t == NULL) 138 | { 139 | for (ti = 0; e[ti]; ti++) 140 | _putchar(e[ti]); 141 | free(a); 142 | exit(98); 143 | } 144 | } 145 | _print(a, ln - 1); 146 | return (0); 147 | } 148 | -------------------------------------------------------------------------------- /0x1A-hash_tables/100-sorted_hash_table.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * shash_table_create - creates a hash table 5 | * @size: size of the array of linked list in the table 6 | * 7 | * Return: shash_table_t struct 8 | */ 9 | shash_table_t *shash_table_create(unsigned long int size) 10 | { 11 | shash_table_t *sht; 12 | 13 | if (size == 0) 14 | return (NULL); 15 | 16 | sht = calloc(1, sizeof(shash_table_t)); 17 | if (!sht) 18 | return (NULL); 19 | 20 | sht->size = size; 21 | sht->array = calloc((size_t)sht->size, sizeof(shash_node_t *)); 22 | if (sht->array == NULL) 23 | { 24 | free(sht); 25 | return (NULL); 26 | } 27 | 28 | return (sht); 29 | } 30 | 31 | /** 32 | * shash_table_set - adds an element to the hash table 33 | * @ht: hash table to add the element to 34 | * @key: key of the element, will give the index in the array 35 | * @value: value of the element to store in the array 36 | * 37 | * Return: 1 on success, 0 otherwise 38 | */ 39 | int shash_table_set(shash_table_t *ht, const char *key, const char *value) 40 | { 41 | unsigned long int index; 42 | shash_node_t *new; 43 | 44 | if (!ht || !key || !strcmp(key, "") || !value) 45 | return (0); 46 | 47 | index = key_index((unsigned char *)key, ht->size); 48 | 49 | if (check_key_s(ht->array[index], key)) 50 | return (replace_value_s(&ht->array[index], key, value)); 51 | new = add_node_s(&ht->array[index], key, value); 52 | if (!new) 53 | return (0); 54 | 55 | insert_sort(new, ht); 56 | return (1); 57 | } 58 | 59 | /** 60 | * insert_sort - inserts a node in a sorted doubly 61 | * linked list 62 | * @node: shash_node_t to insert 63 | * @ht: pointer to the hash table to insert it into 64 | */ 65 | void insert_sort(shash_node_t *node, shash_table_t *ht) 66 | { 67 | shash_node_t *head = ht->shead; 68 | 69 | if (!head || strcmp(node->key, head->key) < 0) 70 | { 71 | ht->shead = node; 72 | if (!head) 73 | ht->stail = node; 74 | else 75 | { 76 | node->snext = head; 77 | head->sprev = node; 78 | } 79 | return; 80 | } 81 | 82 | while (head->snext && strcmp(node->key, head->snext->key) >= 0) 83 | head = head->snext; 84 | 85 | node->sprev = head; 86 | if (!head->snext) 87 | ht->stail = node; 88 | else 89 | head->snext->sprev = node; 90 | node->snext = head->snext; 91 | head->snext = node; 92 | } 93 | /** 94 | * shash_table_get - retrieves a value associated with a key 95 | * @ht: hash table to look into 96 | * @key: key of the value to retrieve 97 | * 98 | * Return: the value associated with the element, 99 | * or NULL if key couldn’t be found 100 | */ 101 | char *shash_table_get(const shash_table_t *ht, const char *key) 102 | { 103 | unsigned long int index; 104 | shash_node_t *node = NULL; 105 | 106 | if (!ht || !key || !strcmp(key, "")) 107 | return (NULL); 108 | 109 | index = key_index((const unsigned char *)key, ht->size); 110 | node = (ht->array)[index]; 111 | 112 | while (node) 113 | { 114 | if (!strcmp(node->key, key)) 115 | return (node->value); 116 | node = node->next; 117 | } 118 | 119 | return (NULL); 120 | } 121 | 122 | /** 123 | * shash_table_print - prints a hash table 124 | * @ht: hash table to print 125 | */ 126 | void shash_table_print(const shash_table_t *ht) 127 | { 128 | shash_node_t *head = NULL; 129 | 130 | if (!ht) 131 | return; 132 | 133 | head = ht->shead; 134 | 135 | printf("{"); 136 | while (head) 137 | { 138 | printf("'%s': '%s'", head->key, head->value); 139 | if (head->snext) 140 | printf(", "); 141 | head = head->snext; 142 | } 143 | 144 | printf("}\n"); 145 | } 146 | /** 147 | * shash_table_print_rev - prints a hash table in reverse 148 | * @ht: hash table to print 149 | */ 150 | void shash_table_print_rev(const shash_table_t *ht) 151 | { 152 | shash_node_t *tail = NULL; 153 | 154 | if (!ht) 155 | return; 156 | 157 | tail = ht->stail; 158 | 159 | printf("{"); 160 | while (tail) 161 | { 162 | printf("'%s': '%s'", tail->key, tail->value); 163 | if (tail->sprev) 164 | printf(", "); 165 | tail = tail->sprev; 166 | } 167 | 168 | printf("}\n"); 169 | } 170 | 171 | /** 172 | * shash_table_delete - deletes a hash table 173 | * @ht: hash table to be deleted 174 | */ 175 | void shash_table_delete(shash_table_t *ht) 176 | { 177 | if (!ht) 178 | return; 179 | 180 | free_list_s(ht->shead); 181 | free(ht->array); 182 | free(ht); 183 | } 184 | 185 | /** 186 | * replace_value_s - replaces the value at a pre-existing key 187 | * @ht: double pointer to the shash_node_t list 188 | * @key: new key to add in the node 189 | * @value: value to add in the node 190 | * 191 | * Return: 1 on success, 0 on failure 192 | */ 193 | int replace_value_s(shash_node_t **ht, const char *key, const char *value) 194 | { 195 | shash_node_t *temp = *ht; 196 | 197 | while (temp && strcmp(temp->key, key)) 198 | temp = temp->next; 199 | 200 | free(temp->value); 201 | temp->value = strdup(value); 202 | if (!temp->value) 203 | return (0); 204 | return (1); 205 | } 206 | 207 | /** 208 | * check_key_s - checks if a key exists in a hash table 209 | * @ht: pointer to the shash_node_t list 210 | * @key: key to look for 211 | * 212 | * Return: 1 if the key is found, 0 otherwise 213 | */ 214 | int check_key_s(shash_node_t *ht, const char *key) 215 | { 216 | while (ht) 217 | { 218 | if (!strcmp(ht->key, key)) 219 | return (1); 220 | ht = ht->next; 221 | } 222 | 223 | return (0); 224 | } 225 | 226 | /** 227 | * add_node_s - adds a new node at the beginning of a linked list 228 | * @head: double pointer to the shash_node_t list 229 | * @key: new key to add in the node 230 | * @value: value to add in the node 231 | * 232 | * Return: the address of the new element, or NULL if it fails 233 | */ 234 | shash_node_t *add_node_s(shash_node_t **head, const char *key, 235 | const char *value) 236 | { 237 | shash_node_t *new; 238 | 239 | new = calloc(1, sizeof(shash_node_t)); 240 | if (!new) 241 | return (NULL); 242 | 243 | new->key = strdup(key); 244 | if (!new->key) 245 | { 246 | free(new); 247 | return (NULL); 248 | } 249 | new->value = strdup(value); 250 | if (!new->value) 251 | { 252 | free(new); 253 | free(new->key); 254 | return (NULL); 255 | } 256 | 257 | if (*head == NULL) 258 | (*head) = new; 259 | else 260 | { 261 | new->next = (*head); 262 | (*head) = new; 263 | } 264 | 265 | return (*head); 266 | } 267 | 268 | /** 269 | * free_list_s - frees a linked list 270 | * @head: shash_node_t list to be freed 271 | */ 272 | void free_list_s(shash_node_t *head) 273 | { 274 | shash_node_t *temp; 275 | 276 | while (head) 277 | { 278 | temp = head->snext; 279 | free(head->key); 280 | free(head->value); 281 | free(head); 282 | head = temp; 283 | } 284 | } 285 | -------------------------------------------------------------------------------- /0x15-file_io/100-elf_header.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | void check_elf(unsigned char *e_ident); 10 | void print_magic(unsigned char *e_ident); 11 | void print_class(unsigned char *e_ident); 12 | void print_data(unsigned char *e_ident); 13 | void print_version(unsigned char *e_ident); 14 | void print_abi(unsigned char *e_ident); 15 | void print_osabi(unsigned char *e_ident); 16 | void print_type(unsigned int e_type, unsigned char *e_ident); 17 | void print_entry(unsigned long int e_entry, unsigned char *e_ident); 18 | void close_elf(int elf); 19 | 20 | /** 21 | * check_elf - Checks if a file is an ELF file. 22 | * @e_ident: A pointer to an array containing the ELF magic numbers. 23 | * 24 | * Description: If the file is not an ELF file - exit code 98. 25 | */ 26 | void check_elf(unsigned char *e_ident) 27 | { 28 | int index; 29 | 30 | for (index = 0; index < 4; index++) 31 | { 32 | if (e_ident[index] != 127 && 33 | e_ident[index] != 'E' && 34 | e_ident[index] != 'L' && 35 | e_ident[index] != 'F') 36 | { 37 | dprintf(STDERR_FILENO, "Error: Not an ELF file\n"); 38 | exit(98); 39 | } 40 | } 41 | } 42 | 43 | /** 44 | * print_magic - Prints the magic numbers of an ELF header. 45 | * @e_ident: A pointer to an array containing the ELF magic numbers. 46 | * 47 | * Description: Magic numbers are separated by spaces. 48 | */ 49 | void print_magic(unsigned char *e_ident) 50 | { 51 | int index; 52 | 53 | printf(" Magic: "); 54 | 55 | for (index = 0; index < EI_NIDENT; index++) 56 | { 57 | printf("%02x", e_ident[index]); 58 | 59 | if (index == EI_NIDENT - 1) 60 | printf("\n"); 61 | else 62 | printf(" "); 63 | } 64 | } 65 | 66 | /** 67 | * print_class - Prints the class of an ELF header. 68 | * @e_ident: A pointer to an array containing the ELF class. 69 | */ 70 | void print_class(unsigned char *e_ident) 71 | { 72 | printf(" Class: "); 73 | 74 | switch (e_ident[EI_CLASS]) 75 | { 76 | case ELFCLASSNONE: 77 | printf("none\n"); 78 | break; 79 | case ELFCLASS32: 80 | printf("ELF32\n"); 81 | break; 82 | case ELFCLASS64: 83 | printf("ELF64\n"); 84 | break; 85 | default: 86 | printf("\n", e_ident[EI_CLASS]); 87 | } 88 | } 89 | 90 | /** 91 | * print_data - Prints the data of an ELF header. 92 | * @e_ident: A pointer to an array containing the ELF class. 93 | */ 94 | void print_data(unsigned char *e_ident) 95 | { 96 | printf(" Data: "); 97 | 98 | switch (e_ident[EI_DATA]) 99 | { 100 | case ELFDATANONE: 101 | printf("none\n"); 102 | break; 103 | case ELFDATA2LSB: 104 | printf("2's complement, little endian\n"); 105 | break; 106 | case ELFDATA2MSB: 107 | printf("2's complement, big endian\n"); 108 | break; 109 | default: 110 | printf("\n", e_ident[EI_CLASS]); 111 | } 112 | } 113 | 114 | /** 115 | * print_version - Prints the version of an ELF header. 116 | * @e_ident: A pointer to an array containing the ELF version. 117 | */ 118 | void print_version(unsigned char *e_ident) 119 | { 120 | printf(" Version: %d", 121 | e_ident[EI_VERSION]); 122 | 123 | switch (e_ident[EI_VERSION]) 124 | { 125 | case EV_CURRENT: 126 | printf(" (current)\n"); 127 | break; 128 | default: 129 | printf("\n"); 130 | break; 131 | } 132 | } 133 | 134 | /** 135 | * print_osabi - Prints the OS/ABI of an ELF header. 136 | * @e_ident: A pointer to an array containing the ELF version. 137 | */ 138 | void print_osabi(unsigned char *e_ident) 139 | { 140 | printf(" OS/ABI: "); 141 | 142 | switch (e_ident[EI_OSABI]) 143 | { 144 | case ELFOSABI_NONE: 145 | printf("UNIX - System V\n"); 146 | break; 147 | case ELFOSABI_HPUX: 148 | printf("UNIX - HP-UX\n"); 149 | break; 150 | case ELFOSABI_NETBSD: 151 | printf("UNIX - NetBSD\n"); 152 | break; 153 | case ELFOSABI_LINUX: 154 | printf("UNIX - Linux\n"); 155 | break; 156 | case ELFOSABI_SOLARIS: 157 | printf("UNIX - Solaris\n"); 158 | break; 159 | case ELFOSABI_IRIX: 160 | printf("UNIX - IRIX\n"); 161 | break; 162 | case ELFOSABI_FREEBSD: 163 | printf("UNIX - FreeBSD\n"); 164 | break; 165 | case ELFOSABI_TRU64: 166 | printf("UNIX - TRU64\n"); 167 | break; 168 | case ELFOSABI_ARM: 169 | printf("ARM\n"); 170 | break; 171 | case ELFOSABI_STANDALONE: 172 | printf("Standalone App\n"); 173 | break; 174 | default: 175 | printf("\n", e_ident[EI_OSABI]); 176 | } 177 | } 178 | 179 | /** 180 | * print_abi - Prints the ABI version of an ELF header. 181 | * @e_ident: A pointer to an array containing the ELF ABI version. 182 | */ 183 | void print_abi(unsigned char *e_ident) 184 | { 185 | printf(" ABI Version: %d\n", 186 | e_ident[EI_ABIVERSION]); 187 | } 188 | 189 | /** 190 | * print_type - Prints the type of an ELF header. 191 | * @e_type: The ELF type. 192 | * @e_ident: A pointer to an array containing the ELF class. 193 | */ 194 | void print_type(unsigned int e_type, unsigned char *e_ident) 195 | { 196 | if (e_ident[EI_DATA] == ELFDATA2MSB) 197 | e_type >>= 8; 198 | 199 | printf(" Type: "); 200 | 201 | switch (e_type) 202 | { 203 | case ET_NONE: 204 | printf("NONE (None)\n"); 205 | break; 206 | case ET_REL: 207 | printf("REL (Relocatable file)\n"); 208 | break; 209 | case ET_EXEC: 210 | printf("EXEC (Executable file)\n"); 211 | break; 212 | case ET_DYN: 213 | printf("DYN (Shared object file)\n"); 214 | break; 215 | case ET_CORE: 216 | printf("CORE (Core file)\n"); 217 | break; 218 | default: 219 | printf("\n", e_type); 220 | } 221 | } 222 | 223 | /** 224 | * print_entry - Prints the entry point of an ELF header. 225 | * @e_entry: The address of the ELF entry point. 226 | * @e_ident: A pointer to an array containing the ELF class. 227 | */ 228 | void print_entry(unsigned long int e_entry, unsigned char *e_ident) 229 | { 230 | printf(" Entry point address: "); 231 | 232 | if (e_ident[EI_DATA] == ELFDATA2MSB) 233 | { 234 | e_entry = ((e_entry << 8) & 0xFF00FF00) | 235 | ((e_entry >> 8) & 0xFF00FF); 236 | e_entry = (e_entry << 16) | (e_entry >> 16); 237 | } 238 | 239 | if (e_ident[EI_CLASS] == ELFCLASS32) 240 | printf("%#x\n", (unsigned int)e_entry); 241 | 242 | else 243 | printf("%#lx\n", e_entry); 244 | } 245 | 246 | /** 247 | * close_elf - Closes an ELF file. 248 | * @elf: The file descriptor of the ELF file. 249 | * 250 | * Description: If the file cannot be closed - exit code 98. 251 | */ 252 | void close_elf(int elf) 253 | { 254 | if (close(elf) == -1) 255 | { 256 | dprintf(STDERR_FILENO, 257 | "Error: Can't close fd %d\n", elf); 258 | exit(98); 259 | } 260 | } 261 | 262 | /** 263 | * main - Displays the information contained in the 264 | * ELF header at the start of an ELF file. 265 | * @argc: The number of arguments supplied to the program. 266 | * @argv: An array of pointers to the arguments. 267 | * 268 | * Return: 0 on success. 269 | * 270 | * Description: If the file is not an ELF File or 271 | * the function fails - exit code 98. 272 | */ 273 | int main(int __attribute__((__unused__)) argc, char *argv[]) 274 | { 275 | Elf64_Ehdr *header; 276 | int o, r; 277 | 278 | o = open(argv[1], O_RDONLY); 279 | if (o == -1) 280 | { 281 | dprintf(STDERR_FILENO, "Error: Can't read file %s\n", argv[1]); 282 | exit(98); 283 | } 284 | header = malloc(sizeof(Elf64_Ehdr)); 285 | if (header == NULL) 286 | { 287 | close_elf(o); 288 | dprintf(STDERR_FILENO, "Error: Can't read file %s\n", argv[1]); 289 | exit(98); 290 | } 291 | r = read(o, header, sizeof(Elf64_Ehdr)); 292 | if (r == -1) 293 | { 294 | free(header); 295 | close_elf(o); 296 | dprintf(STDERR_FILENO, "Error: `%s`: No such file\n", argv[1]); 297 | exit(98); 298 | } 299 | 300 | check_elf(header->e_ident); 301 | printf("ELF Header:\n"); 302 | print_magic(header->e_ident); 303 | print_class(header->e_ident); 304 | print_data(header->e_ident); 305 | print_version(header->e_ident); 306 | print_osabi(header->e_ident); 307 | print_abi(header->e_ident); 308 | print_type(header->e_type, header->e_ident); 309 | print_entry(header->e_entry, header->e_ident); 310 | 311 | free(header); 312 | close_elf(o); 313 | return (0); 314 | } 315 | --------------------------------------------------------------------------------