├── 0x1C-makefiles ├── practice │ ├── file.txt │ └── Makefile ├── 0-Makefile ├── 1-Makefile ├── m.h ├── main.c ├── 2-Makefile ├── 100-Makefile ├── 3-Makefile ├── 4-Makefile ├── 5-island_perimeter.py ├── school.c └── README.md ├── 0x1E-search_algorithms ├── 1-binary.c ├── 100-jump.c ├── 105-jump_list.c ├── 107-O ├── 2-O ├── 3-O ├── 5-O ├── 6-O ├── 101-O ├── 102-interpolation.c ├── 103-exponential.c ├── 106-linear_skip.c ├── 108-O ├── 4-O ├── 104-advanced_binary.c ├── search_algos.h ├── 0-linear.c └── README.md ├── 0x17-doubly_linked_lists ├── 100-password ├── 102-result ├── 103-keygen.c ├── 4-free_dlistint.c ├── 7-insert_dnodeint.c ├── 1-dlistint_len.c ├── 6-sum_dlistint.c ├── 0-print_dlistint.c ├── 2-add_dnodeint.c ├── 5-get_dnodeint.c ├── README.md ├── 3-add_dnodeint_end.c ├── lists.h └── 8-delete_dnodeint.c ├── 0x1A-hash_tables ├── 100-sorted_hash_table.c ├── 2-key_index.c ├── 1-djb2.c ├── 5-hash_table_print.c ├── 0-hash_table_create.c ├── 6-hash_table_delete.c ├── 4-hash_table_get.c ├── hash_tables.h ├── 3-hash_table_set.c └── README.md ├── 0x0A-argc_argv ├── main.h ├── 0-whatsmyname.c ├── 2-args.c ├── 1-args.c ├── 3-mul.c ├── 4-add.c ├── 100-change.c └── README.md ├── 0x00-hello_world ├── 1-compiler ├── 3-name ├── 0-preprocessor ├── 2-assembler ├── 100-intel ├── 4-puts.c ├── 5-printf.c ├── 101-quote.c ├── 6-size.c └── README.md ├── 0x0D-preprocessor ├── 1-pi.h ├── 4-sum.h ├── 0-object_like_macro.h ├── 3-function_like_macro.h ├── 2-main.c ├── README.md └── quiz.md ├── 0x09-static_libraries ├── create_static_lib.sh ├── 6-abs.o ├── libmy.a ├── 3-puts.o ├── 0-isupper.o ├── 0-memset.o ├── 0-strcat.o ├── 1-isdigit.o ├── 1-memcpy.o ├── 1-strncat.o ├── 100-atoi.o ├── 2-strchr.o ├── 2-strlen.o ├── 2-strncpy.o ├── 3-islower.o ├── 3-strcmp.o ├── 3-strspn.o ├── 4-isalpha.o ├── 4-strpbrk.o ├── 5-strstr.o ├── 9-strcpy.o ├── _putchar.o ├── 1-isdigit.c ├── 0-isupper.c ├── 2-strlen.c ├── 3-puts.c ├── _putchar.c ├── 6-abs.c ├── 1-memcpy.c ├── 3-islower.c ├── 4-isalpha.c ├── 0-strcat.c ├── 9-strcpy.c ├── 2-strchr.c ├── 100-atoi.c ├── 0-memset.c ├── 4-strpbrk.c ├── 3-strcmp.c ├── 1-strncat.c ├── 3-strspn.c ├── 2-strncpy.c ├── main.h ├── 5-strstr.c └── README.md ├── 0x18-dynamic_libraries ├── 1-create_dynamic_lib.sh ├── libdynamic.so ├── 100-operations.o ├── main.h └── README.md ├── 0x04-more_functions_nested_loops ├── bzz ├── 2-mul.c ├── 0-isupper.c ├── 1-isdigit.c ├── 3-print_numbers.c ├── main.h ├── 4-print_most_numbers.c ├── 6-print_line.c ├── 100-prime_factor.c ├── 5-more_numbers.c ├── 101-print_number.c ├── 10-print_triangle.c ├── 8-print_square.c ├── 7-print_diagonal.c ├── 9-fizz_buzz.c └── README.md ├── 0x0C-more_malloc_free ├── main.h ├── 0-malloc_checked.c ├── 3-array_range.c ├── 2-calloc.c ├── 1-string_nconcat.c ├── quiz.md └── README.md ├── 0x12-singly_linked_lists ├── 101-hello_holberton.asm ├── 4-free_list.c ├── 100-first.c ├── 1-list_len.c ├── 0-print_list.c ├── 2-add_node.c ├── lists.h ├── 3-add_node_end.c ├── quiz.md └── README.md ├── 0x05-pointers_arrays_strings ├── 0-reset_to_98.c ├── 1-swap.c ├── 3-puts.c ├── _putchar.c ├── 2-strlen.c ├── 4-print_rev.c ├── 6-puts2.c ├── main.h ├── 8-print_array.c ├── 9-strcpy.c ├── 7-puts_half.c ├── 5-rev_string.c ├── 100-atoi.c ├── 101-keygen.c └── README.md ├── 0x02-functions_nested_loops ├── 1-alphabet.c ├── 6-abs.c ├── 3-islower.c ├── 10-add.c ├── 0-putchar.c ├── 4-isalpha.c ├── 11-print_to_98.c ├── 103-fibonacci.c ├── 7-print_last_digit.c ├── 102-fibonacci.c ├── main.h ├── 2-print_alphabet_x10.c ├── 5-sign.c ├── 101-natural.c ├── 9-times_table.c ├── 8-24_hours.c ├── 100-times_table.c └── README.md ├── 0x03-debugging ├── 0-main.c ├── main.h ├── 1-main.c ├── 2-largest_number.c ├── README.md ├── 3-print_remaining_days.c └── 3-convert_day.c ├── 0x14-bit_manipulation ├── 100-get_endianness.c ├── 2-get_bit.c ├── 4-clear_bit.c ├── main.h ├── 3-set_bit.c ├── 1-print_binary.c ├── 5-flip_bits.c ├── 0-binary_to_uint.c └── README.md ├── 0x0F-function_pointers ├── 0-print_name.c ├── function_pointers.h ├── 3-calc.h ├── 1-array_iterator.c ├── 3-main.c ├── 2-int_index.c ├── 3-get_op_func.c ├── 3-op_functions.c ├── README.md └── quiz.md ├── 0x07-pointers_arrays_strings ├── 100-set_string.c ├── 2-strchr.c ├── 7-print_chessboard.c ├── main.h ├── 1-memcpy.c ├── 4-strpbrk.c ├── 3-strspn.c ├── 5-strstr.c ├── 0-memset.c ├── 8-print_diagsums.c └── README.md ├── 0x0E-structures_typedef ├── 5-free_dog.c ├── 1-init_dog.c ├── 2-print_dog.c ├── dog.h ├── quiz.md ├── 4-new_dog.c └── README.md ├── 0x08-recursion ├── 0-puts_recursion.c ├── 1-print_rev_recursion.c ├── main.h ├── 2-strlen_recursion.c ├── 3-factorial.c ├── 4-pow_recursion.c ├── 5-sqrt_recursion.c ├── 6-is_prime_number.c └── README.md ├── 0x01-variables_if_else_while ├── 8-print_base16.c ├── 9-print_comb.c ├── 2-print_alphabet.c ├── 6-print_numberz.c ├── 7-print_tebahpla.c ├── 4-print_alphabt.c ├── 5-print_numbers.c ├── 3-print_alphabets.c ├── 0-positive_or_negative.c ├── 1-last_digit.c └── README.md ├── 0x0B-malloc_free ├── 4-free_grid.c ├── main.h ├── 1-strdup.c ├── 0-create_array.c ├── 3-alloc_grid.c ├── 2-str_concat.c ├── quiz.md ├── 100-argstostr.c └── README.md ├── 0x06-pointers_arrays_strings ├── 5-string_toupper.c ├── main.h ├── 3-strcmp.c ├── 4-rev_array.c ├── 101-print_number.c ├── 0-strcat.c ├── 7-leet.c ├── 2-strncpy.c ├── 1-strncat.c ├── 6-cap_string.c ├── 100-rot13.c └── README.md ├── 0x13-more_singly_linked_lists ├── 4-free_listint.c ├── 5-free_listint2.c ├── 8-sum_listint.c ├── 1-listint_len.c ├── 0-print_listint.c ├── 6-pop_listint.c ├── 100-reverse_listint.c ├── 2-add_nodeint.c ├── 7-get_nodeint.c ├── 101-print_listint_safe.c ├── 0-main.c ├── 102-free_listint_safe.c ├── 103-find_loop.c ├── 3-add_nodeint_end.c ├── README.md ├── 10-delete_nodeint.c ├── 9-insert_nodeint.c └── lists.h ├── 0x10-variadic_functions ├── variadic_functions.h ├── 0-sum_them_all.c ├── 1-print_numbers.c ├── 2-print_strings.c ├── 3-print_all.c └── README.md ├── 0x15-file_io ├── 2-append_text_to_file.c ├── 1-create_file.c ├── 0-read_textfile.c ├── main.h ├── 3-cp.c ├── README.md └── quiz.md └── README.md /0x1C-makefiles/practice/file.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/1-binary.c: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/100-jump.c: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/100-password: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/102-result: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/103-keygen.c: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/105-jump_list.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 | -------------------------------------------------------------------------------- /0x1A-hash_tables/100-sorted_hash_table.c: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/101-O: -------------------------------------------------------------------------------- 1 | O(sqrt(n)) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/102-interpolation.c: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/103-exponential.c: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/106-linear_skip.c: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/108-O: -------------------------------------------------------------------------------- 1 | O(sqrt(n)) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/4-O: -------------------------------------------------------------------------------- 1 | O(log(n)) 2 | -------------------------------------------------------------------------------- /0x0A-argc_argv/main.h: -------------------------------------------------------------------------------- 1 | int _putchar(char); 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/104-advanced_binary.c: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /0x00-hello_world/1-compiler: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -c $CFILE 3 | -------------------------------------------------------------------------------- /0x00-hello_world/3-name: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc $CFILE -o cisfun 3 | -------------------------------------------------------------------------------- /0x00-hello_world/0-preprocessor: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -E $CFILE -o ./c 3 | -------------------------------------------------------------------------------- /0x00-hello_world/2-assembler: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -S $CFILE -o ./${CFILE//'.c'/'.s'} 3 | -------------------------------------------------------------------------------- /0x00-hello_world/100-intel: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -S -masm=intel $CFILE -o ./${CFILE//'.c'/'.s'} 3 | -------------------------------------------------------------------------------- /0x0D-preprocessor/1-pi.h: -------------------------------------------------------------------------------- 1 | #ifndef PI_H 2 | #define PI_H 3 | #define PI 3.14159265359 4 | #endif 5 | -------------------------------------------------------------------------------- /0x09-static_libraries/create_static_lib.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -c ./*.c 3 | ar -rc liball.a ./*.o 4 | -------------------------------------------------------------------------------- /0x0D-preprocessor/4-sum.h: -------------------------------------------------------------------------------- 1 | #ifndef SUM_H 2 | #define SUM_H 3 | #define SUM(x, y) ((x) + (y)) 4 | 5 | #endif 6 | -------------------------------------------------------------------------------- /0x09-static_libraries/6-abs.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Itsfoss0/alx-low_level_programming/HEAD/0x09-static_libraries/6-abs.o -------------------------------------------------------------------------------- /0x09-static_libraries/libmy.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Itsfoss0/alx-low_level_programming/HEAD/0x09-static_libraries/libmy.a -------------------------------------------------------------------------------- /0x09-static_libraries/3-puts.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Itsfoss0/alx-low_level_programming/HEAD/0x09-static_libraries/3-puts.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/1-create_dynamic_lib.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/bash 2 | gcc -Wall -c -fPIC *.c 3 | gcc -shared *.o -o liball.so 4 | -------------------------------------------------------------------------------- /0x09-static_libraries/0-isupper.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Itsfoss0/alx-low_level_programming/HEAD/0x09-static_libraries/0-isupper.o -------------------------------------------------------------------------------- /0x09-static_libraries/0-memset.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Itsfoss0/alx-low_level_programming/HEAD/0x09-static_libraries/0-memset.o -------------------------------------------------------------------------------- /0x09-static_libraries/0-strcat.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Itsfoss0/alx-low_level_programming/HEAD/0x09-static_libraries/0-strcat.o -------------------------------------------------------------------------------- /0x09-static_libraries/1-isdigit.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Itsfoss0/alx-low_level_programming/HEAD/0x09-static_libraries/1-isdigit.o -------------------------------------------------------------------------------- /0x09-static_libraries/1-memcpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Itsfoss0/alx-low_level_programming/HEAD/0x09-static_libraries/1-memcpy.o -------------------------------------------------------------------------------- /0x09-static_libraries/1-strncat.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Itsfoss0/alx-low_level_programming/HEAD/0x09-static_libraries/1-strncat.o -------------------------------------------------------------------------------- /0x09-static_libraries/100-atoi.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Itsfoss0/alx-low_level_programming/HEAD/0x09-static_libraries/100-atoi.o -------------------------------------------------------------------------------- /0x09-static_libraries/2-strchr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Itsfoss0/alx-low_level_programming/HEAD/0x09-static_libraries/2-strchr.o -------------------------------------------------------------------------------- /0x09-static_libraries/2-strlen.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Itsfoss0/alx-low_level_programming/HEAD/0x09-static_libraries/2-strlen.o -------------------------------------------------------------------------------- /0x09-static_libraries/2-strncpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Itsfoss0/alx-low_level_programming/HEAD/0x09-static_libraries/2-strncpy.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-islower.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Itsfoss0/alx-low_level_programming/HEAD/0x09-static_libraries/3-islower.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-strcmp.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Itsfoss0/alx-low_level_programming/HEAD/0x09-static_libraries/3-strcmp.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-strspn.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Itsfoss0/alx-low_level_programming/HEAD/0x09-static_libraries/3-strspn.o -------------------------------------------------------------------------------- /0x09-static_libraries/4-isalpha.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Itsfoss0/alx-low_level_programming/HEAD/0x09-static_libraries/4-isalpha.o -------------------------------------------------------------------------------- /0x09-static_libraries/4-strpbrk.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Itsfoss0/alx-low_level_programming/HEAD/0x09-static_libraries/4-strpbrk.o -------------------------------------------------------------------------------- /0x09-static_libraries/5-strstr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Itsfoss0/alx-low_level_programming/HEAD/0x09-static_libraries/5-strstr.o -------------------------------------------------------------------------------- /0x09-static_libraries/9-strcpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Itsfoss0/alx-low_level_programming/HEAD/0x09-static_libraries/9-strcpy.o -------------------------------------------------------------------------------- /0x09-static_libraries/_putchar.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Itsfoss0/alx-low_level_programming/HEAD/0x09-static_libraries/_putchar.o -------------------------------------------------------------------------------- /0x1C-makefiles/0-Makefile: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env make 2 | # first makefile 3 | 4 | all: main.c school.c m.h 5 | @gcc main.c school.c -o school -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/bzz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Itsfoss0/alx-low_level_programming/HEAD/0x04-more_functions_nested_loops/bzz -------------------------------------------------------------------------------- /0x0D-preprocessor/0-object_like_macro.h: -------------------------------------------------------------------------------- 1 | #ifndef OBJECT_LIKE_MACRO_H 2 | #define OBJECT_LIKE_MACRO_H 3 | #define SIZE 1024 4 | 5 | #endif 6 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/libdynamic.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Itsfoss0/alx-low_level_programming/HEAD/0x18-dynamic_libraries/libdynamic.so -------------------------------------------------------------------------------- /0x18-dynamic_libraries/100-operations.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Itsfoss0/alx-low_level_programming/HEAD/0x18-dynamic_libraries/100-operations.o -------------------------------------------------------------------------------- /0x1C-makefiles/1-Makefile: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env make 2 | # Makefile with variables 3 | 4 | CC = gcc 5 | SRC = main.c school.c 6 | all: 7 | @$(CC) $(SRC) -o school -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x0D-preprocessor/3-function_like_macro.h: -------------------------------------------------------------------------------- 1 | #ifndef FUNCTION_LIKE_MACRO_H 2 | #define FUNCTION_LIKE_MACRO_H 3 | #define ABS(x) (((x) < (0)) ? ((x) * (-1)) : (x)) 4 | 5 | #endif 6 | -------------------------------------------------------------------------------- /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 | } -------------------------------------------------------------------------------- /0x1C-makefiles/2-Makefile: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env make 2 | #first 'useful' Makefile 3 | 4 | CC = gcc 5 | SRC = school.c main.c 6 | OBJ = $(SRC:.c=.o) 7 | NAME = school 8 | 9 | all: $(OBJ) 10 | @$(CC) $(OBJ) -o $(NAME) 11 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/search_algos.h: -------------------------------------------------------------------------------- 1 | #ifndef SEARCH_ALGOS_H 2 | #define SEARCH_ALGOS_H 3 | 4 | #include 5 | #include 6 | 7 | int linear_search(int *array, size_t size, int value); 8 | 9 | #endif 10 | -------------------------------------------------------------------------------- /0x0D-preprocessor/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | *main - prints the name of 4 | *the file it was compiled from 5 | *Return: 0 6 | */ 7 | int main(void) 8 | { 9 | printf("%s\n", __FILE__); 10 | return (0); 11 | } 12 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/main.h: -------------------------------------------------------------------------------- 1 | void *malloc_checked(unsigned int b); 2 | char *string_nconcat(char *s1, char *s2, unsigned int n); 3 | int _strlen(char *string); 4 | void *_calloc(unsigned int nmemb, unsigned int size); 5 | int *array_range(int min, int max); 6 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/101-hello_holberton.asm: -------------------------------------------------------------------------------- 1 | global main 2 | extern printf 3 | main: 4 | mov edi, format 5 | xor eax, eax 6 | call printf 7 | mov eax, 0 8 | ret 9 | format: db `Hello, Holberton\n`,0 10 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/0-reset_to_98.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * reset_to_98 - Reset the parameter to 98 4 | *@n: Pointe to the value we want to set to 98 5 | *Return : void 6 | */ 7 | void reset_to_98(int *n) 8 | { 9 | *n = 98; 10 | } 11 | -------------------------------------------------------------------------------- /0x09-static_libraries/1-isdigit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isdigit - checks if a character is digit 5 | * @c: carrier variable 6 | * Return: 1 if true, 0 if false 7 | */ 8 | 9 | int _isdigit(int c) 10 | { 11 | return (c >= 48 && c <= 57); 12 | } 13 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/1-alphabet.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_alphabet - Prints alphabets 'a - z' 5 | */ 6 | 7 | void print_alphabet(void) 8 | { 9 | char c; 10 | 11 | for (c = 'a'; c <= 'z'; c++) 12 | { 13 | _putchar(c); 14 | } 15 | _putchar('\n'); 16 | } 17 | -------------------------------------------------------------------------------- /0x03-debugging/0-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - tests function that prints if integer is positive or negative 5 | * Return: 0 6 | */ 7 | 8 | 9 | int main(void) 10 | { 11 | int i; 12 | 13 | i = 98; 14 | positive_or_negative(i); 15 | 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x00-hello_world/101-quote.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main - Entry point 5 | * 6 | * Return: Always 1 (Error) 7 | */ 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 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/100-get_endianness.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * get_endianness - checks the endianness 5 | * Return: 0 if big endian, 1 if little endian 6 | */ 7 | int get_endianness(void) 8 | { 9 | unsigned int i = 1; 10 | char *c = (char *) &i; 11 | 12 | return (*c); 13 | } 14 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/2-mul.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *mul - multiply two numbers ( a and b) 4 | *@a: first number to be multiplied 5 | *@b: number to be multiplied agains 6 | *Return: the product of 'a' and 'b' 7 | */ 8 | 9 | int mul(int a, int b) 10 | { 11 | return (a * b); 12 | } 13 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/6-abs.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _abs - COmputes the absolute value of an integer 5 | * 6 | *@a: The integer to be computed 7 | * 8 | * Return: Absolute value 9 | */ 10 | 11 | int _abs(int a) 12 | { 13 | if (a >= 0) 14 | return (a); 15 | else 16 | return (a * -1); 17 | } 18 | -------------------------------------------------------------------------------- /0x03-debugging/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | #include 5 | 6 | void positive_or_negative(int i); 7 | 8 | int largest_number(int a, int b, int c); 9 | 10 | int convert_day(int month, int day); 11 | 12 | void print_remaining_days(int month, int day, int year); 13 | 14 | #endif /* MAIN_H */ 15 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/1-swap.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | *swap_int - swap two integers 4 | *@a: Pointer to the integer to be swaped with a 5 | *@b: Pointer to the interget to be swaped with b 6 | *Return: void 7 | */ 8 | void swap_int(int *a, int *b) 9 | { 10 | int c = *a; 11 | *a = *b; 12 | *b = c; 13 | } 14 | -------------------------------------------------------------------------------- /0x09-static_libraries/0-isupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isupper - checks is a character is uppercase 5 | * @c: carrier integer variable 6 | * Return: 1 if true, 0 if false 7 | */ 8 | 9 | int _isupper(int c) 10 | { 11 | if (c >= 65 && c <= 90) 12 | { 13 | return (1); 14 | } 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x09-static_libraries/2-strlen.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strlen - checks the length of a string 5 | * @s: string to check 6 | * 7 | * Return: int length of the string 8 | */ 9 | 10 | int _strlen(char *s) 11 | { 12 | int length = 0; 13 | 14 | while (s[length]) 15 | length++; 16 | return (length); 17 | } 18 | -------------------------------------------------------------------------------- /0x0F-function_pointers/0-print_name.c: -------------------------------------------------------------------------------- 1 | #include "function_pointers.h" 2 | #include 3 | /** 4 | * print_name - prints a name 5 | * @name: person's name 6 | * @f: pointer to a function 7 | * Return: void 8 | */ 9 | void print_name(char *name, void (*f)(char *)) 10 | { 11 | if (f != NULL) 12 | f(name); 13 | 14 | } 15 | -------------------------------------------------------------------------------- /0x0F-function_pointers/function_pointers.h: -------------------------------------------------------------------------------- 1 | #ifndef FUNCTION_POINTERS_H 2 | #define FUNCTION_POINTERS_H 3 | 4 | int _putchar(char); 5 | void print_name(char *name, void (*f)(char *)); 6 | void array_iterator(int *array, size_t size, void (*action)(int)); 7 | int int_index(int *array, int size, int (*cmp)(int)); 8 | 9 | #endif 10 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/3-puts.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _puts - prints string to stdout 5 | * @str: pointer to string to be printed 6 | * Return: no return 7 | */ 8 | 9 | void _puts(char *str) 10 | { 11 | while (*str != '\0') 12 | { 13 | _putchar(*str); 14 | str++; 15 | } 16 | _putchar('\n'); 17 | } 18 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/100-set_string.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * set_string - sets the value of a pointer to a char 6 | * 7 | * @s: Double pointer 8 | * 9 | * @to: Pointer 10 | * 11 | * Return: Void 12 | */ 13 | 14 | void set_string(char **s, char *to) 15 | { 16 | *s = to; 17 | } 18 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/_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 | -------------------------------------------------------------------------------- /0x0A-argc_argv/0-whatsmyname.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - prints program name 6 | *@argc: number of arguments 7 | *@argv: array of arguments 8 | *Return: 0 9 | */ 10 | int main(int __attribute__((__unused__)) argc, char *argv[]) 11 | { 12 | printf("%s\n", argv[0]); 13 | 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/5-free_dog.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | 4 | /** 5 | * free_dog - free memory allocated to the dog 6 | * @d: pointe to the dog 7 | * Return: void 8 | */ 9 | void free_dog(dog_t *d) 10 | { 11 | if (d != NULL) 12 | { 13 | free(d->owner); 14 | free(d->name); 15 | free(d); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/4-free_list.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * free_list - frees a linked list 4 | * @head: list_t list to be freed 5 | */ 6 | void free_list(list_t *head) 7 | { 8 | list_t *tmp; 9 | 10 | while (head) 11 | { 12 | tmp = head->next; 13 | free(head->str); 14 | free(head); 15 | head = tmp; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-puts.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _puts - prints a string and a new line 5 | * @str: pointer to a string to print 6 | * 7 | * Return: 0 8 | */ 9 | 10 | void _puts(char *str) 11 | { 12 | int i = 0; 13 | 14 | while (str[i]) 15 | { 16 | _putchar(str[i]); 17 | i++; 18 | } 19 | _putchar('\n'); 20 | } 21 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/100-first.c: -------------------------------------------------------------------------------- 1 | #include 2 | void first(void) __attribute__ ((constructor)); 3 | 4 | /** 5 | * first - prints a sentence before the main 6 | * function is executed 7 | */ 8 | void first(void) 9 | { 10 | printf("You're beat! and yet, you must allow,\n"); 11 | printf("I bore my house upon my back!\n"); 12 | } 13 | -------------------------------------------------------------------------------- /0x1C-makefiles/practice/Makefile: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env make 2 | # looking at varibles in make 3 | myvar = $(newvar) 4 | newvar = $(oldvar) 5 | oldvar = Variable; 6 | 7 | show_variables: file.txt 8 | @echo $(oldvar) 9 | @echo $(myvar) 10 | @echo ${MAKE} 11 | @echo $< 12 | @echo $@ 13 | 14 | file.txt: 15 | touch file.txt 16 | @echo $(shell whoami) 17 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/3-islower.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _islower - Checks for lowercase character 5 | *@c: The character to be checked 6 | * Return: 1 (True - lowercase) OR 0 (False - not lowecase) 7 | */ 8 | 9 | int _islower(int c) 10 | { 11 | if (c >= 97 && c <= 122) 12 | return (1); 13 | else 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x09-static_libraries/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * _putchar - writes the character c to stdout 6 | * @c: The character to print 7 | * 8 | * Return: On success 1. 9 | * On error, -1 is returned, and errno is set appropriately. 10 | */ 11 | int _putchar(char c) 12 | { 13 | return (write(1, &c, 1)); 14 | } 15 | -------------------------------------------------------------------------------- /0x0A-argc_argv/2-args.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - Print all arguments recieved 4 | * @argc: arguments' count 5 | * @argv: Pointer to a string of args 6 | * Return: 0 7 | */ 8 | int main(int argc, char *argv[]) 9 | { 10 | int i; 11 | 12 | for (i = 0; i < argc; i++) 13 | printf("%s\n", argv[i]); 14 | 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/10-add.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | *add - add two integer number and return the value 5 | *@a : - the first argument of the add function 6 | *@b : - The second argument of the add function 7 | * Return: void 8 | */ 9 | int add(int a, int b) 10 | { 11 | int result; 12 | 13 | result = a + b; 14 | return (result); 15 | } 16 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/2-strlen.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_strlen - Return the length of a string 4 | *@s: pointer to a string to find its length 5 | *Return: int - the length of a string 6 | */ 7 | int _strlen(char *s) 8 | { 9 | int len = 0, i = 0; 10 | while (s[i] != '\0') 11 | { 12 | len += 1; 13 | i++; 14 | } 15 | return (len); 16 | } 17 | -------------------------------------------------------------------------------- /0x08-recursion/0-puts_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_puts_recursion - print a string to console 4 | * @s: pointer to a strint to print 5 | * return : void (Always) 6 | */ 7 | void _puts_recursion(char *s) 8 | { 9 | if (*s == '\0') 10 | _putchar('\n'); 11 | else 12 | { 13 | _putchar(s[0]); 14 | _puts_recursion(s + 1); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /0x08-recursion/1-print_rev_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_print_rev_recursion - Print a string in reverse recursively 4 | *@s:Pointer to the strint to be printed 5 | *Return - void (Always) 6 | */ 7 | void _print_rev_recursion(char *s) 8 | { 9 | if (*s != '\0') 10 | { 11 | _print_rev_recursion(s + 1); 12 | _putchar(*s); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /0x0A-argc_argv/1-args.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * main - prints the number of arguments 6 | *@argc: number of arguments 7 | *@argv: pointer to a string of args 8 | *Return:0 9 | */ 10 | 11 | int main(int argc, char __attribute__((__unused__)) *argv[]) 12 | { 13 | printf("%d\n", argc - 1); 14 | 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/8-print_base16.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - Entry point 4 | * 5 | * Return: Always 0 (Success) 6 | */ 7 | int main(void) 8 | { 9 | int z; 10 | 11 | for (z = 48; z <= 57; z++) 12 | { 13 | putchar(z); 14 | } 15 | for (z = 97; z <= 102; z++) 16 | { 17 | putchar(z); 18 | } 19 | putchar('\n'); 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/9-print_comb.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - Entry point 4 | * 5 | * Return: Always 0 (Success) 6 | */ 7 | int main(void) 8 | { 9 | int k; 10 | 11 | for (k = 48; k <= 57; k++) 12 | { 13 | putchar(k); 14 | if (k < 57) 15 | { 16 | putchar(','); 17 | putchar(' '); 18 | } 19 | } 20 | putchar('\n'); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/0-isupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _isupper - Checks for uppercase character 4 | *@c: The character to be checked 5 | * Return: 1 (True - uppercase) OR 0 (False - lowecase) 6 | */ 7 | int _isupper(int c) 8 | { 9 | if (c > 64 && c < 91) 10 | { 11 | return (1); 12 | } 13 | else 14 | { 15 | return (0); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/1-isdigit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isdigit - checks for a digit (0 through 9). 5 | * @c: input character 6 | * Return: 1 if c is a digit and 0 otherwise 7 | */ 8 | 9 | int _isdigit(int c) 10 | { 11 | if (c >= 48 && c <= 57) 12 | { 13 | return (1); 14 | } 15 | else 16 | { 17 | return (0); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/3-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * print_numbers - prints the numbers, from 0 to 9, followed by a new line 6 | * Return: void 7 | */ 8 | 9 | void print_numbers(void) 10 | { 11 | int i; 12 | 13 | for (i = 48; i < 58; i++) 14 | { 15 | putchar(i); 16 | } 17 | putchar('\n'); 18 | } 19 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/main.h: -------------------------------------------------------------------------------- 1 | int _isupper(int c); 2 | int _isdigit(int c); 3 | int mul(int a, int b); 4 | void print_numbers(void); 5 | void print_most_numbers(void); 6 | void more_numbers(void); 7 | void print_line(int n); 8 | void print_diagonal(int n); 9 | void print_square(int size); 10 | void print_triangle(int size); 11 | void print_number(int n); 12 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/4-print_rev.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *print_rev - print a string in reverse 4 | *@s: pointer to the string to be printed 5 | *Return: void 6 | */ 7 | 8 | 9 | void print_rev(char *s) 10 | { 11 | int i = 0; 12 | 13 | while (s[i]) 14 | i++; 15 | 16 | while (i--) 17 | _putchar(s[i]); 18 | 19 | _putchar('\n'); 20 | } 21 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/0-putchar.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * main - Entry point 4 | * Description: prints '_putchar\n' 5 | * Return: Always 0 (success) 6 | */ 7 | int main(void) 8 | { 9 | char str[] = "_putchar"; 10 | int i = 0; 11 | 12 | while (str[i] != '\0') 13 | { 14 | char c = str[i]; 15 | 16 | _putchar(c); 17 | i++; 18 | } 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /0x0B-malloc_free/4-free_grid.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * free_grid - frees a 2d array 5 | *@grid:memory block to be freed 6 | *@height:height of the array 7 | *Return:returns void 8 | */ 9 | 10 | void free_grid(int **grid, int height) 11 | { 12 | int i; 13 | 14 | for (i = 0; i < height; i++) 15 | free(grid[i]); 16 | 17 | free(grid); 18 | } 19 | -------------------------------------------------------------------------------- /0x0B-malloc_free/main.h: -------------------------------------------------------------------------------- 1 | int _putchar(char); 2 | char *create_array(unsigned int size, char c); 3 | char *_strdup(char *str); 4 | char *str_concat(char *s1, char *s2); 5 | int _strlen(char *s); 6 | int **alloc_grid(int width, int height); 7 | void free_grid(int **grid, int height); 8 | char *argstostr(int ac, char **av); 9 | char **strtow(char *str); 10 | int len(char *str); 11 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/6-puts2.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * puts2 - prints every other character of a string 5 | *@str: pointer to the string 6 | * Return: void 7 | */ 8 | void puts2(char *str) 9 | { 10 | int i = 0; 11 | 12 | while (str[i] != '\0') 13 | { 14 | if (i % 2 == 0) 15 | _putchar(str[i]); 16 | i++; 17 | } 18 | 19 | _putchar('\n'); 20 | } 21 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/5-string_toupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * string_toupper - lower to upper 4 | * @s: pointer to char params 5 | * 6 | * Return: *s 7 | */ 8 | 9 | char *string_toupper(char *s) 10 | { 11 | int i; 12 | 13 | for (i = 0; s[i] != '\0'; i++) 14 | { 15 | if (s[i] >= 'a' && s[i] <= 'z') 16 | s[i] -= 32; 17 | } 18 | return (s); 19 | } 20 | -------------------------------------------------------------------------------- /0x08-recursion/main.h: -------------------------------------------------------------------------------- 1 | void _puts_recursion(char *s); 2 | int _putchar(char); 3 | void _print_rev_recursion(char *s); 4 | int _strlen_recursion(char *s); 5 | int factorial(int n); 6 | int _pow_recursion(int x, int y); 7 | int _sqrt_recursion(int n); 8 | int _sqrt(int n, int i); 9 | int is_prime_number(int n); 10 | int is_palindrome(char *s); 11 | int wildcmp(char *s1, char *s2); 12 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/2-get_bit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * get_bit - return value of a bit at a given index 5 | * @n: decimal parameter 6 | * @index: index 7 | * Return: val 8 | */ 9 | int get_bit(unsigned long int n, unsigned int index) 10 | { 11 | int bit; 12 | 13 | bit = (n >> index); 14 | if (index > 32) 15 | return (-1); 16 | return (bit & 1); 17 | } 18 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/4-free_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_dlistint - frees a dlistint_t list. 5 | * @head: pointer to the head of the list. 6 | * 7 | * 8 | */ 9 | 10 | void free_dlistint(dlistint_t *head) 11 | { 12 | 13 | while (head != NULL) 14 | { 15 | dlistint_t *next = head->next; 16 | 17 | free(head); 18 | head = next; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /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 | /** 17 | * while (i < 10) 18 | * { 19 | * putchar(i); 20 | * } 21 | */ 22 | printf("Infinite loop avoided! \\o/\n"); 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | char *_strcat(char *dest, char *src); 2 | char *_strncat(char *dest, char *src, int n); 3 | char *_strncpy(char *dest, char *src, int n); 4 | int _strcmp(char *s1, char *s2); 5 | void reverse_array(int *a, int n); 6 | char *string_toupper(char *); 7 | char *cap_string(char *); 8 | char *leet(char *); 9 | char *rot13(char *); 10 | void print_number(int n); 11 | -------------------------------------------------------------------------------- /0x08-recursion/2-strlen_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_strlen_recursion - recursively find the lenght of a string 4 | *@s : pointer to the string parameter 5 | *Return: len (length of the string) 6 | */ 7 | int _strlen_recursion(char *s) 8 | { 9 | int len = 0; 10 | if (!(*s == '\0')) 11 | { 12 | len++; 13 | len += _strlen_recursion(s + 1); 14 | } 15 | return (len); 16 | } 17 | -------------------------------------------------------------------------------- /0x1A-hash_tables/2-key_index.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * key_index - find the index of a key 5 | * @key: key 6 | * @size: size of the array of the hash table 7 | * Return: position 8 | */ 9 | unsigned long int key_index(const unsigned char *key, unsigned long int size) 10 | { 11 | unsigned long int postion = hash_djb2(key) % size; 12 | 13 | return (postion); 14 | } 15 | -------------------------------------------------------------------------------- /0x09-static_libraries/6-abs.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _abs - Entry point 5 | * @n: n is an integer 6 | * Description: prints the absolute value of an integer 7 | * Return: int 8 | */ 9 | 10 | int _abs(int n) 11 | { 12 | if (n > 0) 13 | { 14 | return (n); 15 | } 16 | else if (n < 0) 17 | { 18 | return (-n); 19 | } 20 | else 21 | { 22 | return (n); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/4-isalpha.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isalpha - Checks for Alphabetic Characters 5 | * 6 | * @c: Alphabetic character 7 | * 8 | * Return: 1 (True - alphabetic chars) OR 0 (False - not alphabetic char) 9 | */ 10 | 11 | int _isalpha(int c) 12 | { 13 | if ((c >= 97 && c <= 122) || (c >= 65 && c <= 90)) 14 | return (1); 15 | else 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/4-print_most_numbers.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * print_most_numbers - prints the numbers, from 0 to 9 . 6 | * Return: void 7 | */ 8 | 9 | void print_most_numbers(void) 10 | { 11 | int i; 12 | 13 | for (i = 48; i < 58; i++) 14 | { 15 | if (!(i == 50) && !(i == 52)) 16 | { 17 | putchar(i); 18 | } 19 | } 20 | putchar(10); 21 | } 22 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/0-malloc_checked.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * malloc_checked - allocates memory using malloc. 5 | * @b: size of memory to be allocated. 6 | * 7 | * Return: pointer to allocated memory. 8 | */ 9 | void *malloc_checked(unsigned int b) 10 | { 11 | void *p = malloc(b); 12 | 13 | if (p == NULL) 14 | exit(98); 15 | 16 | return (p); 17 | } 18 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/2-print_alphabet.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main - main block 5 | * Description: Get a random number and check its last digit, compare it with 5 6 | * Return: 0 7 | */ 8 | int main(void) 9 | { 10 | char c = 'a'; 11 | 12 | while (c <= 'z') 13 | { 14 | putchar(c); 15 | c++; 16 | } 17 | 18 | putchar('\n'); 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/6-print_numberz.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - main block 4 | * Description: prints all single digit numbers of base 10 5 | * starting from 0, followed by a new line. 6 | * Return: 0 7 | */ 8 | int main(void) 9 | { 10 | int c = 0; 11 | 12 | while (c < 10) 13 | { 14 | putchar(48 + c); 15 | c++; 16 | } 17 | 18 | putchar('\n'); 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/7-print_tebahpla.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - main block 4 | * Description: prints all single digit numbers of base 10 5 | * starting from 0, followed by a new line. 6 | * Return: 0 7 | */ 8 | int main(void) 9 | { 10 | char c = 'z'; 11 | 12 | while (c >= 'a') 13 | { 14 | putchar(c); 15 | c--; 16 | } 17 | 18 | putchar('\n'); 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/1-init_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | 3 | /** 4 | * init_dog - initialize a var of type dog 5 | *@d: pointer to dog's id 6 | *@name: name of dog 7 | *@age: age of dog 8 | *@owner: owner's name 9 | */ 10 | void init_dog(struct dog *d, char *name, float age, char *owner) 11 | { 12 | if (d != NULL) 13 | { 14 | (*d).name = name; 15 | (*d).age = age; 16 | (*d).owner = owner; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/4-free_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /** 5 | * free_listint - Frees a list. 6 | * @head: Address of the first node of a list. 7 | **/ 8 | 9 | void free_listint(listint_t *head) 10 | { 11 | listint_t *tp, *tp2; 12 | 13 | tp = head; 14 | while (!(tp = NULL)) 15 | { 16 | tp2 = tp->next; 17 | free(tp); 18 | tp = tp2; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /0x1A-hash_tables/1-djb2.c: -------------------------------------------------------------------------------- 1 | /** 2 | * hash_djb2 - implementing the djb2 algorithm 3 | * @str: string to generate hash value 4 | * 5 | * Return: hash value 6 | */ 7 | unsigned long int hash_djb2(const unsigned char *str) 8 | { 9 | unsigned long int hash; 10 | int c; 11 | 12 | hash = 5381; 13 | while ((c = *str++)) 14 | { 15 | hash = ((hash << 5) + hash) + c; 16 | } 17 | return (hash); 18 | } 19 | -------------------------------------------------------------------------------- /0x09-static_libraries/1-memcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * *_memcpy - copies memory area 5 | * @dest: memory area 6 | * @src: source 7 | * @n: length of src 8 | * 9 | * Return: pointer to dest. 10 | */ 11 | char *_memcpy(char *dest, char *src, unsigned int n) 12 | { 13 | unsigned int i; 14 | 15 | for (i = 0; i < n; i++) 16 | { 17 | dest[i] = src[i]; 18 | } 19 | return (dest); 20 | } 21 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/6-print_line.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * print_line - draws a straight line in the terminal. 6 | * @n: The number of _ characters to be printed. 7 | */ 8 | 9 | void print_line(int n) 10 | { 11 | int line; 12 | 13 | if (n > 0) 14 | { 15 | for (line = 0; line < n; line++) 16 | { 17 | putchar('_'); 18 | } 19 | } 20 | putchar('\n'); 21 | } 22 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/4-clear_bit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * clear_bit - sets the value of a bit to 0 at a given index 6 | * @n: parameter 7 | * @index: index 8 | * Return: 1 if works, -1 if error 9 | */ 10 | 11 | int clear_bit(unsigned long int *n, unsigned int index) 12 | { 13 | if (index > sizeof(n) * 8) 14 | return (-1); 15 | *n &= ~(1 << index); 16 | return (1); 17 | } 18 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/4-print_alphabt.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - main block 4 | * Description: print lowercase letters of the alphabet that are not 'q' and 'e' 5 | * Return: 0 6 | */ 7 | int main(void) 8 | { 9 | char c = 'a'; 10 | 11 | while (c <= 'z') 12 | { 13 | if (c != 'e' && c != 'q') 14 | { 15 | putchar(c); 16 | } 17 | c++; 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 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | void reset_to_98(int *n); 2 | void swap_int(int *a, int *b); 3 | int _strlen(char *s); 4 | void _puts(char *str); 5 | int _putchar(char c); 6 | void print_rev(char *s); 7 | void rev_string(char *s); 8 | void rev_string(char *s); 9 | void puts2(char *str); 10 | void puts_half(char *str); 11 | void print_array(int *a, int n); 12 | char *_strcpy(char *dest, char *src); 13 | int _atoi(char *s); 14 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/3-strcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strcmp - compare 4 | * @s1 : pointerto char params 5 | * @s2 : pointer to char params 6 | * Return: *dest 7 | */ 8 | int _strcmp(char *s1, char *s2) 9 | { 10 | int i; 11 | int R; 12 | 13 | i = 0; 14 | 15 | while (s1[i] == s2[i] && (s1[i] != '\0' || s2[i] != '\0')) 16 | { 17 | i++; 18 | } 19 | R = s1[i] - s2[i]; 20 | return (R); 21 | } 22 | -------------------------------------------------------------------------------- /0x1C-makefiles/100-Makefile: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env make 2 | #Advanced Makefile 3 | CC = gcc 4 | SRC = school.c main.c 5 | OBJ = $(SRC:.c=.o) 6 | NAME = school 7 | CFLAGS = -Wall -Werror -Wextra -pedantic 8 | 9 | .PHONY: all oclean fclean re clean 10 | 11 | all: m.h $(OBJ) 12 | $(CC) $(OBJ) -o $(NAME) 13 | 14 | clean: 15 | $(RM) *~ $(NAME) 16 | 17 | oclean: 18 | $(RM) $(OBJ) 19 | 20 | fclean: clean oclean 21 | 22 | re: fclean all -------------------------------------------------------------------------------- /0x01-variables_if_else_while/5-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main - main block 5 | * Description: prints all single digit numbers of base 10 6 | * starting from 0, followed by a new line. 7 | * Return: 0 8 | */ 9 | int main(void) 10 | { 11 | char c = '0'; 12 | 13 | while (c <= '9') 14 | { 15 | putchar(c); 16 | c++; 17 | } 18 | 19 | putchar('\n'); 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-islower.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _islower - Entry point 5 | * Description: checks is a character is lowercase 6 | * @c: the integer value it receives 7 | * Return: 1 if true. 0 if false. 8 | */ 9 | int _islower(int c) 10 | { 11 | int i = 'a'; 12 | 13 | for (i = 'a'; i <= 'z'; i++) 14 | { 15 | /* refer int c*/ 16 | if (c == i) 17 | { 18 | return (1); 19 | } 20 | } 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/100-prime_factor.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints largest prime factor of the number 612852475143. 5 | * Return: Always 0. 6 | */ 7 | 8 | int main(void) 9 | { 10 | long int n, i; 11 | 12 | n = 612852475143; 13 | for (i = 2; i <= n; i++) 14 | { 15 | if (n % i == 0) 16 | { 17 | n /= i; 18 | i--; 19 | } 20 | } 21 | printf("%ld\n", i); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/5-free_listint2.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /** 5 | * free_listint2 - Frees a list. 6 | * @head: Address of the first node of a list. 7 | **/ 8 | 9 | void free_listint2(listint_t **head) 10 | { 11 | listint_t *temp; 12 | 13 | if (head == NULL) 14 | return; 15 | while (*head != NULL) 16 | { 17 | temp = (*head)->next; 18 | free(*head); 19 | *head = temp; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/8-sum_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /** 5 | * sum_listint - Sum all of the data (n) in a list. 6 | * @head: Address of the first node of the list. 7 | * Return: Integer. 8 | **/ 9 | 10 | int sum_listint(listint_t *head) 11 | { 12 | int total = 0; 13 | 14 | while (head != NULL) 15 | { 16 | total += head->n; 17 | head = head->next; 18 | } 19 | return (total); 20 | } 21 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/8-print_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | *print_array - print array elements 5 | *@a: pointer to the array 6 | *@n: number of elements to print 7 | *return: void 8 | */ 9 | void print_array(int *a, int n) 10 | { 11 | int i; 12 | 13 | for (i = 0; i < n; i++) 14 | { 15 | printf("%d", a[i]); 16 | 17 | if (i + 1 != n) 18 | printf(", "); 19 | } 20 | 21 | printf("\n"); 22 | } 23 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/4-rev_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * reverse_array - reverse 4 | * @a : pointer to int params 5 | * @n : pointer to int params 6 | * Return: no 7 | */ 8 | void reverse_array(int *a, int n) 9 | { 10 | int i; 11 | int j; 12 | int tmp; 13 | 14 | i = 0; 15 | j = n - 1; 16 | 17 | while (i < j) 18 | { 19 | tmp = a[i]; 20 | a[i] = a[j]; 21 | a[j] = tmp; 22 | i++; 23 | j--; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /0x09-static_libraries/4-isalpha.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isalpha - Entry point 5 | * @c: the integer value it receives 6 | * Description: checks if alphabet 7 | * Return: 1 if true. 0 if false 8 | */ 9 | 10 | int _isalpha(int c) 11 | { 12 | if (c >= 'a' && c <= 'z') 13 | { 14 | return (1); 15 | } 16 | else if (c >= 'A' && c <= 'Z') 17 | { 18 | return (1); 19 | } 20 | else 21 | { 22 | return (0); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/101-print_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_number - print numbers chars 5 | * @n: integer params 6 | * Return: 0 7 | **/ 8 | 9 | void print_number(int n) 10 | { 11 | unsigned int n1; 12 | 13 | n1 = n; 14 | 15 | if (n < 0) 16 | { 17 | _putchar('-'); 18 | n1 = -n; 19 | } 20 | 21 | if (n1 / 10 != 0) 22 | { 23 | print_number(n1 / 10); 24 | } 25 | _putchar((n1 % 10) + '0'); 26 | } 27 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/103-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | *main - prints sum of even fibonacci 4 | *suit elements 5 | *fibonacci suit numbers 6 | *Return: return 0 7 | */ 8 | int main(void) 9 | { 10 | unsigned long n1 = 0, n2 = 1, n3 = 0, sum = 0; 11 | while (n3 <= 4000000) 12 | { 13 | n3 = n1 + n2; 14 | n1 = n2; 15 | n2 = n3; 16 | 17 | if ((n1 % 2) == 0) 18 | sum += n1; 19 | 20 | } 21 | printf("%ld\n", sum); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /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 | * 6 | * @n: A number 7 | * 8 | * Return: Last digit of n 9 | */ 10 | 11 | int print_last_digit(int n) 12 | { 13 | int last_digit; 14 | 15 | if (n < 0) 16 | last_digit = (n % 10) * -1; 17 | else 18 | last_digit = n % 10; 19 | 20 | _putchar(last_digit + '0'); 21 | 22 | return (last_digit); 23 | } 24 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/0-strcat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * *_strcat - concatenates two strings 4 | * @dest: pointer to the destination 5 | * @src: pointer to the source 6 | * Return: void 7 | */ 8 | 9 | char *_strcat(char *dest, char *src) 10 | { 11 | int a = -1, i; 12 | for (i = 0; dest[i] != '\0'; i++) 13 | ; 14 | 15 | do { 16 | a++; 17 | dest[i] = src[a]; 18 | i++; 19 | } while (src[a] != '\0'); 20 | 21 | return (dest); 22 | } 23 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/main.h: -------------------------------------------------------------------------------- 1 | /** Function prototypes here **/ 2 | int _putchar(char); 3 | unsigned int binary_to_uint(const char *b); 4 | void print_binary(unsigned long int n); 5 | int get_bit(unsigned long int n, unsigned int index); 6 | int set_bit(unsigned long int *n, unsigned int index); 7 | int clear_bit(unsigned long int *n, unsigned int index); 8 | unsigned int flip_bits(unsigned long int n, unsigned long int m); 9 | int get_endianness(void); 10 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/2-strchr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strchr -Search for a character in a string 5 | * 6 | * @s: The string to be searched against 7 | * 8 | * @c: The character to find 9 | * 10 | * Return: Pointer to @s or NULL 11 | * 12 | */ 13 | 14 | char *_strchr(char *s, char c) 15 | { 16 | while (*s) 17 | { 18 | s++; 19 | 20 | if (*s == c) 21 | { 22 | return (s); 23 | } 24 | } 25 | 26 | return (0); 27 | } 28 | -------------------------------------------------------------------------------- /0x10-variadic_functions/variadic_functions.h: -------------------------------------------------------------------------------- 1 | #ifndef _VARIADIC_FUNCTIONS_H_ 2 | #define _VARIADIC_FUNCTIONS_H_ 3 | 4 | int sum_them_all(const unsigned int n, ...); 5 | int _putchar(char); 6 | void print_numbers(const char *separator, const unsigned int n, ...); 7 | void print_strings(const char *separator, const unsigned int n, ...); 8 | void print_all(const char * const format, ...); 9 | 10 | #include 11 | #include 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/1-listint_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * listint_len - return number of elements in a linked list. 5 | * @h: pointer to a singly linked list. 6 | * Return: an int. 7 | **/ 8 | 9 | size_t listint_len(const listint_t *h) 10 | { 11 | const listint_t *temp; 12 | unsigned int count = 0; 13 | 14 | temp = h; 15 | while (temp) 16 | { 17 | count += 1; 18 | temp = temp->next; 19 | } 20 | return (count); 21 | } 22 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/102-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | *main - prints out first 50 4 | *fibonacci suit numbers 5 | *Return: return 0 6 | */ 7 | int main(void) 8 | { 9 | int inc; 10 | unsigned long n1 = 0, n2 = 1, n3; 11 | for (inc = 0; inc < 50; inc++) 12 | { 13 | n3 = n1 + n2; 14 | printf("%lu", n3); 15 | n1 = n2; 16 | n2 = n3; 17 | 18 | if (inc == 49) 19 | printf("\n"); 20 | else 21 | printf(", "); 22 | 23 | } 24 | 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/9-strcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_strcpy -Copy a string from src to dest 4 | *@dest: Pointer to the destination address 5 | *@src: Ponter to the source address 6 | *Return: char copy of the string 7 | */ 8 | char *_strcpy(char *dest, char *src) 9 | { 10 | int i = 0; 11 | 12 | while (src[i] != '\0') 13 | { 14 | *(dest + i) = src[i]; 15 | i++; 16 | } 17 | 18 | *(dest + i) = '\0'; 19 | 20 | return (dest); 21 | } 22 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/7-leet.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * leet - encode 4 | * @s: pointer to char params 5 | * Return: *s 6 | */ 7 | 8 | char *leet(char *s) 9 | { 10 | int i; 11 | int j; 12 | char l[] = "ol_ea__t"; 13 | 14 | for (i = 0; s[i] != '\0'; i++) 15 | { 16 | for (j = 0; l[j] != '\0'; j++) 17 | { 18 | if (s[i] == l[j] || s[i] == (l[j] - 32)) 19 | { 20 | s[i] = j + '0'; 21 | } 22 | } 23 | } 24 | return (s); 25 | } 26 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/7-print_chessboard.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_chessboard - Prints a chessboard. 5 | * @a: The chessboard to be printed. 6 | * I honestly dont know what I did :( 7 | */ 8 | void print_chessboard(char (*a)[8]) 9 | { 10 | int indx1, indx2; 11 | 12 | for (indx1 = 0; a[indx1][7]; indx1++) 13 | { 14 | for (indx2 = 0; indx2 < 8; indx2++) 15 | _putchar(a[indx1][indx2]); 16 | 17 | _putchar('\n'); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/0-print_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * print_listint - print all elemement to stdout 5 | * @h: pointer to a singly liked list. 6 | * Return: an int. 7 | **/ 8 | 9 | size_t print_listint(const listint_t *h) 10 | { 11 | const listint_t *tp; 12 | unsigned int cntr = 0; 13 | 14 | tp = h; 15 | while (tp) 16 | { 17 | printf("%d\n", tp->n); 18 | cntr++; 19 | tp = tp->next; 20 | } 21 | return (cntr); 22 | } 23 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/3-set_bit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * set_bit - sets the value of a bit to 1 at a given index 6 | * @n: the value whose bit is to be set to 1 7 | * @index: position of the bit 8 | * Return: 1 if it worked, -1 if error occured 9 | */ 10 | int set_bit(unsigned long int *n, unsigned int index) 11 | { 12 | if (index > sizeof(n) * 8) 13 | return (-1); 14 | *n |= (1 << index); 15 | return (1); 16 | } 17 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/7-puts_half.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * puts_half - print half a strong 5 | *@str: Pointe to the string 6 | * Return: void 7 | */ 8 | 9 | void puts_half(char *str) 10 | { 11 | int i = 0; 12 | int n; 13 | 14 | while (str[i] != '\0') 15 | i++; 16 | 17 | if (i % 2 != 0) 18 | n = (i + 1) / 2; 19 | else 20 | n = i / 2; 21 | 22 | for (; str[n] != '\0'; n++) 23 | _putchar(str[n]); 24 | 25 | _putchar('\n'); 26 | } 27 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | int _putchar(char); 2 | char *_memset(char *s, char b, unsigned int n); 3 | char *_memcpy(char *dest, char *src, unsigned int n); 4 | char *_strchr(char *s, char c); 5 | unsigned int _strspn(char *s, char *accept); 6 | char *_strpbrk(char *s, char *accept); 7 | char *_strstr(char *haystack, char *needle); 8 | void print_chessboard(char (*a)[8]); 9 | void print_diagsums(int *a, int size); 10 | void set_string(char **s, char *to); 11 | 12 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/7-insert_dnodeint.c: -------------------------------------------------------------------------------- 1 | 2 | #include "lists.h" 3 | 4 | /** 5 | * insert_dnodeint_at_index - inserts a new node at a given position 6 | * @h: pointer to head of list 7 | * @idx: index of the list where the new node should be added 8 | * @n: data of the new node 9 | * 10 | * Return: the address of the new node, or NULL if it failed 11 | */ 12 | 13 | dlistint_t *insert_dnodeint_at_index(dlistint_t **h, unsigned int idx, int n) 14 | { 15 | } 16 | -------------------------------------------------------------------------------- /0x08-recursion/3-factorial.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *factorial - return the factorial of a number 4 | *@n:number to find its factorial 5 | * Return: If n > 0 - the factorial of n. 6 | * If n < 0 - 1 to indicate an error. 7 | */ 8 | int factorial(int n) 9 | { 10 | int result = n; 11 | 12 | if (n < 0) 13 | return (-1); 14 | 15 | else if (n >= 0 && n <= 1) 16 | return (1); 17 | 18 | result *= factorial(n - 1); 19 | 20 | return (result); 21 | } 22 | -------------------------------------------------------------------------------- /0x0A-argc_argv/3-mul.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | *main - multiply two numbers 5 | *@argc: argument count 6 | *@argv: pointer to a string of args 7 | *Return: 0 8 | */ 9 | int main(int argc, char *argv[]) 10 | { 11 | int a, b, prod; 12 | 13 | if (argc <= 2) 14 | { 15 | printf("Error\n"); 16 | return (1); 17 | } 18 | 19 | a = atoi(argv[1]); 20 | b = atoi(argv[2]); 21 | prod = a * b; 22 | 23 | printf("%d\n", prod); 24 | return (0); 25 | } 26 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/2-print_dog.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "dog.h" 4 | 5 | /** 6 | * print_dog - Prints all the data of a dog 7 | * @d: A dog structure 8 | * 9 | * Return: Nothing 10 | */ 11 | void print_dog(struct dog *d) 12 | { 13 | if (d != NULL) 14 | { 15 | printf("Name: %s\n", (d->name) ? d->name : "(nil)"); 16 | printf("Age: %f\n", (d->age) ? d->age : 0); 17 | printf("Owner: %s\n", (d->owner) ? d->owner : "(nil)"); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-calc.h: -------------------------------------------------------------------------------- 1 | #ifndef _HEADER_ 2 | #define _HEADER_ 3 | 4 | /** 5 | * struct op - Struct op 6 | * @op: operator 7 | * @f:function to be performed 8 | */ 9 | typedef struct op 10 | { 11 | char *op; 12 | int (*f)(int a, int b); 13 | } op_t; 14 | 15 | int op_add(int a, int b); 16 | int op_sub(int a, int b); 17 | int op_mul(int a, int b); 18 | int op_div(int a, int b); 19 | int op_mod(int a, int b); 20 | int (*get_op_func(char *s))(int, int); 21 | 22 | #endif 23 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/main.h: -------------------------------------------------------------------------------- 1 | int _putchar(char c); 2 | void print_alphabet(void); 3 | void print_alphabet_x10(void); 4 | int _islower(int c); 5 | int _isalpha(int c); 6 | int print_sign(int n); 7 | int _abs(int); 8 | int print_last_digit(int); 9 | void jack_bauer(void); 10 | void times_table(void); 11 | void times_table(void); 12 | int add(int, int); 13 | void print_to_98(int n); 14 | void print_to_98(int n); 15 | void print_times_table(int n); 16 | void print_times_table(int n); 17 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/2-strncpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strncpy - two words 4 | * @dest : pointer to char params 5 | * @src : pointer to char params 6 | * @n : int params 7 | * Return: *dest 8 | */ 9 | 10 | char *_strncpy(char *dest, char *src, int n) 11 | { 12 | int i; 13 | 14 | for (i = 0; src[i] != '\0' && i < n; i++) 15 | { 16 | dest[i] = src[i]; 17 | } 18 | 19 | for ( ; i < n; i++) 20 | { 21 | dest[i] = '\0'; 22 | } 23 | return (dest); 24 | } 25 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/2-print_alphabet_x10.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_alphabet_x10 - Prints the letters of the alphabet 10 times 5 | * Description - We cant use standard library to implement our functions 6 | * Return: void 7 | */ 8 | 9 | void print_alphabet_x10(void) 10 | { 11 | int i; 12 | 13 | for (i = 0; i < 10; i++) 14 | { 15 | char c; 16 | 17 | for (c = 'a'; c <= 'z'; c++) 18 | { 19 | _putchar(c); 20 | } 21 | _putchar('\n'); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/1-dlistint_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * dlistint_len - returns the number of elements in a linked dlistint_t list. 5 | * @h: pointer to the head of the list 6 | * 7 | * Return: number of nodes 8 | */ 9 | 10 | size_t dlistint_len(const dlistint_t *h) 11 | { 12 | int counter = 0; 13 | 14 | if (h == NULL) 15 | return (counter); 16 | 17 | while (h != NULL) 18 | { 19 | counter++; 20 | h = h->next; 21 | } 22 | 23 | return (counter); 24 | } 25 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/3-print_alphabets.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - main block 4 | * Description: prints the alphabet in lowercase, 5 | * and then in uppercase, followed by a new line. 6 | * Return: 0 7 | */ 8 | int main(void) 9 | { 10 | char c = 'a'; 11 | 12 | while (c <= 'z') 13 | { 14 | putchar(c); 15 | c++; 16 | } 17 | 18 | c = 'A'; 19 | 20 | while (c <= 'Z') 21 | { 22 | putchar(c); 23 | c++; 24 | } 25 | 26 | putchar('\n'); 27 | return (0); 28 | } 29 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/5-more_numbers.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * more_numbers - prints 10 times the numbers, from 0 to 14, 6 | * followed by a new line. 7 | * Return: void 8 | */ 9 | 10 | void more_numbers(void) 11 | { 12 | int i, j; 13 | 14 | for (i = 0; i < 10; i++) 15 | { 16 | for (j = 0; j <= 14; j++) 17 | { 18 | if (j > 9) 19 | putchar((j / 10) + '0'); 20 | putchar((j % 10) + '0'); 21 | } 22 | putchar('\n'); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/6-sum_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * sum_dlistint - returns the sum of all 5 | * the data (n) of a dlistint_t linked list. 6 | * @head: double pointer to the head of the list 7 | * 8 | * Return: if the list is empty, return 0 9 | */ 10 | 11 | int sum_dlistint(dlistint_t *head) 12 | { 13 | 14 | int sum = 0; 15 | dlistint_t *i; 16 | 17 | for (i = head; i != NULL; i = i->next) 18 | { 19 | sum += i->n; 20 | } 21 | 22 | return (sum); 23 | } 24 | -------------------------------------------------------------------------------- /0x1C-makefiles/3-Makefile: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env make 2 | # make file with PHONY targets 3 | 4 | #phony targets 5 | .PHONY: clean 6 | .PHONY: oclean 7 | .PHONY: fclean 8 | .PHONY: re 9 | 10 | #variables 11 | CC = gcc 12 | SRC = main.c school.c 13 | OBJ = $(SRC:.c=.o) 14 | NAME = school 15 | RM = rm -f 16 | 17 | #targets 18 | all: $(OBJ) 19 | $(CC) $(OBJ) -o $(NAME) 20 | 21 | clean: 22 | $(RM) *~ $(NAME) 23 | 24 | oclean: 25 | $(RM) $(OBJ) 26 | 27 | fclean: clean oclean 28 | 29 | re: fclean all 30 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/101-print_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * print_number - Prints an integer. 6 | * @n: The integer to prints. 7 | * 8 | * Return: Nothing! 9 | */ 10 | void print_number(int n) 11 | { 12 | unsigned int k = n; 13 | 14 | if (n < 0) 15 | { 16 | n *= -1; 17 | k = n; 18 | putchar('-'); 19 | } 20 | 21 | k /= 10; 22 | 23 | if (k != 0) 24 | print_number(k); 25 | 26 | putchar((unsigned int) n % 10 + '0'); 27 | 28 | } 29 | -------------------------------------------------------------------------------- /0x09-static_libraries/0-strcat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * *_strcat - concatenates @src to @dest 5 | * @src: the source string to append to @dest 6 | * @dest: the destiation string to be concatenated upon 7 | * Return: char 8 | */ 9 | 10 | char *_strcat(char *dest, char *src) 11 | { 12 | int index = 0; 13 | int dest_len = 0; 14 | 15 | while (dest[index++]) 16 | dest_len++; 17 | for (index = 0; src[index]; index++) 18 | dest[dest_len++] = src[index]; 19 | return (dest); 20 | } 21 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/5-sign.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_sign - Print the sign of a number 5 | * 6 | * @n: Number 7 | * 8 | * Return: 1 (is greater that zero) OR 0 (is zero) 9 | * OR -1 (is less that zero) 10 | */ 11 | 12 | int print_sign(int n) 13 | { 14 | if (n > 0) 15 | { 16 | _putchar(43); 17 | return (1); 18 | } 19 | else if (n == 0) 20 | { 21 | _putchar(48); 22 | return (0); 23 | } 24 | else 25 | { 26 | _putchar(45); 27 | return (-1); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /0x09-static_libraries/9-strcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * *_strcpy - copies the string pointed to by src 5 | * @dest: char type string 6 | * @src: char type string 7 | * Description: Copy the string pointed to by pointer `src` to 8 | * the buffer pointed to by `dest` 9 | * Return: Pointer to `dest 10 | */ 11 | 12 | char *_strcpy(char *dest, char *src) 13 | { 14 | int i = -1; 15 | 16 | do { 17 | i++; 18 | dest[i] = src[i]; 19 | } while (src[i] != '\0'); 20 | 21 | return (dest); 22 | } 23 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/101-natural.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main - main block 5 | * Description: computes and prints the sum of all the multiples of 3 or 6 | * 5 below 1024 (excluded), followed by a new line 7 | * Return: 0 8 | */ 9 | int main(void) 10 | { 11 | int c = 0; 12 | int sum = 0; 13 | 14 | while (c < 1024) 15 | { 16 | if (c % 3 == 0 || c % 5 == 0) 17 | { 18 | sum += c; 19 | } 20 | 21 | c++; 22 | } 23 | printf("%i\n", sum); 24 | return (0); 25 | } 26 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/1-memcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _memcpy - Copies content from source to destination 5 | * memory area 6 | * @src: pointer to source memory area 7 | * @dest: Pointer to destination memory destination 8 | * @n: Number of bytes 9 | * Return: A pointer to @dest 10 | */ 11 | char *_memcpy(char *dest, char *src, unsigned int n) 12 | { 13 | unsigned int i; 14 | 15 | for (i = 0; i < n; i++) 16 | { 17 | *(dest + i) = *(src + i); 18 | } 19 | 20 | return (dest); 21 | } 22 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/10-print_triangle.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * print_triangle - prints a triangle. 6 | * @size: size of the triangle. 7 | * Return: void. 8 | */ 9 | 10 | void print_triangle(int size) 11 | { 12 | int i, j; 13 | 14 | for (i = 0; i < size; i++) 15 | { 16 | for (j = 1; j < (size - i); j++) 17 | putchar(' '); 18 | for (j--; j < size; j++) 19 | putchar(35); 20 | if (i < (size - 1)) 21 | putchar('\n'); 22 | } 23 | putchar('\n'); 24 | } 25 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/6-pop_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include "stdlib.h" 3 | /** 4 | * pop_listint - pop the first element in a linked list. 5 | * @head: Pointer to a list. 6 | * Return: Integer(the value of the popped list) 7 | **/ 8 | 9 | int pop_listint(listint_t **head) 10 | { 11 | listint_t *tp; 12 | int lists_value; 13 | 14 | if (!(*head != NULL)) 15 | return (0); 16 | 17 | tp = *head; 18 | *head = tp->next; 19 | lists_value = tp->n; 20 | free(tp); 21 | return (lists_value); 22 | } 23 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/4-strpbrk.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strpbrk - Searches a string for any of a set of bytes 5 | * 6 | * @s: Character to check 7 | * 8 | * @accept: Character to look for 9 | * 10 | * Return: Character 11 | */ 12 | 13 | char *_strpbrk(char *s, char *accept) 14 | { 15 | int i, k; 16 | 17 | for (i = 0; s[i] != '\0'; i++) 18 | { 19 | for (k = 0; accept[k] != '\0'; k++) 20 | { 21 | if (s[i] == accept[k]) 22 | return (s + i); 23 | } 24 | } 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /0x09-static_libraries/2-strchr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strchr - Locates a character in a string. 5 | * @s: The string to be searched. 6 | * @c: The character to be located. 7 | * 8 | * Return: If c is found - a pointer to the first occurence. 9 | * If c is not found - NULL. 10 | */ 11 | 12 | char *_strchr(char *s, char c) 13 | { 14 | int index; 15 | 16 | for (index = 0; s[index] >= '\0'; index++) 17 | { 18 | if (s[index] == c) 19 | return (s + index); 20 | } 21 | 22 | return ('\0'); 23 | } 24 | -------------------------------------------------------------------------------- /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: parameter 6 | */ 7 | 8 | void print_binary(unsigned long int n) 9 | { 10 | int i, count = 0; 11 | unsigned long int current; 12 | 13 | for (i = 63; i >= 0; i--) 14 | { 15 | current = n >> i; 16 | 17 | if (current & 1) 18 | { 19 | _putchar('1'); 20 | count++; 21 | } 22 | else if (count) 23 | _putchar('0'); 24 | } 25 | if (!count) 26 | _putchar('0'); 27 | } 28 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/0-positive_or_negative.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | /** 5 | * main - Entry point 6 | * 7 | * Return: Always 0 (Success) 8 | */ 9 | int main(void) 10 | { 11 | int n; 12 | srand(time(0)); 13 | n = rand() - RAND_MAX / 2; 14 | /* your code goes there */ 15 | if (n > 0) 16 | { 17 | printf("%d is positive\n", n); 18 | } 19 | if (n < 0) 20 | { 21 | printf("%d is negative\n", n); 22 | } 23 | if (n == 0) 24 | { 25 | printf("%d is zero\n", n); 26 | } 27 | return (0); 28 | } 29 | -------------------------------------------------------------------------------- /0x0F-function_pointers/1-array_iterator.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "function_pointers.h" 3 | /** 4 | * array_iterator - execute function on each element 5 | * of an array 6 | * @array: given array 7 | * @size: size of the array 8 | * @action: pointer to function 9 | * Return: void 10 | */ 11 | void array_iterator(int *array, size_t size, void (*action)(int)) 12 | { 13 | int i; 14 | 15 | if (array == NULL || action == NULL) 16 | return; 17 | 18 | for (i = 0; i < (int) size; i++) 19 | { 20 | action(array[i]); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /0x08-recursion/4-pow_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_pow_recursion - return x power y 4 | *@x: x to raise to power y 5 | *@y: y to be raised by x 6 | *Dont worry - I also wasted time here, 7 | *I dont even know what I honestly did 8 | *Return: res(The value of x raised to the power of y.) 9 | */ 10 | int _pow_recursion(int x, int y) 11 | { 12 | int res = x; 13 | 14 | if (y < 0) 15 | return (-1); 16 | 17 | else if (y == 0) 18 | return (1); 19 | 20 | res *= _pow_recursion(x, y - 1); 21 | 22 | return (res); 23 | } 24 | -------------------------------------------------------------------------------- /0x08-recursion/5-sqrt_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _sqrt_recursion - main funct 5 | * @n: int n 6 | * Return: int 7 | */ 8 | int _sqrt_recursion(int n) 9 | { 10 | return (_sqrt(n, 1)); 11 | } 12 | 13 | /** 14 | * _sqrt - _sqrt_recursion 15 | * @n: integer paramtr 16 | * @i: integer parameter 17 | * Return: sqrt 18 | */ 19 | int _sqrt(int n, int i) 20 | { 21 | if (n < 0) 22 | return (-1); 23 | if ((i * i) > n) 24 | return (-1); 25 | if (i * i == n) 26 | return (i); 27 | return (_sqrt(n, i + 1)); 28 | } 29 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/5-rev_string.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * rev_string - prints a string in reverse 5 | * @s: pointer to the string to be reversed 6 | * 7 | * Return: void 8 | */ 9 | 10 | void rev_string(char *s) 11 | { 12 | int i = 0, j, right; 13 | 14 | while (s[i]) 15 | i++; 16 | 17 | j = i / 2; 18 | right = 0; 19 | 20 | while (right != j) 21 | { 22 | char temp = s[right]; 23 | int left = i - right - 1; 24 | 25 | s[right] = s[left]; 26 | s[left] = temp; 27 | 28 | right++; 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /0x1C-makefiles/4-Makefile: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env make 2 | # make file with PHONY targets 3 | 4 | #phony targets 5 | .PHONY: clean 6 | .PHONY: oclean 7 | .PHONY: fclean 8 | .PHONY: re 9 | 10 | #variables 11 | CC = gcc 12 | CFLAGS = -Wall -Werror -Wextra -pedantic 13 | SRC = main.c school.c 14 | OBJ = $(SRC:.c=.o) 15 | NAME = school 16 | RM = rm -f 17 | 18 | #targets 19 | all: $(OBJ) 20 | @$(CC) $(CFLAGS) $(OBJ) -o $(NAME) 21 | 22 | clean: 23 | $(RM) *~ $(NAME) 24 | 25 | oclean: 26 | $(RM) $(OBJ) 27 | 28 | fclean:clean oclean 29 | 30 | re: fclean all 31 | -------------------------------------------------------------------------------- /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 | int sign = 1; 13 | unsigned int num = 0; 14 | 15 | do { 16 | 17 | if (*s == '-') 18 | sign *= -1; 19 | 20 | else if (*s >= '0' && *s <= '9') 21 | num = (num * 10) + (*s - '0'); 22 | 23 | else if (num > 0) 24 | break; 25 | } while (*s++); 26 | 27 | return (num * sign); 28 | } 29 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/8-print_square.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * print_square - prints a square, 6 | * followed by a new line. 7 | * @size: The size of the square. 8 | */ 9 | 10 | void print_square(int size) 11 | { 12 | int i, j; 13 | 14 | if (size > 0) 15 | { 16 | for (i = 0; i < size; i++) 17 | { 18 | for (j = 0; j < size; j++) 19 | { 20 | putchar('#'); 21 | } 22 | if (i == size - 1) 23 | { 24 | continue; 25 | } 26 | putchar('\n'); 27 | } 28 | } 29 | putchar('\n'); 30 | } 31 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/3-strspn.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _strspn - Get the length of a prefix substring 6 | * 7 | * @s: The character to print 8 | * 9 | * @accept: Character 10 | * 11 | * Return: unsigned int 12 | */ 13 | 14 | unsigned int _strspn(char *s, char *accept) 15 | { 16 | unsigned int i, j; 17 | 18 | for (i = 0; s[i] != '\0'; i++) 19 | { 20 | for (j = 0; accept[j] != s[i]; j++) 21 | { 22 | if (accept[j] == '\0') 23 | return (i); 24 | } 25 | 26 | } 27 | 28 | return (i); 29 | 30 | } 31 | -------------------------------------------------------------------------------- /0x09-static_libraries/0-memset.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _memset - Fills the first n bytes of the memory area 5 | * pointed to by @s with the constant byte @c 6 | * @s: A pointer to the memory area to be filled. 7 | * @b: The character to fill the memory area with. 8 | * @n: The number of bytes to be filled. 9 | * 10 | * Return: A pointer to the filled memory area @s. 11 | */ 12 | 13 | char *_memset(char *s, char b, unsigned int n) 14 | { 15 | unsigned int i; 16 | 17 | for (i = 0; i < n; i++) 18 | s[i] = b; 19 | 20 | return (s); 21 | } 22 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-main.c: -------------------------------------------------------------------------------- 1 | #include "3-calc.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * main - ... 7 | * @argc: ... 8 | * @argv: ... 9 | * 10 | * Return: ... 11 | */ 12 | int main(int argc, char *argv[]) 13 | { 14 | int (*oprt)(int, int); 15 | 16 | if (argc != 4) 17 | { 18 | printf("Error\n"); 19 | exit(98); 20 | } 21 | 22 | oprt = get_op_func(argv[2]); 23 | 24 | if (!oprt) 25 | { 26 | printf("Error\n"); 27 | exit(99); 28 | } 29 | 30 | printf("%d\n", oprt(atoi(argv[1]), atoi(argv[3]))); 31 | return (0); 32 | } 33 | -------------------------------------------------------------------------------- /0x10-variadic_functions/0-sum_them_all.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * sum_them_all - sum of all its parameters 7 | * @n: n args 8 | * Return: sum or if n == 0, return 0 9 | */ 10 | int sum_them_all(const unsigned int n, ...) 11 | { 12 | int sum = 0; 13 | unsigned int i; 14 | va_list arguments; 15 | 16 | if (n) 17 | { 18 | va_start(arguments, n); 19 | for (i = 0; i < n; i++) 20 | { 21 | sum += va_arg(arguments, int); 22 | } 23 | va_end(arguments); 24 | } 25 | return (sum); 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: pointer to the first node in the list 6 | * 7 | * Return: pointer to the first node in the new list 8 | */ 9 | listint_t *reverse_listint(listint_t **head) 10 | { 11 | listint_t *prev = NULL; 12 | listint_t *next = NULL; 13 | 14 | while (*head) 15 | { 16 | next = (*head)->next; 17 | (*head)->next = prev; 18 | prev = *head; 19 | *head = next; 20 | } 21 | 22 | *head = prev; 23 | 24 | return (*head); 25 | } 26 | -------------------------------------------------------------------------------- /0x0F-function_pointers/2-int_index.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "function_pointers.h" 3 | /** 4 | * int_index - searches for an integer 5 | * @array: array with data 6 | * @size: size of the array 7 | * @cmp: function pointer 8 | * Return: count of integer 9 | */ 10 | int int_index(int *array, int size, int (*cmp)(int)) 11 | { 12 | int i; 13 | 14 | if (size <= 0) 15 | return (-1); 16 | if (array == NULL || cmp == NULL) 17 | return (-1); 18 | 19 | for (i = 0; i < size; i++) 20 | { 21 | if (cmp(array[i]) == 1) 22 | return (i); 23 | } 24 | return (-1); 25 | } 26 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-get_op_func.c: -------------------------------------------------------------------------------- 1 | #include "3-calc.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * get_op_func - ... 7 | * @s: ... 8 | * 9 | * Return: ... 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 | int i = 0; 22 | 23 | while (i < 5) 24 | { 25 | if (strcmp(s, ops[i].op) == 0) 26 | return (ops[i].f); 27 | i++; 28 | } 29 | 30 | return (0); 31 | } 32 | -------------------------------------------------------------------------------- /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 (a > b && c > a) 20 | { 21 | largest = c; 22 | } 23 | else if (b > c) 24 | { 25 | largest = b; 26 | } 27 | else 28 | { 29 | largest = c; 30 | } 31 | return (largest); 32 | } 33 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/5-flip_bits.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * flip_bits - number of bits you would need to flip 5 | * to get from one number to another 6 | * @n: parameter 7 | * @m: destiny 8 | * Return: nflips 9 | */ 10 | unsigned int flip_bits(unsigned long int n, unsigned long int m) 11 | { 12 | unsigned int i, nflips = 0; 13 | unsigned long int j = sizeof(unsigned long int) * 8; 14 | 15 | for (i = 0; i < j; i++) 16 | { 17 | if ((m & 1) != (n & 1)) 18 | nflips += 1; 19 | n = n >> 1; 20 | m = m >> 1; 21 | } 22 | return (nflips); 23 | } 24 | 25 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/0-print_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * print_dlistint - prints all the elements of a dlistint_t list. 5 | * @h: pointer to the head of the list 6 | * 7 | * Return: number of nodes 8 | */ 9 | 10 | size_t print_dlistint(const dlistint_t *h) 11 | { 12 | 13 | int counter = 0; 14 | 15 | if (h == NULL) 16 | return (counter); 17 | 18 | while (h->prev != NULL) 19 | h = h->prev; 20 | 21 | while (h != NULL) 22 | { 23 | printf("%d\n", h->n); 24 | h = h->next; 25 | counter++; 26 | } 27 | 28 | return (counter); 29 | } 30 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/100-atoi.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _atoi - convert a number in a string to an integer 4 | * @s: String to be converted 5 | * Return: converted integer OR 0 if none 6 | */ 7 | 8 | int _atoi(char *s) 9 | { 10 | int sign_cache = 1; 11 | int result = 0; 12 | 13 | while (s[0] != '\0') 14 | { 15 | if (s[0] == '-') 16 | sign_cache *= -1; 17 | else if (s[0] >= '0' && s[0] <= '9') 18 | result = (result * 10) + (s[0] - '0') * sign_cache; 19 | else if (result) 20 | break; 21 | 22 | s++; 23 | } 24 | 25 | return (result); 26 | } 27 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/2-add_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /** 5 | * add_nodeint - Add a new node at the beginning of a list. 6 | * @head: Address of the first node of a list. 7 | * @n: Integer to insert into the new node. 8 | * Return: Address of the new node. 9 | **/ 10 | 11 | listint_t *add_nodeint(listint_t **head, const int n) 12 | { 13 | listint_t *tp; 14 | 15 | tp = malloc(sizeof(listint_t)); 16 | if (tp == NULL) 17 | return (NULL); 18 | 19 | tp->n = n; 20 | tp->next = *head; 21 | *head = tp; 22 | return (*head); 23 | } 24 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/5-strstr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strstr - Locating a substring 5 | * 6 | * @needle: Occurance to find 7 | * 8 | * @haystack: String to search 9 | * 10 | * Return: Pointer 11 | */ 12 | 13 | char *_strstr(char *haystack, char *needle) 14 | { 15 | while (*haystack) 16 | { 17 | char *src = haystack; 18 | char *sub = needle; 19 | 20 | while (*haystack && *sub && *haystack == *sub) 21 | { 22 | haystack++; 23 | sub++; 24 | } 25 | if (!*sub) 26 | return (src); 27 | haystack = src + 1; 28 | } 29 | return (0); 30 | } 31 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/0-binary_to_uint.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * binary_to_uint - converts a binary number to unsigned int 5 | * @b: string containing the binary number 6 | * Return: the unsinged int number 7 | */ 8 | unsigned int binary_to_uint(const char *b) 9 | { 10 | int i; 11 | unsigned int converted_num = 0; 12 | 13 | if (!b) 14 | return (0); 15 | 16 | for (i = 0; b[i]; i++) 17 | { 18 | if (b[i] < '0' || b[i] > '1') 19 | return (0); 20 | converted_num = 2 * converted_num + (b[i] - '0'); 21 | } 22 | return (converted_num); 23 | } 24 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/0-print_list.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | #include 4 | #include 5 | /** 6 | * print_list - Prints all the elements of a list_t list. 7 | * @h: The list_t list. 8 | * 9 | * Return: The number of nodes in h. 10 | */ 11 | 12 | size_t print_list(const list_t *h) 13 | { 14 | size_t nodes = 0; 15 | 16 | while (h) 17 | { 18 | if (h->str == NULL) 19 | printf("[0] (nil)\n"); 20 | 21 | else 22 | printf("[%d] %s\n", h->len, h->str); 23 | 24 | nodes++; 25 | h = h->next; 26 | } 27 | 28 | return (nodes); 29 | } 30 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/7-print_diagonal.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * print_diagonal - draws a diagonal line on the terminal. 6 | * @n: The number of \ characters to be printed. 7 | */ 8 | 9 | void print_diagonal(int n) 10 | { 11 | int line, spaces; 12 | 13 | if (n > 0) 14 | { 15 | for (line = 0; line < n; line++) 16 | { 17 | for (spaces = 0; spaces < line; spaces++) 18 | { 19 | putchar(' '); 20 | } 21 | putchar('\\'); 22 | if (line == n - 1) 23 | continue; 24 | putchar('\n'); 25 | } 26 | } 27 | putchar('\n'); 28 | } 29 | -------------------------------------------------------------------------------- /0x09-static_libraries/4-strpbrk.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strpbrk - Searches a string for any of a set of bytes. 5 | * @s: The string to be searched. 6 | * @accept: The set of bytes to be searched for. 7 | * 8 | * Return: If a set is matched - a pointer to the matched byte. 9 | * If no set is matched - NULL. 10 | */ 11 | char *_strpbrk(char *s, char *accept) 12 | { 13 | int index; 14 | 15 | while (*s) 16 | { 17 | for (index = 0; accept[index]; index++) 18 | { 19 | if (*s == accept[index]) 20 | return (s); 21 | } 22 | 23 | s++; 24 | } 25 | return ('\0'); 26 | } 27 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/1-strncat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strncat - two words 4 | * @dest : pointer to char param 5 | * @src : pointer to char param 6 | * @n : int parameter 7 | * Return: *dest 8 | */ 9 | 10 | char *_strncat(char *dest, char *src, int n) 11 | { 12 | int m; 13 | int i; 14 | 15 | m = 0; 16 | 17 | for (i = 0; i < 1000; i++) 18 | { 19 | if (dest[i] == '\0') 20 | { 21 | break; 22 | } 23 | m++; 24 | } 25 | 26 | for (i = 0; src[i] != '\0' && i < n; i++) 27 | { 28 | dest[m + i] = src[i]; 29 | } 30 | dest[m + i] = '\0'; 31 | return (dest); 32 | } 33 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/6-cap_string.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * *cap_string - this is awesome 6 | * @s: pointer to char params 7 | * 8 | * Return: *s 9 | */ 10 | 11 | char *cap_string(char *s) 12 | { 13 | int i, j; 14 | char delimeters[] = " \t\n,;.!?\"(){}"; 15 | 16 | for (i = 0; s[i] != '\0'; i++) 17 | { 18 | if (s[0] >= 97 && s[0] <= 122) 19 | s[0] = s[0] - 32; 20 | for (j = 0; delimeters[j] != '\0'; j++) 21 | if (s[i] == delimeters[j] && s[i + 1] >= 97 && s[i + 1] <= 122) 22 | s[i + 1] = s[i + 1] - 32; 23 | } 24 | return (s); 25 | } 26 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/0-memset.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _memset - fills the first @n bytes of the memory area pointed 5 | * to by @s with the constant byte of @b 6 | * 7 | * @s: Pointer to the memory area to be filled. 8 | * 9 | * @b: Value that is to fill up memory area @s 10 | * 11 | * @n: number of bytes of memory area @s to be filled with @b 12 | * 13 | * Return: Pointer to the memory area @s 14 | */ 15 | 16 | char *_memset(char *s, char b, unsigned int n) 17 | { 18 | unsigned int i; 19 | 20 | for (i = 0; i < n; i++) 21 | { 22 | *(s + i) = b; 23 | } 24 | 25 | return (s); 26 | } 27 | -------------------------------------------------------------------------------- /0x0A-argc_argv/4-add.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | *main - adds positive numbers 6 | *@argc: number of arguments 7 | *@argv: array of arguments 8 | *Return: 0 on success, 1 on failure 9 | */ 10 | 11 | int main(int argc, char *argv[]) 12 | { 13 | int i, j, sum = 0; 14 | 15 | for (i = 1; i < argc; i++) 16 | { 17 | for (j = 0; argv[i][j] != '\0'; j++) 18 | { 19 | if (argv[i][j] < '0' || argv[i][j] > '9') 20 | { 21 | printf("Error\n"); 22 | return (1); 23 | } 24 | } 25 | 26 | sum += atoi(argv[i]); 27 | } 28 | 29 | printf("%d\n", sum); 30 | 31 | return (0); 32 | } 33 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/9-times_table.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * times_table - Prints the nine times table starting from 0 5 | * 6 | * Return: void 7 | */ 8 | 9 | void times_table(void) 10 | { 11 | int a, b, c; 12 | 13 | for (a = 0; a <= 9; a++) 14 | { 15 | _putchar(48); 16 | for (b = 1; b <= 9; b++) 17 | { 18 | c = a * b; 19 | _putchar(44); 20 | _putchar(32); 21 | if (c <= 9) 22 | { 23 | _putchar(32); 24 | _putchar(c + 48); 25 | } 26 | else 27 | { 28 | _putchar((c / 10) + 48); 29 | _putchar((c % 10) + 48); 30 | } 31 | } 32 | _putchar('\n'); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/100-rot13.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * rot13 - encoder rot13 6 | * @s: pointer to string params 7 | * 8 | * Return: *s 9 | */ 10 | 11 | char *rot13(char *s) 12 | { 13 | int i; 14 | int j; 15 | char data1[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; 16 | char datarot[] = "NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm"; 17 | 18 | for (i = 0; s[i] != '\0'; i++) 19 | { 20 | for (j = 0; j < 52; j++) 21 | { 22 | if (s[i] == data1[j]) 23 | { 24 | s[i] = datarot[j]; 25 | break; 26 | } 27 | } 28 | } 29 | return (s); 30 | } 31 | -------------------------------------------------------------------------------- /0x10-variadic_functions/1-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | #include 3 | #include 4 | /** 5 | * print_numbers - prints numbers & new line at the end 6 | * @separator: string 7 | * @n: number of elements 8 | * Return: void 9 | */ 10 | void print_numbers(const char *separator, const unsigned int n, ...) 11 | { 12 | va_list ap; 13 | unsigned int i; 14 | 15 | va_start(ap, n); 16 | 17 | for (i = 0; i < n; i++) 18 | { 19 | printf("%d", va_arg(ap, int)); 20 | 21 | if (separator != NULL && i != (n - 1)) 22 | { 23 | printf("%s", separator); 24 | } 25 | } 26 | putchar('\n'); 27 | va_end(ap); 28 | } 29 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/101-keygen.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /** 6 | * main - generates random valid passwords 7 | * 8 | * Return: Always 0 9 | */ 10 | 11 | int main(void) 12 | { 13 | char k[200]; 14 | int num = 0; 15 | int random = 0; 16 | char *key = k; 17 | 18 | srand(time(NULL)); 19 | 20 | while (num < 2645) 21 | { 22 | random = rand() % 122; 23 | 24 | if (random > 32) 25 | { 26 | *key = random; 27 | key = key + 1; 28 | num += random; 29 | } 30 | } 31 | 32 | *key = (2772 - num); 33 | *(key + 1) = '\n'; 34 | printf("%s", k); 35 | return (0); 36 | } 37 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-strcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strcmp - Compares pointers to two strings. 5 | * @s1: A pointer to the first string to be compared. 6 | * @s2: A pointer to the second string to be compared. 7 | * 8 | * Return: If str1 < str2, the negative difference of 9 | * the first unmatched characters. 10 | * If str1 == str2, 0. 11 | * If str1 > str2, the positive difference of 12 | * wthe first unmatched characters. 13 | */ 14 | int _strcmp(char *s1, char *s2) 15 | { 16 | while (*s1 && *s2 && *s1 == *s2) 17 | { 18 | s1++; 19 | s2++; 20 | } 21 | 22 | return (*s1 - *s2); 23 | } 24 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/7-get_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /** 5 | * get_nodeint_at_index - Find node at position index in a list. 6 | * @head: Address of the first node in a list. 7 | * @index: Position of a the node to find (starting from 0). 8 | * Return: Pointer to he found node 9 | **/ 10 | 11 | 12 | listint_t *get_nodeint_at_index(listint_t *head, unsigned int index) 13 | { 14 | unsigned int y = 0; 15 | 16 | if (head == NULL) 17 | return (NULL); 18 | for (y = 0; y < index; y++) 19 | { 20 | head = head->next; 21 | if (head == NULL) 22 | return (NULL); 23 | } 24 | return (head); 25 | } 26 | -------------------------------------------------------------------------------- /0x09-static_libraries/1-strncat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strncat - Concatenates two strings using at most 5 | * an inputted number of bytes from src. 6 | * @dest: The string to be appended upon. 7 | * @src: The string to be appended to dest. 8 | * @n: The number of bytes from src to be appended to dest. 9 | * 10 | * Return: A pointer to the resulting string dest. 11 | */ 12 | char *_strncat(char *dest, char *src, int n) 13 | { 14 | int index = 0, dest_len = 0; 15 | 16 | while (dest[index++]) 17 | dest_len++; 18 | for (index = 0; src[index] && index < n; index++) 19 | dest[dest_len++] = src[index]; 20 | return (dest); 21 | } 22 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/2-add_node.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * add_node - adds a new node at the beginning of a sigly linked list 4 | * @head: double pointer to the list_t list 5 | * @str: new string to add in the node 6 | * Return: the address of the new element, or NULL if it fails 7 | */ 8 | list_t *add_node(list_t **head, const char *str) 9 | { 10 | list_t *new; 11 | unsigned int len = 0; 12 | 13 | while (str[len]) 14 | len++; 15 | 16 | new = malloc(sizeof(list_t)); 17 | if (!new) 18 | return (NULL); 19 | 20 | new->str = strdup(str); 21 | new->len = len; 22 | new->next = (*head); 23 | (*head) = new; 24 | 25 | return (*head); 26 | } 27 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/8-print_diagsums.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * print_diagsums - Prints the sum of the two diagonals 6 | * of a square matrix of integers. 7 | * @a: The matrix of integers. 8 | * @size: The size of the matrix. 9 | */ 10 | void print_diagsums(int *a, int size) 11 | { 12 | int index, sum1 = 0, sum2 = 0; 13 | 14 | for (index = 0; index < size; index++) 15 | { 16 | sum1 += a[index]; 17 | a += size; 18 | } 19 | 20 | a -= size; 21 | 22 | for (index = 0; index < size; index++) 23 | { 24 | sum2 += a[index]; 25 | a -= size; 26 | } 27 | 28 | printf("%d, %d\n", sum1, sum2); 29 | } 30 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/101-print_listint_safe.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * print_listint_safe - prints a linked list, safely 5 | * @head: list of type listint_t to print 6 | * 7 | * Return: number of nodes in the list 8 | */ 9 | size_t print_listint_safe(const listint_t *head) 10 | { 11 | size_t num = 0; 12 | long int diff; 13 | 14 | while (head) 15 | { 16 | diff = head - head->next; 17 | num++; 18 | printf("[%p] %d\n", (void *)head, head->n); 19 | if (diff > 0) 20 | head = head->next; 21 | else 22 | { 23 | printf("-> [%p] %d\n", (void *)head->next, head->next->n); 24 | break; 25 | } 26 | } 27 | 28 | return (num); 29 | } 30 | -------------------------------------------------------------------------------- /0x1A-hash_tables/5-hash_table_print.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | /** 3 | * hash_table_print - prints hashtable 4 | * @ht: the hash table you want to print 5 | */ 6 | void hash_table_print(const hash_table_t *ht) 7 | { 8 | 9 | unsigned long int i; 10 | hash_node_t *node; 11 | short int comma = 0; 12 | 13 | if (ht == NULL) 14 | return; 15 | 16 | putchar('{'); 17 | for (i = 0; i < ht->size; i++) 18 | { 19 | node = ht->array[i]; 20 | while (node != NULL) 21 | { 22 | if (comma) 23 | printf(", "); 24 | 25 | printf("'%s': '%s'", node->key, node->value); 26 | node = node->next; 27 | 28 | if (comma == 0) 29 | comma = 1; 30 | } 31 | } 32 | printf("}\n"); 33 | } 34 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/1-last_digit.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | /** 5 | * main - Entry point 6 | * 7 | * Return: Always 0 (Success) 8 | */ 9 | int main(void) 10 | { 11 | int n, num; 12 | 13 | srand(time(0)); 14 | n = rand() - RAND_MAX / 2; 15 | /* your code goes there */ 16 | num = n % 10; 17 | if (num > 5) 18 | { 19 | printf("Last digit of %d is %d and is greater than 5\n", n, num); 20 | } 21 | else if (num == 0) 22 | { 23 | printf("Last digit of %d is %d and is 0\n", n, num); 24 | } 25 | else if (num < 6) 26 | { 27 | printf("Last digit of %d is %d and is less than 6 and not 0\n", n, num); 28 | } 29 | return (0); 30 | } 31 | -------------------------------------------------------------------------------- /0x10-variadic_functions/2-print_strings.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | 3 | /** 4 | * print_strings - Print numbers using a separator 5 | * @separator: The string separator 6 | * @n: The quantity of numbers 7 | * @...: The list of numbers 8 | * 9 | * Return: Nothing 10 | */ 11 | void print_strings(const char *separator, const unsigned int n, ...) 12 | { 13 | va_list valist; 14 | unsigned int i; 15 | char *p; 16 | 17 | va_start(valist, n); 18 | 19 | for (i = 0; i < n; i++) 20 | { 21 | if (separator != NULL && i != 0) 22 | printf("%s", separator); 23 | p = va_arg(valist, char *); 24 | printf("%s", (p == NULL) ? "(nil)" : p); 25 | } 26 | printf("\n"); 27 | va_end(valist); 28 | } 29 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | listint_t *head; 14 | listint_t *new; 15 | listint_t hello = {8, NULL}; 16 | size_t n; 17 | 18 | head = &hello; 19 | new = malloc(sizeof(listint_t)); 20 | if (new == NULL) 21 | { 22 | printf("Error\n"); 23 | return (1); 24 | } 25 | new->n = 9; 26 | new->next = head; 27 | head = new; 28 | n = print_listint(head); 29 | printf("-> %lu elements\n", n); 30 | free(new); 31 | return (0); 32 | } 33 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/2-add_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_dnodeint - adds a new node at the beginning of a dlistint_t list. 5 | * @head: double pointer to the head of the list 6 | * @n: data to be added 7 | * 8 | * Return: he address of the new element, or NULL if it failed 9 | */ 10 | 11 | dlistint_t *add_dnodeint(dlistint_t **head, const int n) 12 | { 13 | dlistint_t *new_node; 14 | 15 | new_node = malloc(sizeof(dlistint_t)); 16 | if (new_node == NULL) 17 | return (NULL); 18 | 19 | new_node->n = n; 20 | new_node->next = *head; 21 | new_node->prev = NULL; 22 | if (*head != NULL) 23 | (*head)->prev = new_node; 24 | *head = new_node; 25 | return (new_node); 26 | } 27 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/dog.h: -------------------------------------------------------------------------------- 1 | #ifndef DOG_H 2 | #define DOG_H 3 | 4 | #include 5 | #include 6 | 7 | /** 8 | *struct dog - structure representing a dog 9 | *@name: the dogs' name 10 | *@age: the dog's age 11 | *@owner: owner's name 12 | */ 13 | struct dog 14 | { 15 | char *name; 16 | float age; 17 | char *owner; 18 | }; 19 | 20 | /** 21 | *dog_t - typedef for struct dog 22 | */ 23 | typedef struct dog dog_t; 24 | 25 | void init_dog(struct dog *d, char *name, float age, char *owner); 26 | void print_dog(struct dog *d); 27 | dog_t *new_dog(char *name, float age, char *owner); 28 | char *_strcpy(char *dest, char *src); 29 | int _strlen(char *s); 30 | void free_dog(dog_t *d); 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-strspn.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strspn - Gets the length of a prefix substring. 5 | * @s: The string to be searched. 6 | * @accept: The prefix to be measured. 7 | * 8 | * Return: The number of bytes in s which 9 | * consist only of bytes from accept. 10 | */ 11 | 12 | unsigned int _strspn(char *s, char *accept) 13 | { 14 | unsigned int bytes = 0; 15 | int index; 16 | 17 | while (*s) 18 | { 19 | for (index = 0; accept[index]; index++) 20 | { 21 | if (*s == accept[index]) 22 | { 23 | bytes++; 24 | break; 25 | } 26 | 27 | else if (accept[index + 1] == '\0') 28 | return (bytes); 29 | } 30 | 31 | s++; 32 | } 33 | 34 | return (bytes); 35 | } 36 | -------------------------------------------------------------------------------- /0x1C-makefiles/5-island_perimeter.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | """ 3 | Grid perimeter calculator module 4 | """ 5 | 6 | 7 | def island_perimeter(grid) -> int: 8 | """"Culculate the perimter of a grid 9 | Args: 10 | grid (list): A list of lists 11 | """ 12 | width = len(grid[0]) 13 | height = len(grid) 14 | edges = 0 15 | size = 0 16 | 17 | for i in range(height): 18 | for j in range(width): 19 | if grid[i][j] == 1: 20 | size += 1 21 | if (j > 0 and grid[i][j - 1] == 1): 22 | edges += 1 23 | if (i > 0 and grid[i - 1][j] == 1): 24 | edges += 1 25 | return (size * 4 - edges * 2) 26 | -------------------------------------------------------------------------------- /0x09-static_libraries/2-strncpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strncpy - Copies at most an inputted number 5 | * of bytes from string src into dest. 6 | * @dest: The buffer storing the string copy. 7 | * @src: The source string. 8 | * @n: The maximum number of bytes to copied from src. 9 | * 10 | * Return: A pointer to the resulting string dest. 11 | */ 12 | char *_strncpy(char *dest, char *src, int n) 13 | { 14 | int index = 0, src_len = 0; 15 | 16 | while (src[index++]) 17 | src_len++; 18 | 19 | for (index = 0; src[index] && index < n; index++) 20 | dest[index] = src[index]; 21 | 22 | for (index = src_len; index < n; index++) 23 | dest[index] = '\0'; 24 | 25 | return (dest); 26 | } 27 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/README.md: -------------------------------------------------------------------------------- 1 | ![img](https://assets.imaginablefutures.com/media/images/ALX_Logo.max-200x150.png) 2 | > Bit manipulation 3 | 4 | ## Intro 5 | In this section, we will explore bit manipulation in C. 6 | 7 | ## Resources 8 | 1. [Google](https://www.google.com/webhp?q=bit+manipulation+C) 9 | 2. [Youtube](https://www.youtube.com/results?search_query=bitwise+operators+in+c) 10 | 11 | ## Learning Objectives 12 | 13 | At the end of this session, you should be able to [explain to anyone](https://fs.blog/feynman-learning-technique/) the following concepts without the help of Google 14 | 15 | * [X] Look for the right source of information without too much help 16 | * [X] How to manipulate bits and use bitwise operators -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/5-get_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * get_dnodeint_at_index - eturns the nth node of a dlistint_t linked list. 5 | * @head: double pointer to the head of the list 6 | * @index: index of the node to return 7 | * 8 | * Return: if the node does not exist, return NULL 9 | * where index is the index of the node, starting from 0 10 | */ 11 | 12 | dlistint_t *get_dnodeint_at_index(dlistint_t *head, unsigned int index) 13 | { 14 | dlistint_t *current; 15 | unsigned int i; 16 | 17 | current = head; 18 | i = 0; 19 | while (current != NULL) 20 | { 21 | if (i == index) 22 | return (current); 23 | current = current->next; 24 | i++; 25 | } 26 | return (NULL); 27 | } 28 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/102-free_listint_safe.c: -------------------------------------------------------------------------------- 1 | 2 | #include "lists.h" 3 | #include 4 | /** 5 | * free_listint_safe - frees a linked list 6 | * @h: pointer to the first node in the linked list 7 | * 8 | * Return: number of elements in the freed list 9 | */ 10 | size_t free_listint_safe(listint_t **h) 11 | { 12 | size_t len = 0; 13 | int diff; 14 | listint_t *temp; 15 | 16 | if (!h || !*h) 17 | return (0); 18 | 19 | while (*h) 20 | { 21 | diff = *h - (*h)->next; 22 | if (diff > 0) 23 | { 24 | temp = (*h)->next; 25 | *h = temp; 26 | len++; 27 | } 28 | else 29 | { 30 | *h = NULL; 31 | len++; 32 | break; 33 | } 34 | } 35 | 36 | *h = NULL; 37 | 38 | return (len); 39 | } 40 | -------------------------------------------------------------------------------- /0x08-recursion/6-is_prime_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | int tmp_prime(int n, int i); 4 | 5 | /** 6 | * divisors - number is prime? 7 | * @n: integer params 8 | * @m: integer params 9 | * Return: boolean 10 | */ 11 | 12 | int divisors(int n, int m) 13 | { 14 | if (m % n == 0) 15 | { 16 | return (0); 17 | } 18 | else if (m / 2 > n) 19 | { 20 | return (divisors(n + 2, m)); 21 | } 22 | else 23 | { 24 | return (1); 25 | } 26 | } 27 | 28 | /** 29 | * is_prime_number - prime 30 | * @n: integer params 31 | * Return: recursion 32 | */ 33 | 34 | int is_prime_number(int n) 35 | { 36 | if ((!(n % 2) && n != 2) || n < 2) 37 | { 38 | return (0); 39 | } 40 | else 41 | { 42 | return (divisors(3, n)); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /0x09-static_libraries/main.h: -------------------------------------------------------------------------------- 1 | int _putchar(char c); 2 | int _islower(int c); 3 | int _isalpha(int c); 4 | int _abs(int n); 5 | int _isupper(int c); 6 | int _isdigit(int c); 7 | int _strlen(char *s); 8 | void _puts(char *s); 9 | char *_strcpy(char *dest, char *src); 10 | int _atoi(char *s); 11 | char *_strcat(char *dest, char *src); 12 | char *_strncat(char *dest, char *src, int n); 13 | char *_strncpy(char *dest, char *src, int n); 14 | int _strcmp(char *s1, char *s2); 15 | char *_memset(char *s, char b, unsigned int n); 16 | char *_memcpy(char *dest, char *src, unsigned int n); 17 | char *_strchr(char *s, char c); 18 | unsigned int _strspn(char *s, char *accept); 19 | char *_strpbrk(char *s, char *accept); 20 | char *_strstr(char *haystack, char *needle); 21 | -------------------------------------------------------------------------------- /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: linked list to search for 6 | * 7 | * Return: address of the node where the loop starts, or NULL 8 | */ 9 | listint_t *find_listint_loop(listint_t *head) 10 | { 11 | listint_t *slow = head; 12 | listint_t *fast = head; 13 | 14 | if (!head) 15 | return (NULL); 16 | 17 | while (slow && fast && fast->next) 18 | { 19 | fast = fast->next->next; 20 | slow = slow->next; 21 | if (fast == slow) 22 | { 23 | slow = head; 24 | while (slow != fast) 25 | { 26 | slow = slow->next; 27 | fast = fast->next; 28 | } 29 | return (fast); 30 | } 31 | } 32 | 33 | return (NULL); 34 | } 35 | -------------------------------------------------------------------------------- /0x1A-hash_tables/0-hash_table_create.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_create - function to create a hash table 5 | * @size: size of the array 6 | * 7 | * Return: pointer to the newly created hash table 8 | */ 9 | hash_table_t *hash_table_create(unsigned long int size) 10 | { 11 | hash_table_t *hash_table; 12 | unsigned long int j; 13 | 14 | hash_table = malloc(sizeof(hash_table_t)); 15 | if (hash_table == NULL) 16 | return (NULL); 17 | hash_table->size = size; 18 | hash_table->array = malloc(size * sizeof(hash_node_t *)); 19 | if (hash_table->array == NULL) 20 | { 21 | free(hash_table); 22 | return (NULL); 23 | } 24 | for (j = 0 ; j < size; j++) 25 | hash_table->array[j] = NULL; 26 | return (hash_table); 27 | } 28 | -------------------------------------------------------------------------------- /0x0B-malloc_free/1-strdup.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * _strdup - returns a pointer to a newly allocated 5 | *space in memory, which contains a copy of the 6 | *string given as a parameter. 7 | *@str:String to be copied 8 | * 9 | *Return: NULL in case of error, pointer to allocated 10 | */ 11 | 12 | char *_strdup(char *str) 13 | { 14 | char *cpy; 15 | int index, len; 16 | 17 | if (str == NULL) 18 | return (NULL); 19 | 20 | for (index = 0; str[index]; index++) 21 | len++; 22 | cpy = malloc(sizeof(char) * (len + 1)); 23 | 24 | if (cpy == NULL) 25 | return (NULL); 26 | 27 | for (index = 0; str[index]; index++) 28 | { 29 | cpy[index] = str[index]; 30 | } 31 | 32 | cpy[len] = '\0'; 33 | 34 | return (cpy); 35 | 36 | } 37 | -------------------------------------------------------------------------------- /0x0B-malloc_free/0-create_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | *create_array - create memory of size size 5 | * initializes it with a specific char 6 | *@size: size of amount of memory to allocate in bytes 7 | *@c: char to initialize with the array 8 | *Return: If size == 0 or the function fails - NULL. 9 | * Otherwise - a pointer to the array./ 10 | */ 11 | char *create_array(unsigned int size, char c) 12 | { 13 | char *arrays; 14 | unsigned int pos; 15 | 16 | if (size == 0) 17 | { 18 | return (NULL); 19 | } 20 | arrays = malloc(sizeof(char) * size); 21 | 22 | if (arrays == NULL) 23 | { 24 | return (NULL); 25 | } 26 | for (pos = 0; pos < size; pos++) 27 | { 28 | arrays[pos] = c; 29 | } 30 | return (arrays); 31 | } 32 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/3-add_nodeint_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /** 5 | * add_nodeint_end - Add a new node at the end of a list. 6 | * @head: Address of the first node of a list. 7 | * @n: Integer to insert in the new node. 8 | * Return: Address of the new node. 9 | **/ 10 | 11 | listint_t *add_nodeint_end(listint_t **head, const int n) 12 | { 13 | listint_t *temp, *temp2; 14 | 15 | temp = malloc(sizeof(listint_t)); 16 | if (temp == NULL) 17 | return (NULL); 18 | 19 | temp->n = n; 20 | temp->next = NULL; 21 | 22 | if (*head == NULL) 23 | { 24 | *head = temp; 25 | return (temp); 26 | } 27 | 28 | temp2 = *head; 29 | while (temp2->next) 30 | temp2 = temp2->next; 31 | temp2->next = temp; 32 | return (temp); 33 | } 34 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | ![img](https://assets.imaginablefutures.com/media/images/ALX_Logo.max-200x150.png) 2 | >More pointers arrays and strings 3 | 4 | ## Intro 5 | In this session, we will explore pointers, arrays and strings in details 6 | 7 | ## Resources 8 | 1. [Introduction to pointers](https://byjus.com/gate/pointers-in-c/) 9 | 2. [Introduction to arrays](https://www.tutorialspoint.com/cprogramming/c_arrays.htm) 10 | 3. [Strings in C](https://www.tutorialspoint.com/cprogramming/c_strings.htm) 11 | 12 | ## Learning objectives 13 | * [X] What are pointers and how to use them 14 | * [X] What are arrays and how to use them 15 | * [X] What are the differences between pointers and arrays 16 | * [X] How to use strings and how to manipulate them 17 | * [X] Scope of variables -------------------------------------------------------------------------------- /0x03-debugging/README.md: -------------------------------------------------------------------------------- 1 | ![img](https://assets.imaginablefutures.com/media/images/ALX_Logo.max-200x150.png) 2 | >Debuging is like being a detective in a crime movie where you are also the murderer 3 | 4 | ## Intro 5 | In this section, we will explore debugging and debugging techniques 6 | 7 | ## Resources 8 | 1. [Debugging](https://en.wikipedia.org/wiki/Debugging) 9 | 2. [Rubber Ducky Debugging](https://www.thoughtfulcode.com/rubber-duck-debugging-psychology/) 10 | 11 | ## Learning objectives 12 | By the end of this session, you should be able to [explain to anyone](https://fs.blog/feynman-learning-technique/) without the help of Google the following concepts 13 | 14 | * [X] What is debugging 15 | * [X] What are some methods of debugging manually 16 | * [X] How to read the error messages -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | ![img](https://assets.imaginablefutures.com/media/images/ALX_Logo.max-200x150.png) 2 | >Pointers, arrays and strings 3 | 4 | ## Intro 5 | In this session, we will explore pointers, arrays and strings. 6 | 7 | ## Resources 8 | 1. [Introduction to pointers](https://byjus.com/gate/pointers-in-c/) 9 | 2. [Introduction to arrays](https://www.tutorialspoint.com/cprogramming/c_arrays.htm) 10 | 3. [Strings in C](https://www.tutorialspoint.com/cprogramming/c_strings.htm) 11 | 12 | 13 | ## Learning objectives 14 | * [X] What are pointers and how to use them 15 | * [X] What are arrays and how to use them 16 | * [X] What are the differences between pointers and arrays 17 | * [X] How to use strings and how to manipulate them 18 | * [X] Scope of variables 19 | 20 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/3-array_range.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * array_range - Creates an array of integers ordered 6 | * from min to max, inclusive. 7 | * @min: The first value of the array. 8 | * @max: The last value of the array. 9 | * 10 | * Return: If min > max or the function fails - NULL. 11 | * Otherwise - a pointer to the newly created array. 12 | */ 13 | int *array_range(int min, int max) 14 | { 15 | int *array, index, size; 16 | 17 | if (min > max) 18 | return (NULL); 19 | 20 | size = max - min + 1; 21 | 22 | array = malloc(sizeof(int) * size); 23 | 24 | if (array == NULL) 25 | return (NULL); 26 | 27 | for (index = 0; index < size; index++) 28 | array[index] = min++; 29 | 30 | return (array); 31 | } 32 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | ![img](https://assets.imaginablefutures.com/media/images/ALX_Logo.max-200x150.png) 2 | > Even more pointers arrays and strings 3 | 4 | ## Intro 5 | In this session, we will explore pointers, arrays and strings in more details 6 | 7 | ## Resources 8 | 1. [Introduction to pointers](https://byjus.com/gate/pointers-in-c/) 9 | 2. [Introduction to arrays](https://www.tutorialspoint.com/cprogramming/c_arrays.htm) 10 | 3. [Strings in C](https://www.tutorialspoint.com/cprogramming/c_strings.htm) 11 | 12 | ## Learning objectives 13 | * [X] What are pointers and how to use them 14 | * [X] What are arrays and how to use them 15 | * [X] What are the differences between pointers and arrays 16 | * [X] How to use strings and how to manipulate them 17 | * [X] Scope of variables 18 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/8-24_hours.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * jack_bauer - Print every minute of the day from 0:00 to 23:59 5 | * 6 | * Return: void 7 | */ 8 | 9 | void jack_bauer(void) 10 | { 11 | int hr1 = 0; 12 | int hr2 = 0; 13 | int sec1 = 0; 14 | int sec2 = 0; 15 | 16 | while (hr1 < 3) 17 | { 18 | while (hr2 <= 9) 19 | { 20 | if (hr1 == 2 && hr2 == 4) 21 | break; 22 | while (sec1 < 6) 23 | { 24 | 25 | while (sec2 <= 9) 26 | { 27 | _putchar(hr1 + '0'); 28 | _putchar(hr2 + '0'); 29 | _putchar(':'); 30 | _putchar(sec1 + '0'); 31 | _putchar(sec2 + '0'); 32 | _putchar('\n'); 33 | sec2++; 34 | } 35 | sec2 = 0; 36 | sec1++; 37 | } 38 | sec1 = 0; 39 | hr2++; 40 | } 41 | hr2 = 0; 42 | hr1++; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/README.md: -------------------------------------------------------------------------------- 1 | ![img](https://assets.imaginablefutures.com/media/images/ALX_Logo.max-200x150.png) 2 | > More Singly linked lists 3 | 4 | ## Intro 5 | In this session, we will explore linked lists data structures 6 | 7 | ## Resources 8 | Take a look at the following resources 9 | 1. [Singly Linkes lists](https://www.youtube.com/watch?v=udapt4FGY20) 10 | 2. [Youtube](https://www.youtube.com/results?search_query=linked+lists) 11 | 3. [Google](https://www.google.com/search?q=linked+lists) 12 | 13 | ## Learning objecives 14 | By the end of this session, you should be able to [Explain to anyone](https://fs.blog/feynman-learning-technique/) without the help of GOogle the following concepts. 15 | 16 | * [X] When and why using linked lists vs arrays 17 | * [X] How to build and use linked lists 18 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/0-linear.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * linear_search - function tolook for a value in an arrray of integers 5 | * @array: ptr to the first element in the array. 6 | * @size: number of elements in the array. 7 | * @value: Value to search for. 8 | * Return: (-1) If value is not in array or array is NULL. 9 | * Otherwise, first index of value 10 | * Description: Print each value in the array as it gets compared. 11 | */ 12 | int linear_search(int *array, size_t size, int value) 13 | { 14 | size_t index; 15 | 16 | if (array == NULL) 17 | return (-1); 18 | 19 | for (index = 0; index < size; index++) 20 | { 21 | printf("Value checked array[%ld] = [%d]\n", index, array[index]); 22 | if (array[index] == value) 23 | return (index); 24 | } 25 | 26 | return (-1); 27 | } 28 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/9-fizz_buzz.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints the numbers from 1 to 100, followed by a new line. 5 | * For multiples of three print Fizz instead of the number and 6 | * for the multiples of five print Buzz. 7 | * For numbers which are multiples of both three and five print FizzBuzz. 8 | * Return: Always 0. 9 | */ 10 | 11 | int main(void) 12 | { 13 | int n; 14 | 15 | for (n = 1; n <= 100; n++) 16 | { 17 | if (n == 1) 18 | { 19 | printf("%d", n); 20 | } 21 | else if ((n % 3 == 0) && (n % 5 == 0)) 22 | { 23 | printf(" FizzBuzz"); 24 | } 25 | else if (n % 3 == 0) 26 | { 27 | printf(" Fizz"); 28 | } 29 | else if (n % 5 == 0) 30 | { 31 | printf(" Buzz"); 32 | } 33 | else 34 | { 35 | printf(" %d", n); 36 | } 37 | } 38 | printf("\n"); 39 | return (0); 40 | } 41 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/README.md: -------------------------------------------------------------------------------- 1 | ![img](https://assets.imaginablefutures.com/media/images/ALX_Logo.max-200x150.png) 2 | > Doubly linked lists 3 | 4 | ## Intro 5 | In this session, we will take a look at doubly linked data structure 6 | 7 | ## Resources 8 | 1. [What are Doubly linked lists](https://www.youtube.com/watch?v=k0pjD12bzP0) 9 | 2. [Youtube](https://www.youtube.com/results?search_query=doubly+linked+lists+c) 10 | 3. [Google](https://www.google.com/search?q=doubly+linked+lists) 11 | 12 | ## Learning objectives 13 | By the end of this session, you should be able to [explain to anyone](https://fs.blog/feynman-learning-technique/) without the help of google the following concepts. 14 | 15 | * [X] What is a doubly linked list 16 | * [X] How to use doubly linked lists 17 | * [X] Start to look for the right source of information without too much help 18 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/2-calloc.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _calloc - Allocates memory for an array of a certain number 6 | * of elements each of an inputted byte size. 7 | * @nmemb: The number of elements. 8 | * @size: The byte size of each array element. 9 | * 10 | * Return: If nmemb = 0, size = 0, or the function fails - NULL. 11 | * Otherwise - a pointer to the allocated memory. 12 | */ 13 | void *_calloc(unsigned int nmemb, unsigned int size) 14 | { 15 | void *mem; 16 | char *filler; 17 | unsigned int index; 18 | 19 | if (nmemb == 0 || size == 0) 20 | return (NULL); 21 | 22 | mem = malloc(size * nmemb); 23 | 24 | if (mem == NULL) 25 | return (NULL); 26 | 27 | filler = mem; 28 | 29 | for (index = 0; index < (size * nmemb); index++) 30 | filler[index] = '\0'; 31 | 32 | return (mem); 33 | } 34 | -------------------------------------------------------------------------------- /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: name of the file 6 | * @text_content the NULL terminated string to add at the end of the file 7 | * Return: 1 on success and -1 on failure 8 | */ 9 | 10 | int append_text_to_file(const char *filename, char *text_content) 11 | { 12 | register int _file, _write, text_len = 0; 13 | 14 | if (!(filename)) 15 | { 16 | return (-1); 17 | } 18 | _file = open(filename, O_WRONLY, O_APPEND); 19 | if (_file == -1) 20 | { 21 | return (-1); 22 | } 23 | if (text_content) 24 | { 25 | while (text_content[text_len]) 26 | { 27 | text_len++; 28 | } 29 | _write = write(_file, text_content, text_len); 30 | if (_write == -1) 31 | { 32 | return (-1); 33 | } 34 | } 35 | close(_file); 36 | return (1); 37 | } 38 | -------------------------------------------------------------------------------- /0x09-static_libraries/5-strstr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strstr - Locates a substring. 5 | * @haystack: The string to be searched. 6 | * @needle: The substring to be located. 7 | * 8 | * Return: If the substring is located - a pointer to the beginning 9 | * of the located substring. 10 | * If the substring is not located - NULL. 11 | */ 12 | 13 | char *_strstr(char *haystack, char *needle) 14 | { 15 | int index; 16 | 17 | if (*needle == 0) 18 | return (haystack); 19 | 20 | while (*haystack) 21 | { 22 | index = 0; 23 | 24 | if (haystack[index] == needle[index]) 25 | { 26 | do { 27 | if (needle[index + 1] == '\0') 28 | return (haystack); 29 | 30 | index++; 31 | 32 | } while (haystack[index] == needle[index]); 33 | } 34 | 35 | haystack++; 36 | } 37 | 38 | return ('\0'); 39 | } 40 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef LISTS_H 2 | #define LISTS_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | /** structures here **/ 9 | /** 10 | * struct list_s - singly linked list 11 | * @str: string - (malloc'ed string) 12 | * @len: length of the string 13 | * @next: points to the next node 14 | * Description: singly linked list node structure 15 | * for the ALX project 16 | */ 17 | 18 | typedef struct list_s 19 | { 20 | char *str; 21 | unsigned int len; 22 | struct list_s *next; 23 | } list_t; 24 | 25 | /** function prototypes here**/ 26 | int _putchar(char); 27 | size_t print_list(const list_t *h); 28 | size_t list_len(const list_t *h); 29 | list_t *add_node(list_t **head, const char *str); 30 | list_t *add_node_end(list_t **head, const char *str); 31 | void free_list(list_t *head); 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/10-delete_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /** 5 | * delete_nodeint_at_index - Delete a node at a index positiion. 6 | * @head: First node address. 7 | * @index: Position of the node to delete. 8 | * Return: If success (1). 9 | **/ 10 | 11 | int delete_nodeint_at_index(listint_t **head, unsigned int index) 12 | { 13 | unsigned int i; 14 | listint_t *current, *next; 15 | 16 | if (head == NULL || *head == NULL) 17 | return (-1); 18 | if (index == 0) 19 | { 20 | next = (*head)->next; 21 | free(*head); 22 | *head = next; 23 | return (1); 24 | } 25 | current = *head; 26 | for (i = 0; i < index - 1; i++) 27 | { 28 | if (current->next == NULL) 29 | return (-1); 30 | current = current->next; 31 | } 32 | next = current->next; 33 | current->next = next->next; 34 | free(next); 35 | return (1); 36 | } 37 | -------------------------------------------------------------------------------- /0x1A-hash_tables/6-hash_table_delete.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | /** 3 | * hash_table_delete - deletes a hash table 4 | * @ht: the hash table you want to delete 5 | */ 6 | void hash_table_delete(hash_table_t *ht) 7 | { 8 | unsigned long int i; 9 | hash_node_t *node; 10 | 11 | if (ht == NULL) 12 | return; 13 | 14 | for (i = 0; i < ht->size; i++) 15 | { 16 | node = ht->array[i]; 17 | free_hash_list(node); 18 | } 19 | free(ht->array); 20 | free(ht); 21 | } 22 | 23 | 24 | /** 25 | * free_hash_list - frees a hash_node_t list 26 | * @head: head of linked list 27 | */ 28 | void free_hash_list(hash_node_t *head) 29 | { 30 | hash_node_t *current; 31 | hash_node_t *next; 32 | 33 | current = head; 34 | 35 | while (current != NULL) 36 | { 37 | next = current->next; 38 | free(current->key); 39 | free(current->value); 40 | free(current); 41 | current = next; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /0x1A-hash_tables/4-hash_table_get.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | /** 3 | * hash_table_get - return a value associated with a key. 4 | * @ht: the hash table to search 5 | * @key: the search key 6 | * Return: value['key'] if found, NULL if else 7 | */ 8 | char *hash_table_get(const hash_table_t *ht, const char *key) 9 | { 10 | unsigned long int idx; 11 | hash_node_t *node; 12 | 13 | if (ht == NULL || key == NULL || strlen(key) == 0) 14 | return (NULL); 15 | 16 | idx = key_index((unsigned char *) key, ht->size); 17 | 18 | if (ht->array[idx] == NULL || ht->array[idx] == 0) 19 | return (NULL); 20 | 21 | if (strcmp(ht->array[idx]->key, key) == 0) 22 | return (ht->array[idx]->value); 23 | node = ht->array[idx]; 24 | while (node != NULL) 25 | { 26 | if (strcmp(node->key, key) == 0) 27 | return (node->value); 28 | node = node->next; 29 | } 30 | 31 | return (NULL); 32 | } 33 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/3-add_dnodeint_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_dnodeint_end - adds a new node at the end of a dlistint_t list. 5 | * @head: double pointer to the head of the list 6 | * @n: integer to add to the new node 7 | * 8 | * Return: the address of the new element, or NULL if it failed 9 | */ 10 | 11 | dlistint_t *add_dnodeint_end(dlistint_t **head, const int n) 12 | { 13 | dlistint_t *new_node; 14 | 15 | new_node = malloc(sizeof(dlistint_t)); 16 | 17 | if (new_node == NULL) 18 | return (NULL); 19 | 20 | new_node->n = n; 21 | new_node->next = NULL; 22 | 23 | if (*head == NULL) 24 | { 25 | new_node->prev = NULL; 26 | *head = new_node; 27 | return (new_node); 28 | } 29 | 30 | while ((*head)->next != NULL) 31 | *head = (*head)->next; 32 | 33 | (*head)->next = new_node; 34 | new_node->prev = *head; 35 | 36 | return (new_node); 37 | } 38 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | #include 5 | #include 6 | 7 | /*function protypes here*/ 8 | int _putchar(char c); 9 | int _islower(int c); 10 | int _isalpha(int c); 11 | int _abs(int n); 12 | int _isupper(int c); 13 | int _isdigit(int c); 14 | int _strlen(char *s); 15 | void _puts(char *s); 16 | char *_strcpy(char *dest, char *src); 17 | int _atoi(char *s); 18 | char *_strcat(char *dest, char *src); 19 | char *_strncat(char *dest, char *src, int n); 20 | char *_strncpy(char *dest, char *src, int n); 21 | int _strcmp(char *s1, char *s2); 22 | char *_memset(char *s, char b, unsigned int n); 23 | char *_memcpy(char *dest, char *src, unsigned int n); 24 | char *_strchr(char *s, char c); 25 | unsigned int _strspn(char *s, char *accept); 26 | char *_strpbrk(char *s, char *accept); 27 | char *_strstr(char *haystack, char *needle); 28 | #endif 29 | -------------------------------------------------------------------------------- /0x15-file_io/1-create_file.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * create_file - create a file named filename 4 | * @filename: name of the file 5 | * @text_content: a NULL terminated string to write to the file 6 | * Return: 1 on success, -1 on failure (file can not be created, 7 | * file can not be written, write “fails”, etc…) 8 | */ 9 | 10 | int create_file(const char *filename, char *text_content) 11 | { 12 | register int _file, _write, text_len = 0; 13 | 14 | if (!(filename)) 15 | { 16 | return (-1); 17 | } 18 | _file = open(filename, O_RDWR | O_CREAT | O_TRUNC, 0600); 19 | if (_file == -1) 20 | { 21 | return (-1); 22 | } 23 | if (text_content) 24 | { 25 | while (text_content[text_len]) 26 | { 27 | text_len++; 28 | } 29 | _write = write(_file, text_content, text_len); 30 | if (_write == -1) 31 | { 32 | return (-1); 33 | } 34 | } 35 | close(_file); 36 | return (1); 37 | } 38 | -------------------------------------------------------------------------------- /0x0A-argc_argv/100-change.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | *main - prints the minimum number of coins to make change 6 | * for an amount of money 7 | *@argc: number of arguments 8 | *@argv: array of arguments 9 | *Return: returns 1 if there is an error; else returns 0 10 | */ 11 | 12 | int main(int argc, char *argv[]) 13 | { 14 | int cents, coins = 0; 15 | 16 | if (argc != 2) 17 | { 18 | printf("Error\n"); 19 | return (1); 20 | } 21 | cents = atoi(argv[1]); 22 | while (cents > 0) 23 | { 24 | coins++; 25 | if ((cents - 25) >= 0) 26 | { 27 | cents -= 25; 28 | continue; 29 | } 30 | if ((cents - 10) >= 0) 31 | { 32 | cents -= 10; 33 | continue; 34 | } 35 | if ((cents - 5) >= 0) 36 | { 37 | cents -= 5; 38 | continue; 39 | } 40 | if ((cents - 2) >= 0) 41 | { 42 | cents -= 2; 43 | continue; 44 | } 45 | cents--; 46 | } 47 | printf("%d\n", coins); 48 | return (0); 49 | } 50 | -------------------------------------------------------------------------------- /0x03-debugging/3-print_remaining_days.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * print_remaining_days - takes a date and prints how many days are 6 | * left in the year, taking leap years into account 7 | * @month: month in number format 8 | * @day: day of month 9 | * @year: year 10 | * Return: void 11 | */ 12 | 13 | void print_remaining_days(int month, int day, int year) 14 | { 15 | if (year % 4 == 0 || ((year % 100 == 0) && (year % 400 == 0))) 16 | { 17 | if (month > 2 && day >= 60) 18 | { 19 | day++; 20 | } 21 | 22 | printf("Day of the year: %d\n", day); 23 | printf("Remaining days: %d\n", 366 - day); 24 | } 25 | else 26 | { 27 | if (month == 2 && day == 60) 28 | { 29 | printf("Invalid date: %02d/%02d/%04d\n", month, day - 31, year); 30 | } 31 | else 32 | { 33 | printf("Day of the year: %d\n", day); 34 | printf("Remaining days: %d\n", 365 - day); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /0x15-file_io/0-read_textfile.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * read_textfile - read a file text and print to stdout 5 | * @filename: Name of the file 6 | * @letters: the number of letters it should read and print 7 | * Return: the actual number of letters it could read and print 8 | */ 9 | 10 | ssize_t read_textfile(const char *filename, size_t letters) 11 | { 12 | register int _open, _read, _write; 13 | register char *buffer = malloc(sizeof(char) * letters); 14 | 15 | if (!(buffer)) 16 | { 17 | free(buffer); 18 | return (0); 19 | } 20 | if (!(filename)) 21 | { 22 | return (0); 23 | } 24 | _open = open(filename, O_RDONLY); 25 | _read = read(_open, buffer, letters); 26 | _write = write(STDOUT_FILENO, buffer, _read); 27 | 28 | if (_open == -1 || _read == -1 || _write == -1 || !(_write == _read)) 29 | { 30 | free(buffer); 31 | return (0); 32 | } 33 | free(buffer); 34 | close(_open); 35 | return (_write); 36 | } 37 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/100-times_table.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * print_times_table - prints times table 4 | * @n : times table to use 5 | * Return:void 6 | */ 7 | 8 | void print_times_table(int n) 9 | { 10 | 11 | int a = 0, rep, b; 12 | 13 | if (n < 0 || n > 15) 14 | return; 15 | 16 | while (a <= n) 17 | { 18 | for (b = 0; b <= n; b++) 19 | { 20 | rep = a * b; 21 | if (b == 0) 22 | _putchar('0' + rep); 23 | else if (rep < 10) 24 | { 25 | _putchar(' '); 26 | _putchar(' '); 27 | _putchar('0' + rep); 28 | } 29 | else if (rep < 100) 30 | { 31 | _putchar(' '); 32 | _putchar('0' + rep / 10); 33 | _putchar('0' + rep % 10); 34 | } 35 | else 36 | { 37 | _putchar('0' + rep / 100); 38 | _putchar('0' + (rep - 100) / 10); 39 | _putchar('0' + rep % 10); 40 | } 41 | if (b < n) 42 | { 43 | _putchar(','); 44 | _putchar(' '); 45 | } 46 | } 47 | _putchar('\n'); 48 | a++; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/3-add_node_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * add_node_end - Add a new node at the end of a list. 4 | * @head: Address of the first node of a list. 5 | * @str: Address of the string to insert into the new node. 6 | * Return: Address of the new node. 7 | **/ 8 | list_t *add_node_end(list_t **head, const char *str) 9 | { 10 | list_t *temp, *temp2; 11 | unsigned int length = 0; 12 | 13 | if (str == NULL) 14 | return (NULL); 15 | 16 | temp = malloc(sizeof(list_t)); 17 | if (temp == NULL) 18 | return (NULL); 19 | 20 | temp->str = strdup(str); 21 | if (temp->str == NULL) 22 | { 23 | free(temp); 24 | return (NULL); 25 | } 26 | while (str[length]) 27 | length++; 28 | temp->len = length; 29 | temp->next = NULL; 30 | 31 | if (*head == NULL) 32 | { 33 | *head = temp; 34 | return (temp); 35 | } 36 | 37 | temp2 = *head; 38 | while (temp2->next) 39 | temp2 = temp2->next; 40 | temp2->next = temp; 41 | return (temp); 42 | } 43 | -------------------------------------------------------------------------------- /0x0B-malloc_free/3-alloc_grid.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * alloc_grid - returns a pointer to a 6 | *2 dimensional array of integers. 7 | *@width: width oàf array 8 | *@height: height of array 9 | * 10 | *Return: pointer to the array 11 | */ 12 | 13 | int **alloc_grid(int width, int height) 14 | { 15 | int **array; 16 | int i = 0, j; 17 | 18 | if (!(width > 0) || !(height > 0)) 19 | return (NULL); 20 | array = (int **) malloc(sizeof(int *) * height); 21 | if (array != NULL) 22 | { 23 | for (; i < height; i++) 24 | { 25 | array[i] = (int *) malloc(sizeof(int) * width); 26 | if (array[i] != NULL) 27 | { 28 | for (j = 0; j < width; j++) 29 | array[i][j] = 0; 30 | } 31 | else 32 | { 33 | while (i >= 0) 34 | { 35 | free(array[i]); 36 | i--; 37 | } 38 | free(array); 39 | return (NULL); 40 | } 41 | } 42 | return (array); 43 | } 44 | else 45 | { 46 | return (NULL); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /0x03-debugging/3-convert_day.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * convert_day - converts day of month to day of year, without accounting 5 | * for leap year 6 | * @month: month in number format 7 | * @day: day of month 8 | * Return: day of year 9 | */ 10 | 11 | 12 | int convert_day(int month, int day) 13 | { 14 | switch (month) 15 | { 16 | case 2: 17 | day = 31 + day; 18 | break; 19 | case 3: 20 | day = 59 + day; 21 | break; 22 | case 4: 23 | day = 90 + day; 24 | break; 25 | case 5: 26 | day = 120 + day; 27 | break; 28 | case 6: 29 | day = 151 + day; 30 | break; 31 | case 7: 32 | day = 181 + day; 33 | break; 34 | case 8: 35 | day = 212 + day; 36 | break; 37 | case 9: 38 | day = 243 + day; 39 | break; 40 | case 10: 41 | day = 273 + day; 42 | break; 43 | case 11: 44 | day = 304 + day; 45 | break; 46 | case 12: 47 | day = 334 + day; 48 | break; 49 | default: 50 | break; 51 | } 52 | return (day); 53 | } 54 | -------------------------------------------------------------------------------- /0x15-file_io/main.h: -------------------------------------------------------------------------------- 1 | #ifndef LISTS_H 2 | #define LISTS_H 3 | 4 | /** standard header files **/ 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | /** function prototypes here **/ 14 | ssize_t read_textfile(const char *filename, size_t letters); 15 | int create_file(const char *filename, char *text_content); 16 | int append_text_to_file(const char *filename, char *text_content); 17 | int _putchar(char); 18 | void elf_validation(unsigned char *e_ident); 19 | void print_magic(unsigned char *e_ident); 20 | void print_class(unsigned char *e_ident); 21 | void print_data(unsigned char *e_ident); 22 | void print_version(unsigned char *e_ident); 23 | void print_osabi(unsigned char *e_ident); 24 | void print_type(unsigned int e_type, unsigned char *e_ident); 25 | unsigned int lit_to_big_endian(unsigned int x); 26 | void print_entry(unsigned int e_type, unsigned char *e_ident); 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /0x0B-malloc_free/2-str_concat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * str_concat - Concatenates two strings. 6 | * @s1: The string to be concatenated upon. 7 | * @s2: The string to be concatenated to s1. 8 | * 9 | * Return: If concatenation fails - NULL. 10 | * Otherwise - a pointer the newly-allocated space in memory 11 | * containing the concatenated strings. 12 | */ 13 | char *str_concat(char *s1, char *s2) 14 | { 15 | char *concat_str; 16 | int index, concat_index = 0, len = 0; 17 | 18 | if (s1 == NULL) 19 | s1 = ""; 20 | 21 | if (s2 == NULL) 22 | s2 = ""; 23 | 24 | for (index = 0; s1[index] || s2[index]; index++) 25 | len++; 26 | 27 | concat_str = malloc(sizeof(char) * len); 28 | 29 | if (concat_str == NULL) 30 | return (NULL); 31 | 32 | for (index = 0; s1[index]; index++) 33 | concat_str[concat_index++] = s1[index]; 34 | 35 | for (index = 0; s2[index]; index++) 36 | concat_str[concat_index++] = s2[index]; 37 | 38 | return (concat_str); 39 | } 40 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef _LISTS_ 2 | #define _LISTS_ 3 | 4 | #include 5 | #include 6 | 7 | /** 8 | * struct dlistint_s - doubly linked list 9 | * @n: integer 10 | * @prev: points to the previous node 11 | * @next: points to the next node 12 | * 13 | * Description: doubly linked list node structure 14 | * 15 | */ 16 | typedef struct dlistint_s 17 | { 18 | int n; 19 | struct dlistint_s *prev; 20 | struct dlistint_s *next; 21 | } dlistint_t; 22 | size_t print_dlistint(const dlistint_t *h); 23 | size_t dlistint_len(const dlistint_t *h); 24 | dlistint_t *add_dnodeint(dlistint_t **head, const int n); 25 | dlistint_t *add_dnodeint_end(dlistint_t **head, const int n); 26 | void free_dlistint(dlistint_t *head); 27 | dlistint_t *get_dnodeint_at_index(dlistint_t *head, unsigned int index); 28 | int sum_dlistint(dlistint_t *head); 29 | dlistint_t *insert_dnodeint_at_index(dlistint_t **h, unsigned int idx, int n); 30 | int delete_dnodeint_at_index(dlistint_t **head, unsigned int index); 31 | #endif 32 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/9-insert_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /** 5 | * insert_nodeint_at_index - Insert a new node at a given position. 6 | * @head: First node address. 7 | * @idx: Position of the new node to be inserted in. 8 | * @n: Data of the new node. 9 | * Return: Address of the new node. 10 | **/ 11 | 12 | listint_t *insert_nodeint_at_index(listint_t **head, unsigned int idx, int n) 13 | { 14 | listint_t *new_node, *temp; 15 | unsigned int i = 0; 16 | 17 | if (*head == NULL && idx != 0) 18 | return (NULL); 19 | if (idx != 0) 20 | { 21 | temp = *head; 22 | for (; i < idx - 1 && temp != NULL; i++) 23 | temp = temp->next; 24 | if (temp == NULL) 25 | return (NULL); 26 | } 27 | new_node = malloc(sizeof(listint_t)); 28 | if (new_node == NULL) 29 | return (NULL); 30 | new_node->n = n; 31 | if (idx == 0) 32 | { 33 | new_node->next = *head; 34 | *head = new_node; 35 | return (new_node); 36 | } 37 | new_node->next = temp->next; 38 | temp->next = new_node; 39 | return (new_node); 40 | } 41 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/1-string_nconcat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * string_nconcat - Concatenates two strings using at 6 | * most an inputted number of bytes. 7 | * @s1: The first string. 8 | * @s2: The second string. 9 | * @n: The maximum number of bytes of s2 to concatenate to s1. 10 | * 11 | * Return: If the function fails - NULL. 12 | * Otherwise - a pointer to the concatenated space in memory. 13 | */ 14 | char *string_nconcat(char *s1, char *s2, unsigned int n) 15 | { 16 | char *concat; 17 | unsigned int len = n, index; 18 | 19 | if (s1 == NULL) 20 | s1 = ""; 21 | 22 | if (s2 == NULL) 23 | s2 = ""; 24 | 25 | for (index = 0; s1[index]; index++) 26 | len++; 27 | 28 | concat = malloc(sizeof(char) * (len + 1)); 29 | 30 | if (concat == NULL) 31 | return (NULL); 32 | 33 | len = 0; 34 | 35 | for (index = 0; s1[index]; index++) 36 | concat[len++] = s1[index]; 37 | 38 | for (index = 0; s2[index] && index < n; index++) 39 | concat[len++] = s2[index]; 40 | 41 | concat[len] = '\0'; 42 | 43 | return (concat); 44 | } 45 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-op_functions.c: -------------------------------------------------------------------------------- 1 | #include "3-calc.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * op_add - ... 7 | * @a: ... 8 | * @b: ... 9 | * 10 | * Return: ... 11 | */ 12 | int op_add(int a, int b) 13 | { 14 | return (a + b); 15 | } 16 | 17 | /** 18 | * op_sub - ... 19 | * @a: ... 20 | * @b: ... 21 | * 22 | * Return: ... 23 | */ 24 | int op_sub(int a, int b) 25 | { 26 | return (a - b); 27 | } 28 | 29 | /** 30 | * op_mul - ... 31 | * @a: ... 32 | * @b: ... 33 | * 34 | * Return: ... 35 | */ 36 | int op_mul(int a, int b) 37 | { 38 | return (a * b); 39 | } 40 | 41 | /** 42 | * op_div - ... 43 | * @a: ... 44 | * @b: ... 45 | * 46 | * Return: ... 47 | */ 48 | int op_div(int a, int b) 49 | { 50 | if (b == 0) 51 | { 52 | printf("Error\n"); 53 | exit(100); 54 | } 55 | 56 | return (a / b); 57 | } 58 | 59 | /** 60 | * op_mod - ... 61 | * @a: ... 62 | * @b: ... 63 | * 64 | * Return: ... 65 | */ 66 | int op_mod(int a, int b) 67 | { 68 | if (b == 0) 69 | { 70 | printf("Error\n"); 71 | exit(100); 72 | } 73 | 74 | return (a % b); 75 | } 76 | -------------------------------------------------------------------------------- /0x08-recursion/README.md: -------------------------------------------------------------------------------- 1 | ![img](https://assets.imaginablefutures.com/media/images/ALX_Logo.max-200x150.png) 2 | > Recursion 3 | 4 | ## Intro 5 | In this section, we will cover recursion 6 | 7 | 1. [Introduction to recursion](https://s3.amazonaws.com/alx-intranet.hbtn.io/uploads/misc/2021/1/2818ba6f14f644b871dcbd746925fa15b8cd5937.pdf?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIARDDGGGOUSBVO6H7D%2F20221009%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20221009T101429Z&X-Amz-Expires=86400&X-Amz-SignedHeaders=host&X-Amz-Signature=9f6aaaf2eeb3d0f4ba78b56aa9ffd638ee67dd66d70b156b5941cc6486b70f10) 8 | 2. [What on earth is recursion ](https://www.youtube.com/watch?v=Mv9NEXX1VHc) 9 | 3. [Recursion part 1](https://www.tutorialspoint.com/cprogramming/c_recursion.htm) 10 | 4. [Recursion part 2](https://www.youtube.com/watch?v=XGxbXMP6k8k) 11 | 5. [Recursion part 3](https://www.youtube.com/watch?v=7XiIS6HobNs) 12 | 13 | ## Learning objectives 14 | * [X] What is recursion 15 | * [X] How to implement recursion 16 | * [X] In what situations you should implement recursion 17 | * [X] In what situations you shouldn’t implement recursion -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/8-delete_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * delete_dnodeint_at_index - Delete node at nth index 5 | * 6 | * @head: Head of node 7 | * 8 | * @index: index 9 | * 10 | * Return: 1 succeed, -1 if fail 11 | */ 12 | 13 | int delete_dnodeint_at_index(dlistint_t **head, unsigned int index) 14 | { 15 | dlistint_t *node; 16 | unsigned int count; 17 | 18 | if (*head == NULL) 19 | return (-1); 20 | 21 | node = *head; 22 | if (index == 0) 23 | { 24 | *head = node->next; 25 | if (node->next != NULL) 26 | { 27 | node->next->prev = NULL; 28 | } 29 | free(node); 30 | return (1); 31 | } 32 | for (count = 0; node != NULL && count < index - 1 ; count++) 33 | { 34 | node = node->next; 35 | } 36 | if (node == NULL || node->next == NULL) 37 | { 38 | return (-1); 39 | } 40 | 41 | if (node->next->next != NULL) 42 | { 43 | node->next = node->next->next; 44 | free(node->next->prev); 45 | node->next->prev = node; 46 | return (1); 47 | } 48 | else 49 | { 50 | free(node->next); 51 | node->next = NULL; 52 | return (1); 53 | } 54 | return (-1); 55 | } 56 | -------------------------------------------------------------------------------- /0x0A-argc_argv/README.md: -------------------------------------------------------------------------------- 1 | ![img](https://assets.imaginablefutures.com/media/images/ALX_Logo.max-200x150.png) 2 | > argv argc 3 | 4 | ## Intro 5 | In this session, we'll cover ```argv``` and ```argc``` as used in C programming. 6 | 7 | ## Resources 8 | 9 | 1. [Arguments to main](https://publications.gbdirect.co.uk//c_book/chapter10/arguments_to_main.html) 10 | 2. [argv and argc](http://crasseux.com/books/ctutorial/argc-and-argv.html) 11 | 3. [What does argv and argc mean in C](https://www.youtube.com/watch?v=aP1ijjeZc24) 12 | 4. [How to compile with unsed variables](https://www.google.com/webhp?q=unused+variable+C) 13 | 14 | 15 | ## Learning objectives 16 | After going through the above resources, you should be able to [explain to anyone](https://fs.blog/feynman-learning-technique/) without the help of Goolge the following concepts. 17 | 18 | 19 | * [X] How to use arguments passed to your program 20 | * [X] What are two prototypes of main that you know of, and in which case do you use one or the other 21 | * [X] How to use __attribute__((unused)) or (void) to compile functions with unused variables or parameters 22 | -------------------------------------------------------------------------------- /0x0B-malloc_free/quiz.md: -------------------------------------------------------------------------------- 1 | ![img](https://assets.imaginablefutures.com/media/images/ALX_Logo.max-200x150.png) 2 | > Dynamic memory allocation quizes 3 | 4 | #### Question #0 5 | We declare the following variable 6 | 7 | ```c 8 | int arr[5]; 9 | ``` 10 | What is the size in memory of the variable ```arr```? 11 | * [ ] 8 bytes 12 | * [x] 20 bytes 13 | * [ ] 32 bytes 14 | * [ ] 5 bytes 15 | * [ ] 4 bytes 16 | * [ ] 10 bytes 17 | 18 | #### Question #1 19 | The process of getting the value that is stored in the memory location pointed to by a pointer is called: 20 | 21 | * [X] Dereferencing 22 | * [ ] Pointing 23 | * [ ] Accessing 24 | * [ ] Casting 25 | 26 | #### Question #2 27 | What is the value of ```n``` after the following code is executed? 28 | 29 | ```c 30 | int n = 98; 31 | int *p = &n; 32 | ``` 33 | * [ ] 99 34 | * [ ] 0 35 | * [X] 98 36 | * [ ] 402 37 | 38 | #### Question #3 39 | What happens when one tries to access an illegal memory location? 40 | 41 | * [ ] The computer shuts down 42 | * [ ] The operation is ignored 43 | * [X] Segmentation fault 44 | * [ ] There’s a chance for the computer to catch fire, and sometimes even explode 45 | 46 | 47 | #### Question #4 -------------------------------------------------------------------------------- /0x18-dynamic_libraries/README.md: -------------------------------------------------------------------------------- 1 | ![img](https://assets.imaginablefutures.com/media/images/ALX_Logo.max-200x150.png) 2 | > Dynamic linking 3 | 4 | ## Intro 5 | In this session we will have a look at dynamic linking in C 6 | 7 | ## Resources 8 | 1. [The Compilation pipeline](https://www.linkedin.com/pulse/four-stages-compilastion-c-program-agustin-espinoza-saavedra/) 9 | 2. [Dynamic Vs Static linking](https://www.youtube.com/watch?v=eW5he5uFBNM) 10 | 3. [Creating dynamic libraries](https://www.google.com/search?q=linux+create+dynamic+library) 11 | 4. [Youtube](https://www.youtube.com/results?search_query=dynamic+linking) 12 | 13 | ## Man / Help 14 | - nm 15 | - ldd 16 | - ldconfig 17 | 18 | ## Learning objectives 19 | At the end of this project, you should be able to [explain to anyone](https://fs.blog/feynman-learning-technique/) without the help of Google the following concepts 20 | 21 | * [X] What is a dynamic library, how does it work, how to create one, and how to use it 22 | * [X] What is the environment variable ```$LD_LIBRARY_PATH``` and how to use it 23 | * [X] What are the differences between static and shared libraries 24 | * [X] Basic usage ```nm, ldd, ldconfig``` -------------------------------------------------------------------------------- /0x10-variadic_functions/3-print_all.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "variadic_functions.h" 4 | 5 | /** 6 | * print_all - print char, integer, float and string 7 | * @format: format 8 | */ 9 | void print_all(const char * const format, ...) 10 | { 11 | va_list list; 12 | unsigned int j = 0, start = 0; 13 | char *p; 14 | 15 | va_start(list, format); 16 | while (format && format[j] != '\0') 17 | { 18 | switch (format[j]) 19 | { case 'c': 20 | switch (start) 21 | { case 1: printf(", "); } 22 | start = 1; 23 | printf("%c", va_arg(list, int)); 24 | break; 25 | case 'i': 26 | switch (start) 27 | { case 1: printf(", "); } 28 | start = 1; 29 | printf("%i", va_arg(list, int)); 30 | break; 31 | case 'f': 32 | switch (start) 33 | { case 1: printf(", "); } 34 | start = 1; 35 | printf("%f", va_arg(list, double)); 36 | break; 37 | case's': 38 | switch (start) 39 | { case 1: printf(", "); } 40 | start = 1; 41 | p = va_arg(list, char*); 42 | if (p) 43 | { printf("%s", p); 44 | break; } 45 | printf("%p", p); 46 | break; } 47 | j++; 48 | } 49 | printf("\n"); 50 | va_end(list); 51 | } 52 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/README.md: -------------------------------------------------------------------------------- 1 | ![img](https://assets.imaginablefutures.com/media/images/ALX_Logo.max-200x150.png) 2 | >Functions and nested loops 3 | 4 | ## Intro 5 | In this section, we will explore functions and nested loops in detail 6 | 7 | ## Resources 8 | 1. [Nested While loop](https://www.youtube.com/watch?v=Z3iGeQ1gIss) 9 | 2. [Functions](https://www.tutorialspoint.com/cprogramming/c_functions.htm) 10 | 3. [Youtube](https://www.youtube.com/watch?v=qMlnFwYdqIw) 11 | 4. [Function prototypes](https://www.geeksforgeeks.org/what-is-the-purpose-of-a-function-prototype/) 12 | 5. [Header files](https://www.tutorialspoint.com/cprogramming/c_header_files.htm) 13 | 14 | ## Learning objectives 15 | 16 | By the end of this session, you should be able to [explain to anyone](https://fs.blog/feynman-learning-technique/) without the help of Google the following concepts 17 | 18 | * [X] What are nested loops and how to use them 19 | * [X] What is the difference between a declaration and a definition of a function 20 | * [X] What is a prototype 21 | * [X] Scope of variables 22 | * [X] What are the ```gcc flags -Wall -Werror -pedantic -Wextra -std=gnu89``` 23 | * [X] What are header files and how to to use them with #include 24 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/README.md: -------------------------------------------------------------------------------- 1 | ![img](https://assets.imaginablefutures.com/media/images/ALX_Logo.max-200x150.png) 2 | >More functions and nested loops 3 | 4 | ## Intro 5 | In this section, we will explore functions and nested loops in detail 6 | 7 | ## Resources 8 | 1. [Nested While loop](https://www.youtube.com/watch?v=Z3iGeQ1gIss) 9 | 2. [Functions](https://www.tutorialspoint.com/cprogramming/c_functions.htm) 10 | 3. [Youtube](https://www.youtube.com/watch?v=qMlnFwYdqIw) 11 | 4. [Function prototypes](https://www.geeksforgeeks.org/what-is-the-purpose-of-a-function-prototype/) 12 | 5. [Header files](https://www.tutorialspoint.com/cprogramming/c_header_files.htm) 13 | 14 | ## Learning objectives 15 | 16 | By the end of this session, you should be able to [explain to anyone](https://fs.blog/feynman-learning-technique/) without the help of Google the following concepts 17 | 18 | * [X] What are nested loops and how to use them 19 | * [X] What is the difference between a declaration and a definition of a function 20 | * [X] What is a prototype 21 | * [X] Scope of variables 22 | * [X] What are the ```gcc flags -Wall -Werror -pedantic -Wextra -std=gnu89``` 23 | * [X] What are header files and how to to use them with #include -------------------------------------------------------------------------------- /0x0B-malloc_free/100-argstostr.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | /** 4 | * argstostr - a function that concatenates all the arguments of your program 5 | *@ac: count of args passed to the function 6 | *@av:array of arguments 7 | * 8 | *Return: pointer to the new string 9 | */ 10 | 11 | char *argstostr(int ac, char **av) 12 | { 13 | char *new_string = NULL; 14 | int k = 0, i = ac, j, sum = 0, temp = 0; 15 | 16 | if (ac == 0 || av == NULL) 17 | return (NULL); 18 | 19 | while (ac--) 20 | sum += (len(av[ac]) + 1); 21 | new_string = (char *) malloc(sum + 1); 22 | 23 | if (new_string != NULL) 24 | { 25 | while (k < i) 26 | { 27 | for (j = 0; av[k][j] != '\0'; j++) 28 | new_string[j + temp] = av[k][j]; 29 | new_string[temp + j] = '\n'; 30 | temp += (j + 1); 31 | k++; 32 | } 33 | new_string[temp] = '\0'; 34 | } 35 | else 36 | { 37 | return (NULL); 38 | } 39 | return (new_string); 40 | } 41 | 42 | /** 43 | *len - returns length of str 44 | *@str: string counted 45 | *Return: returns the length 46 | */ 47 | int len(char *str) 48 | { 49 | int len = 0; 50 | 51 | if (str != NULL) 52 | { 53 | while (str[len]) 54 | len++; 55 | } 56 | return (len); 57 | } 58 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/README.md: -------------------------------------------------------------------------------- 1 | ![img](https://assets.imaginablefutures.com/media/images/ALX_Logo.max-200x150.png) 2 | > Searching algorithms 3 | 4 | ## About 5 | Searching algorithms are a set of algorithms used to find a particular item in a collection of items. There are several types of searching algorithms available, each with its own advantages and disadvantages. In this project, we will explore the time and space complexity of the various searching algorithms implementations out there and discus the merrits and drawbacks of each. 6 | 7 | 8 | ## Resource 9 | __Read or watch__: 10 | 1. [Searching algorithms](https://en.wikipedia.org/wiki/Search_algorithm) 11 | 2. [Space complexities](https://www.geeksforgeeks.org/g-fact-86/) 12 | 3. [Youtube](https://www.youtube.com/results?search_query=searching+algorithms) 13 | 4. [Google](https://www.google.com/search?q=searching+algorithms) 14 | 15 | ## Learning objectives 16 | By the end of this project, you are expected to be able to [explain to anyone](https://fs.blog/feynman-learning-technique/) __Without the help of Google__: 17 | 18 | * [X] What is a search algorithm 19 | * [X] What is a linear search 20 | * [X] What is a binary search 21 | * [X] What is the best search algorithm to use depending on your needs 22 | 23 | -------------------------------------------------------------------------------- /0x10-variadic_functions/README.md: -------------------------------------------------------------------------------- 1 | ![img](https://assets.imaginablefutures.com/media/images/ALX_Logo.max-200x150.png) 2 | > Variadic functions 3 | 4 | ## Intro 5 | In this session, we will have a look at variadic functions in C. 6 | 7 | ## About 8 | Variadic functions are function that take a variable number of arguments. Say they take argument whose number is not exacly know at the time of compilation. 9 | 10 | 11 | ## Resources 12 | 13 | 1. [stdarg.h](https://en.wikipedia.org/wiki/Stdarg.h) 14 | 2. [Variadic functions](https://www.gnu.org/software/libc/manual/html_node/Variadic-Functions.html) 15 | 3. [The const keyword](https://www.youtube.com/watch?v=1W4oyuOdXv8) 16 | 4. [Variadic funtions in action](https://www.geeksforgeeks.org/variadic-functions-in-c/) 17 | 5. [What Google says about variadic functions](https://www.google.com/search?q=variadic+functions+in+c) 18 | 19 | ## Learning objectives 20 | After going through the above resources, you should be able to [explain to anyone](https://fs.blog/feynman-learning-technique/) without the help of Goolge the following concepts. 21 | 22 | 23 | * [X] What are variadic functions 24 | * [X] How to use va_start, va_argand va_end macros 25 | * [X] Why and how to use the consttype qualifier -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef LISTS_H 2 | #define LISTS_H 3 | #include 4 | #include 5 | 6 | /** structures here **/ 7 | /** 8 | * struct listint_s - singly linked list 9 | * @n: integer 10 | * @next: points to the next node 11 | * Description: singly linked list node structure 12 | */ 13 | typedef struct listint_s 14 | { 15 | int n; 16 | struct listint_s *next; 17 | } listint_t; 18 | 19 | /** function prototypes here **/ 20 | 21 | int _putchar(char); 22 | size_t print_listint(const listint_t *h); 23 | size_t listint_len(const listint_t *h); 24 | listint_t *add_nodeint(listint_t **head, const int n); 25 | listint_t *add_nodeint_end(listint_t **head, const int n); 26 | void free_listint(listint_t *head); 27 | void free_listint2(listint_t **head); 28 | int pop_listint(listint_t **head); 29 | listint_t *get_nodeint_at_index(listint_t *head, unsigned int index); 30 | int sum_listint(listint_t *head); 31 | listint_t *insert_nodeint_at_index(listint_t **head, unsigned int idx, int n); 32 | int delete_nodeint_at_index(listint_t **head, unsigned int index); 33 | listint_t *reverse_listint(listint_t **head); 34 | size_t print_listint_safe(const listint_t *head); 35 | size_t free_listint_safe(listint_t **h); 36 | listint_t *find_listint_loop(listint_t *head); 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /0x15-file_io/3-cp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * main - copies the content of a file to another file 4 | * @argc: argument count 5 | * @argv: argument vector 6 | * Return: 0 if success 7 | */ 8 | 9 | int main(int argc, char *argv[]) 10 | { 11 | int _file1, _file2, _read, c1, c2; 12 | char buffer[1024]; 13 | 14 | if (argc != 3) 15 | { 16 | dprintf(STDERR_FILENO, "Usage: cp file_from file_to\n"), exit(97); 17 | } 18 | _file1 = open(argv[1], O_RDONLY); 19 | if (_file1 < 0) 20 | { 21 | dprintf(STDERR_FILENO, "Error: Can't read from file %s\n", argv[1]); 22 | exit(98); 23 | } 24 | _file2 = open(argv[2], O_TRUNC | O_CREAT | O_WRONLY, 0664); 25 | while ((_read = read(_file1, buffer, 1024)) > 0) 26 | { 27 | if (_file2 < 0 || (write(_file2, buffer, _read) != _read)) 28 | { 29 | dprintf(STDERR_FILENO, "Error: Can't write to %s\n", argv[2]), exit(99); 30 | } 31 | } 32 | if (_read < 0) 33 | { 34 | dprintf(STDERR_FILENO, "Error: Can't read from file %s\n", argv[1]); 35 | exit(98); 36 | } 37 | c1 = close(_file1); 38 | if (c1 < 0) 39 | { 40 | dprintf(STDERR_FILENO, "Error: Can't close fd %i\n", _file1), exit(100); 41 | } 42 | c2 = close(_file2); 43 | if (c2 < 0) 44 | { 45 | dprintf(STDERR_FILENO, "Error: Can't close fd %i\n", _file2), exit(100); 46 | } 47 | return (0); 48 | } 49 | -------------------------------------------------------------------------------- /0x09-static_libraries/README.md: -------------------------------------------------------------------------------- 1 | ![img](https://assets.imaginablefutures.com/media/images/ALX_Logo.max-200x150.png) 2 | > Static Libraries 3 | 4 | ## Intro 5 | In this session, we will expore static libraries in C 6 | 7 | ## Resources 8 | 1. [What is a C library and what is it good for?](https://docencia.ac.upc.edu/FIB/USO/Bibliografia/unix-c-libraries.html) 9 | 2. [Building and using a static lib](https://docencia.ac.upc.edu/FIB/USO/Bibliografia/unix-c-libraries.html) 10 | 3. [All in one about libraries](https://medium.com/@julianfrancor9/understanding-c-libraries-e5c321fd4aac) 11 | 4. [Understanding C libraries ](https://medium.com/@lifebysech/everything-you-need-to-know-to-start-using-c-libraries-4644ca10d04c) 12 | 5. [How are Dynamic libraries different from Static libraries in C](https://www.linkedin.com/pulse/differences-between-static-dynamic-libraries-juan-david-tuta-botero/) 13 | 6. [Static linking V/S dynamic linking](https://www.youtube.com/watch?v=eW5he5uFBNM) 14 | 15 | 16 | ## Learning objectives 17 | 18 | * [X] The ar command and how to use it. 19 | * [X] The ranlib command and how to use it. 20 | * [X] The nm command and how to use it. 21 | * [X] What is a static library, how does it work, how to create one, and how to use it 22 | * [X] Basic usage of ar, ranlib, nm -------------------------------------------------------------------------------- /0x15-file_io/README.md: -------------------------------------------------------------------------------- 1 | ![img](https://assets.imaginablefutures.com/media/images/ALX_Logo.max-200x150.png) 2 | > Handling file I/O 3 | 4 | ## About 5 | In this session, we will have a look at handling file input and output in C. 6 | 7 | ## Resources 8 | 1. [File Desriptor](https://en.wikipedia.org/wiki/File_descriptor) 9 | 10 | 2. [Handling files in C](https://byjus.com/gate/file-handling-in-c/) 11 | 12 | ## Man / Help 13 | * ```close``` 14 | * ```open``` 15 | * ```read``` 16 | * ```write``` 17 | * ```dprintf``` 18 | 19 | ## Learning objectives 20 | By the end of this session, you should be able to [explain to anyone](https://fs.blog/feynman-learning-technique/) without the help of google the following concepts. 21 | 22 | * [X] Look for the right source of information online 23 | * [X] How to create, open, close, read and write files 24 | * [X] What are file descriptors 25 | * [X] What are the 3 standard file descriptors, what are their purpose and what are their POSIX names 26 | * [X] How to use the I/O system calls ```open, close, read``` and ```write``` 27 | * [X] What are and how to use the flags ```O_RDONLY```, ```O_WRONLY```, ```O_RDWR``` 28 | 29 | * [X] What are file permissions, and how to set them when creating a file with the ```open``` system call 30 | * [X] What is a system call 31 | * [X] What is the difference between a function and a system call 32 | 33 | ## Quizes 34 | [Quizes](./quiz.md) -------------------------------------------------------------------------------- /0x0F-function_pointers/README.md: -------------------------------------------------------------------------------- 1 | ![img](https://assets.imaginablefutures.com/media/images/ALX_Logo.max-200x150.png) 2 | > Function Pointers 3 | 4 | ## Intro 5 | In this session, we will look at function pointers. 6 | 7 | ## About 8 | Remember in one of our [Sessions](https://github.com/Itsfoss0/alx-low_level_programming/tree/main/0x05-pointers_arrays_strings) we introduced pointers. 9 | 10 | We mentioned that pointer are just variables that store the address of other variables of the same type as that of the Pointer in memory. 11 | 12 | Lets Dive into it. 13 | 14 | ## Resources 15 | 16 | 1. [Here is everything you need to know about pointers](https://boredzo.org/pointers/) 17 | 2. [Funtion pointers in C](https://www.geeksforgeeks.org/function-pointer-in-c/) 18 | 3. [Pointer to function ](https://publications.gbdirect.co.uk//c_book/chapter5/function_pointers.html) 19 | 4. [Function pointers in Action](https://www.youtube.com/watch?v=ynYtgGUNelE) 20 | 5. [Why function pointers](https://www.youtube.com/watch?v=sxTFSDAZM8s) 21 | 22 | ## Learning objectives 23 | At the end of this session, you should be able to [explain to anyone](https://fs.blog/feynman-learning-technique/) the following concepts without the help of Google 24 | 25 | * [X] What are function pointers and how to use them 26 | * [X] What does a function pointer exactly hold 27 | * [X] Where does a function pointer point to in the virtual memory 28 | 29 | ## Quizes 30 | [Quiz](./quiz.md) -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![img](https://assets.imaginablefutures.com/media/images/ALX_Logo.max-200x150.png) 2 | > Introduction to programming in C 3 |
4 | 5 | ## ALX low level programming tasks in C 6 | 7 | Table of contents 8 | * [Introduction to programming in C - Hello World!](./0x00-hello_world/) 9 | * [Variables and Branching Control Structures](./0x01-variables_if_else_while/) 10 | * [Looping Control Structures and functions](./0x02-functions_nested_loops/) 11 | * [Debugging](./0x03-debugging/) 12 | * [Nested loops ](./0x04-more_functions_nested_loops/) 13 | * [Arrays Pointer and Strings](./0x05-pointers_arrays_strings/) 14 | * [More Pointers Arrays and Strings](./0x06-pointers_arrays_strings/) 15 | * [Function pointers](./0x0F-function_pointers/) 16 | * [Even more Pointers Arrays and strings](./0x07-pointers_arrays_strings/) 17 | * [Recursion](./0x08-recursion/) 18 | * [Dynamic memory allocation](./0x0B-malloc_free/) 19 | * [Static Libraries](./0x09-static_libraries/) 20 | * [Pre processor](./0x0D-preprocessor/) 21 | * [Structures and typedef](./0x0E-structures_typedef/) 22 | * [Argv and Argc](./0x0A-argc_argv/) 23 | * [Variadic functions](./0x10-variadic_functions/) 24 | * [Singly Linked lists](./0x12-singly_linked_lists/) 25 | * [More singly linked lists](./0x13-more_singly_linked_lists/) 26 | * [Bit manipulation](./0x14-bit_manipulation/) 27 | * [Doubly Linked lists](/0x17-doubly_linked_lists) 28 | * [Dynamic Libraries](./0x18-dynamic_libraries) 29 | * [Hash tables](/0x1A-hash_tables) 30 | * [Makefiles](/0x1C-makefiles) 31 | * [Searching algorithms](0x1E-search_algorithms) -------------------------------------------------------------------------------- /0x1A-hash_tables/hash_tables.h: -------------------------------------------------------------------------------- 1 | #ifndef _HASH_TABLES_H_ 2 | #define _HASH_TABLES_H_ 3 | 4 | /* Header files*/ 5 | #include 6 | #include 7 | #include 8 | 9 | /*Custom data structure*/ 10 | 11 | /** 12 | * struct hash_node_s - Node of a hash table 13 | * 14 | * @key: The key, string 15 | * The key is unique in the HashTable 16 | * @value: The value corresponding to a key 17 | * @next: A pointer to the next node of the List 18 | */ 19 | typedef struct hash_node_s 20 | { 21 | char *key; 22 | char *value; 23 | struct hash_node_s *next; 24 | } hash_node_t; 25 | 26 | /** 27 | * struct hash_table_s - Hash table data structure 28 | * 29 | * @size: The size of the array 30 | * @array: An array of size @size 31 | * Each cell of this array is a pointer to the first node of a linked list, 32 | * because we want our HashTable to use a Chaining collision handling 33 | */ 34 | typedef struct hash_table_s 35 | { 36 | unsigned long int size; 37 | hash_node_t **array; 38 | } hash_table_t; 39 | 40 | /*function prototypes*/ 41 | hash_table_t *hash_table_create(unsigned long int size); 42 | unsigned long int hash_djb2(const unsigned char *str); 43 | unsigned long int key_index(const unsigned char *key, unsigned long int size); 44 | int hash_table_set(hash_table_t *ht, const char *key, const char *value); 45 | hash_node_t *create_new_node (const char *key, const char *value); 46 | char *hash_table_get(const hash_table_t *ht, const char *key) 47 | void hash_table_print(const hash_table_t *ht); 48 | void hash_table_delete(hash_table_t *ht); 49 | 50 | #endif 51 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/quiz.md: -------------------------------------------------------------------------------- 1 | ![img](https://assets.imaginablefutures.com/media/images/ALX_Logo.max-200x150.png) 2 | > Structures and Typedef quizes 3 | 4 | #### Question #0 5 | The general syntax for a struct declaration in C is: 6 | ```c 7 | struct tag_name { 8 | type member1; 9 | type member2; 10 | /* declare as many members as desired, but the entire structure size must be known to the compiler. */ 11 | }; 12 | ``` 13 | * [ ] Maybe 14 | * [X] True 15 | * [ ] False 16 | 17 | #### Question #1 18 | Those two codes do the same thing: 19 | ```c 20 | typedef struct point point; 21 | struct point { 22 | int x; 23 | int y; 24 | }; 25 | point p = {1, 2}; 26 | ``` 27 | ```c 28 | typedef struct point point; 29 | struct point { 30 | int x; 31 | int y; 32 | }; 33 | point p = { .y = 2, .x = 1 }; 34 | ``` 35 | * [ ] False: the members of the structures will not have the same values 36 | * [X] True 37 | * [ ] False: the second does not compile 38 | 39 | #### Question #2 40 | You should write documentation for all the structures you create 41 | 42 | * [X] As soon as I write my structure. 43 | * [X] True 44 | * [ ] I’ll do it 5 minutes before the deadline when I try Betty on my header file 45 | 46 | 47 | #### Question #3 48 | Given this code 49 | 50 | ```c 51 | struct point { 52 | int x; 53 | int y; 54 | }; 55 | struct point my_point = { 3, 7 }; 56 | struct point *p = &my_point; 57 | ``` 58 | To set the member y of my variable my_point to 98, I can do (select all valid answers): 59 | * [ ] p.y = 98; 60 | * [X] my_point.y = 98; 61 | * [ ] my_point->y = 98; 62 | * [X] p->y = 98; 63 | * [X] (*p).y = 98; 64 | -------------------------------------------------------------------------------- /0x1C-makefiles/school.c: -------------------------------------------------------------------------------- 1 | #include "m.h" 2 | 3 | /** 4 | * print_school 5 | * description: print Holberton school logo 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 | -------------------------------------------------------------------------------- /0x0F-function_pointers/quiz.md: -------------------------------------------------------------------------------- 1 | ![img](https://assets.imaginablefutures.com/media/images/ALX_Logo.max-200x150.png) 2 | > Function pointer quizes 3 | 4 | 5 | #### Question #0 6 | What does a pointer to a function point to (check all correct answers if there is more than one)? 7 | 8 | * [ ] The first character of the name of the function 9 | * [ ] data 10 | * [X] code 11 | * [X] The first byte of code of the function 12 | 13 | 14 | #### Question #1 15 | To store the address of this function: 16 | 17 | ```c 18 | void neyo(void); 19 | ``` 20 | 21 | to the variable f of type pointer to a function that does not take any argument and does not return anything, you would do (check all correct answers if there is more than one): 22 | 23 | * [ ] *f = neyo; 24 | * [X] f = neyo; 25 | * [X] f = &neyo; 26 | * [ ]*f = &neyo; 27 | 28 | #### Question #3 29 | Which one is a pointer to a function? 30 | 31 | * [X] int (*func)(int a, float b); 32 | * [ ] int func(int a, float b); 33 | * [ ] int *func(int a, float b); 34 | * [ ] (int *)func(int a, float b); 35 | 36 | #### Question #4 37 | This ```void (*anjula[])(int, float)``` is: 38 | 39 | * [ ] A pointer to a function that takes an int and a float as parameters and returns an empty array 40 | * [ ] A pointer to a function that takes an int and a float as parameters and returns nothing 41 | * [ ] A pointer to a function that takes an array of int and float as a parameter and returns nothing 42 | * [ ] A pointer to an array of functions that take an int and a float as parameters and returns nothing 43 | * [X] An array of pointers to functions that take an int and a float as parameters and returns nothing 44 | 45 | -------------------------------------------------------------------------------- /0x0D-preprocessor/README.md: -------------------------------------------------------------------------------- 1 | ![img](https://assets.imaginablefutures.com/media/images/ALX_Logo.max-200x150.png) 2 | > Preprocessor and macros 3 | 4 | ## Intro 5 | 6 | In this session we will take a look at Macros are and take a peek at what happens under the hood during the preprocessing phase of compilation of a C program. 7 | 8 | ## Resources 9 | 10 | 1. [Understanding the C compilation process](https://www.youtube.com/watch?v=eW5he5uFBNM) 11 | 2. [Object like macros](https://gcc.gnu.org/onlinedocs/gcc-5.1.0/cpp/Object-like-Macros.html#Object-like-Macros) 12 | 5. [Function like macros ](https://www.ibm.com/docs/en/i/7.2?topic=directive-function-like-macros) 13 | 4. [Macro Arguments](https://gcc.gnu.org/onlinedocs/gcc-5.1.0/cpp/Macro-Arguments.html#Macro-Arguments) 14 | 5. [Preprocessor Directive](https://www.youtube.com/watch?v=X6HiYbY3Uak) 15 | 6. [The C preprocessor](https://www.cprogramming.com/tutorial/cpreprocessor.html) 16 | 7. [The standard Predefined macros](https://gcc.gnu.org/onlinedocs/gcc-5.1.0/cpp/Standard-Predefined-Macros.html#Standard-Predefined-Macros) 17 | 8. [Include guard](https://en.wikipedia.org/wiki/Include_guard) 18 | 9. [Common predefined Macros](https://gcc.gnu.org/onlinedocs/gcc-5.1.0/cpp/Common-Predefined-Macros.html#Common-Predefined-Macros) 19 | 20 | 21 | ## Learning objectives 22 | At the end of this session, you should be able to [explain to anyone](https://fs.blog/feynman-learning-technique/) the following concepts without the help of Google 23 | 24 | * [X] What are macros and how to use them 25 | * [X] What are the most common predefined macros 26 | * [X] How to include guard your header files 27 | 28 | ## Quizes 29 | [Quiz](./quiz.md) -------------------------------------------------------------------------------- /0x12-singly_linked_lists/quiz.md: -------------------------------------------------------------------------------- 1 | ![img](https://assets.imaginablefutures.com/media/images/ALX_Logo.max-200x150.png) 2 | > Singly linked lists quizes 3 | 4 | #### Question #0 5 | Arrays Vs Linked Lists: select all true statements 6 | * [X] Memory is aligned for an Array - each elements are back to back in the memory 7 | * [ ] We can easily remove an element from an Array 8 | * [X] Linked list can contain as value a structure 9 | * [ ] We can add elements indefinitely to an array 10 | * [ ] Memory is aligned for a Linked list - each elements are back to back in the memory 11 | * [X] We can add elements indefinitely to a linked list 12 | * [X] We can easily removed an element from a Linked list 13 | * [X] Array can contain as value a structure 14 | 15 | #### Question #1 16 | What’s the “head” of a linked list? 17 | * [X] It’s the first node 18 | * [ ] It’s the last node 19 | * [ ] It’s the node with the highest value 20 | * [ ] It’s the node with the pointer to the next equals to NULL 21 | * [ ] It’s the node with the lowest value 22 | 23 | #### Question #2 24 | What’s the “tail” of a linked list? 25 | 26 | * [ ] It’s the node with the highest value 27 | * [X] It’s the node with the pointer to the next equals to NULL 28 | * [ ] It’s the first node 29 | * [ ] It’s the node with the lowest value 30 | 31 | #### Question #3 32 | What’s a node? (select all possible answers) 33 | 34 | * [ ] It’s a cell in an array 35 | * [ ] It’s a server 36 | * [X] It’s a structure with a pointer to the next node and value information 37 | * [X] It’s a space allocated in memory 38 | * [ ] It’s an integer 39 | 40 | #### Question #4 41 | In a singly linked list, what are possible directions to traverse it? (select all possible answers) 42 | 43 | * [X] Forward 44 | * [ ] Backward 45 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/README.md: -------------------------------------------------------------------------------- 1 | ![img](https://assets.imaginablefutures.com/media/images/ALX_Logo.max-200x150.png) 2 | > Singly linked lists 3 | 4 | ## Intro 5 | In this session, we will look at Data structures and algorithims - Linked lists in particular. 6 | 7 | ## About 8 | Data structures by definition is a way of storing, structuring and organizing data so it can be accessed, traversed, updated, used and searched efficiently depending on the task at hand. 9 | 10 | Data structures can be divided into two - 11 | * Linear Data strucutes 12 | * Non linear Data structures 13 | 14 | Linear Data structures are those in which the data elements are stored in a sequential form, where each element is linked to the adjacent one. Example include __arrays__, __linked List__, __stacks__ and __ques__. Linear Data structures can further be divided into static Data structures and Dymanic Data structures. 15 | 16 | Non Linear Data strucutes on the other hand are Data structures where data elements are not placed sequentially or linearly are called non-linear data structures. In a non-linear data structure, we can’t traverse all the elements in a single run only. Examples are __trees__ and __graphs__ 17 | 18 | 19 | ## Resources 20 | 21 | 1. [Singly Linked lists](https://www.youtube.com/watch?v=udapt4FGY20) 22 | 2. [Youtube](https://www.youtube.com/results?search_query=linked+lists) 23 | 3. [Google](https://www.google.com/search?q=linked+lists) 24 | 25 | 26 | ## Learnig objectives 27 | After going through the above resource, you should be able to [explain to anyone](https://fs.blog/feynman-learning-technique/) without the help of google the following. 28 | 29 | * [X] When and why using linked lists vs arrays 30 | * [X] How to build and use linked lists 31 | 32 | ## Quiz 33 | [Quizes](./quiz.md) -------------------------------------------------------------------------------- /0x0E-structures_typedef/4-new_dog.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | 4 | int _strlen(char *s); 5 | char *_strcpy(char *dest, char *src); 6 | 7 | /** 8 | * new_dog - ... 9 | * @name: ... 10 | * @age: ... 11 | * @owner: ... 12 | * 13 | * Return: ... 14 | */ 15 | dog_t *new_dog(char *name, float age, char *owner) 16 | { 17 | dog_t *cutie_dog; 18 | int name_l = 0, own_l = 0; 19 | 20 | if (name != NULL && owner != NULL) 21 | { 22 | name_l = _strlen(name) + 1; 23 | own_l = _strlen(owner) + 1; 24 | cutie_dog = malloc(sizeof(dog_t)); 25 | 26 | if (cutie_dog == NULL) 27 | return (NULL); 28 | 29 | cutie_dog->name = malloc(sizeof(char) * name_l); 30 | 31 | if (cutie_dog->name == NULL) 32 | { 33 | free(cutie_dog); 34 | return (NULL); 35 | } 36 | 37 | cutie_dog->owner = malloc(sizeof(char) * own_l); 38 | 39 | if (cutie_dog->owner == NULL) 40 | { 41 | free(cutie_dog->name); 42 | free(cutie_dog); 43 | return (NULL); 44 | } 45 | 46 | cutie_dog->name = _strcpy(cutie_dog->name, name); 47 | cutie_dog->owner = _strcpy(cutie_dog->owner, owner); 48 | cutie_dog->age = age; 49 | } 50 | 51 | return (cutie_dog); 52 | } 53 | 54 | /** 55 | * _strlen - Returns the length of a string 56 | * @s: String to count 57 | * 58 | * Return: String length 59 | */ 60 | int _strlen(char *s) 61 | { 62 | int c = 0; 63 | 64 | for (; *s != '\0'; s++) 65 | { 66 | c++; 67 | } 68 | 69 | return (c); 70 | } 71 | 72 | /** 73 | * _strcpy - Copy a string 74 | * @dest: Destination value 75 | * @src: Source value 76 | * 77 | * Return: the pointer to dest 78 | */ 79 | char *_strcpy(char *dest, char *src) 80 | { 81 | int i; 82 | 83 | for (i = 0; src[i] != '\0'; i++) 84 | { 85 | dest[i] = src[i]; 86 | } 87 | 88 | dest[i++] = '\0'; 89 | 90 | return (dest); 91 | } 92 | -------------------------------------------------------------------------------- /0x0B-malloc_free/README.md: -------------------------------------------------------------------------------- 1 | ![img](https://assets.imaginablefutures.com/media/images/ALX_Logo.max-200x150.png) 2 | > Dynamic memory allocation 3 | 4 | ![meme](https://s3.amazonaws.com/intranet-projects-files/holbertonschool-low_level_programming/216/IMG_2410.JPG) 5 | 6 | ## Intro 7 | Dynamic memory allocation is an important concept in C programming, allowing for efficient use of memory by allocating and freeing memory as needed. In this blog post, we will explore the use of the malloc function to dynamically allocate memory in C, including examples and best practices for proper usage 8 | 9 | ## About 10 | In this session, we well cover dyanamic memory allocation in C, why is it important and how it is implemted. 11 | Buckle up, it might get bumpy!! 12 | 13 | ## Resources 14 | 1. [Dyamic memory alloation overview](https://s3.amazonaws.com/alx-intranet.hbtn.io/uploads/misc/2021/1/a094c90e7f466bbeaa49cb24c8f04e7f27aaad41.pdf?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIARDDGGGOUSBVO6H7D%2F20221014%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20221014T190316Z&X-Amz-Expires=86400&X-Amz-SignedHeaders=host&X-Amz-Signature=2bc8065b92c043249914c3bcec07899997842025ce5bd687508d77fafcda9bc3) 15 | 2. [Youtube](https://www.youtube.com/watch?v=xDVC3wKjS64) 16 | 17 | ## Man/Help 18 | 19 | - malloc 20 | - free 21 | 22 | ## Learning objectives 23 | After going through the above resources, you should be able to [explain to anyone](https://fs.blog/feynman-learning-technique/) without the help of Goolge the following concepts. 24 | 25 | 26 | 27 | * [X] What is the difference between automatic and dynamic allocation 28 | * [X] What is malloc and free and how to use them 29 | * [X] Why and when use malloc 30 | * [X] How to use valgrind to check for memory leak 31 | 32 | ## More info 33 | You can learn about ```calloc``` and ```realloc```. 34 | 35 | ## Quizes 36 | [Quiz](./quiz.md) -------------------------------------------------------------------------------- /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: hash table 5 | * @key: is the key. key can not be an empty string 6 | * @value: value associated with the key. 7 | * value must be duplicated. value can be an empty string 8 | * Return: 1 on success, 0 on failurre 9 | */ 10 | int hash_table_set(hash_table_t *ht, const char *key, const char *value) 11 | { 12 | 13 | hash_node_t *node; 14 | hash_node_t *new_node; 15 | unsigned long int index; 16 | 17 | if (ht == NULL || *key == '\n' || *value == '\n') 18 | return (0); 19 | 20 | index = key_index((const unsigned char *)key, ht->size); 21 | node = ht->array[index]; 22 | 23 | if (node == NULL) 24 | { 25 | new_node = create_new_node(key, value); 26 | if (new_node == NULL) 27 | return (0); 28 | 29 | ht->array[index] = new_node; 30 | return (1); 31 | } 32 | 33 | /*If key exists, replace value*/ 34 | while (node != NULL) 35 | { 36 | if (strcmp(key, node->key) == 0) 37 | { 38 | free(node->value); 39 | node->value = strdup(value); 40 | return (1); 41 | } 42 | node = node->next; 43 | } 44 | /*If key doesn't exist, create new node*/ 45 | new_node = create_new_node(key, value); 46 | if (new_node == NULL) 47 | return (0); 48 | 49 | new_node->next = ht->array[index]; 50 | ht->array[index] = new_node; 51 | return (1); 52 | } 53 | 54 | /** 55 | * create_new_node - create a new node 56 | * @key: is the key. key can not be an empty string 57 | * @value: value associated with the key. 58 | * value must be duplicated. value can be an empty string 59 | * Return: 1 on success, 0 on failurre 60 | */ 61 | 62 | hash_node_t *create_new_node (const char *key, const char *value) 63 | { 64 | hash_node_t *new_node; 65 | 66 | new_node = malloc(sizeof(hash_node_t)); 67 | 68 | if (new_node == NULL) 69 | return (NULL); 70 | 71 | new_node->key = strdup(key); 72 | new_node->value = strdup(value); 73 | new_node->next = NULL; 74 | 75 | return (new_node); 76 | } 77 | -------------------------------------------------------------------------------- /0x1C-makefiles/README.md: -------------------------------------------------------------------------------- 1 | ![img](https://assets.imaginablefutures.com/media/images/ALX_Logo.max-200x150.png) 2 | > Makefiles 3 | 4 | ## About 5 | A makefile is a script that automates the building of a program from source code. In the case of C programs, a makefile typically includes instructions for compiling the source code into object files and linking those object files into an executable program. Makefiles can greatly simplify the build process by automating repetitive tasks and allowing developers to easily manage complex build configurations. In this context, C makefiles provide a way to manage the build process for C programs in a structured and efficient manner. Hang tight as we dive into making makefiles. ^_^ 6 | 7 | 8 | ![make-files](https://s3.amazonaws.com/intranet-projects-files/holbertonschool-low_level_programming/273/giphy-2.gif) 9 | 10 | ## Resources 11 | __Read or Watch__ 12 | 1. [Makefiles](https://www.google.com/search?q=makefile) 13 | 2. [Installing the make utility](https://www.geeksforgeeks.org/how-to-install-make-on-ubuntu/) 14 | 3. [make - official documentation](https://www.gnu.org/software/make/manual/html_node/) 15 | 16 | ## Learning objectives 17 | By the end of this project, you should be able to[explain to anyone](https://fs.blog/feynman-learning-technique/) __without the help of Google__ 18 | 19 | * [X] What are ```make```, Makefiles 20 | * [X] When, why and how to use Makefiles 21 | * [X] What are rules and how to set and use them 22 | * [X] What are explicit and implicit rules 23 | * [X] What are the most common / useful rules 24 | * [X] What are variables and how to set and use them 25 | 26 | ## Requirements 27 | ### General 28 | - Allowed editors: ```vi```, ```vim```, ```emacs``` 29 | - OS: Ubuntu 20.04 LTS 30 | - Version of ```gcc```: 9.3.0 31 | - Version of ```make```: GNU Make 4.2.1 32 | - All your files should end with a new line 33 | 34 | ## More info 35 | ### Files 36 | In the following task, we are going to use [these files](https://github.com/holbertonschool/0x1B.c) we want to compile these only -------------------------------------------------------------------------------- /0x01-variables_if_else_while/README.md: -------------------------------------------------------------------------------- 1 | ![img](https://assets.imaginablefutures.com/media/images/ALX_Logo.max-200x150.png) 2 | >Program control structures - if else 3 | 4 | ## Intro 5 | In this section, we will take a look at program control structures - branching (if else ) 6 | 7 | ## Resources 8 | 1. [Keywords and Identifiers in C](https://publications.gbdirect.co.uk//c_book/chapter2/keywords_and_identifiers.html) 9 | 2. [Integral Data types in C](https://publications.gbdirect.co.uk//c_book/chapter2/integral_types.html) 10 | 3. [ Generaly Operators in C](https://www.studytonight.com/c/operators-in-c.php) 11 | 4. [if function in C](https://www.cprogramming.com/tutorial/c/lesson2.html) 12 | 5. [if else statements in C](https://www.tutorialspoint.com/cprogramming/if_else_statement_in_c.htm) 13 | 6. [While loop in C](https://www.tutorialspoint.com/cprogramming/c_while_loop.htm) 14 | 15 | ## All Operators 16 |
    17 |
  • Arithmetic Operators
  • 18 |
  • Relational Operators
  • 19 |
  • Logical Operators
  • 20 |
  • Bitwise Operators
  • 21 |
  • Assignment Operators
  • 22 |
  • Conditional Operators
  • 23 |
  • Special Operators
  • 24 |
25 | 26 | ## Learning objectives 27 | 28 | * [X] What are the arithmetic operators and how to use them 29 | * [X] What are the logical operators (sometimes called boolean operators) and how to use them 30 | * [X] What the the relational operators and how to use them 31 | * [X] What values are considered TRUE and FALSE in C 32 | * [X] What are the boolean operators and how to use them 33 | * [X] How to use the if, if ... else statements 34 | * [X] How to use comments 35 | * [X] How to declare variables of types char, int, unsigned int 36 | * [X] How to assign values to variables 37 | * [X] How to print the values of variables of type char, int, unsigned int with printf 38 | * [X] How to use the while loop 39 | * [X] How to use variables with the while loop 40 | * [X] How to print variables using printf 41 | * [X] What is the ASCII character set 42 | * [X] What are the purpose of the gcc flags ```-m32 and -m64``` 43 | -------------------------------------------------------------------------------- /0x00-hello_world/README.md: -------------------------------------------------------------------------------- 1 | ![img](https://assets.imaginablefutures.com/media/images/ALX_Logo.max-200x150.png) 2 | > Introduction to programming in C 3 | 4 | ## Intro 5 | In this session, we will take a deep dive into introduction to programming in C 6 | 7 | ## Resources 8 | 1. [A brief history of the C programming Language](https://www.javatpoint.com/history-of-c-language) 9 | 2. [The C program compililation Pipeline](https://hackthedeveloper.com/c-program-compilation-process/) 10 | 3. [How to print out text to the screen (using printf() or puts() )](https://stackoverflow.com/questions/2454474/what-is-the-difference-between-printf-and-puts-in-c) 11 | 4. [Betty coding style.... No its not what you think](https://github.com/holbertonschool/Betty/wiki) 12 | 5. [How does the main() function influence the return value of a program?](https://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c) 13 | 6. [Header files in C](https://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c) 14 | 7. [Your first C program](https://www.javatpoint.com/first-c-program) 15 | 8. [Keywords or reserved words in C](https://www.javatpoint.com/keywords-in-c) 16 | 9. [Data types in C](https://byjus.com/gate/data-types-in-c/) 17 | 18 | 19 | ## Man/Help 20 | - gcc 21 | - printf 22 | - puts 23 | - putchar 24 | 25 | ## Learning objectives 26 | By the end of the session, you should have a pretty solid understanding of the following. 27 | 28 | 29 | * [X] Why C programming is awesome 30 | * [X] Who invented C 31 | * [X] Who are Dennis Ritchie, Brian Kernighan and Linus Torvalds 32 | * [X] How to compile using gcc 33 | * [X] What happens when you type gcc main.c 34 | * [X] What is an entry point 35 | * [X] What is main 36 | * [X] How to print text using printf, puts and putchar 37 | * [X] How to get the size of a specific type using the unary operator sizeof 38 | * [X] What is the default program name when compiling with gcc 39 | * [X] What is the official C coding style and how to check your code with betty-style 40 | * [X] How to find the right header to include in your source code when using a standard library function 41 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/README.md: -------------------------------------------------------------------------------- 1 | ![img](https://assets.imaginablefutures.com/media/images/ALX_Logo.max-200x150.png) 2 | > Structures and unions 3 | 4 | ![dog](https://s3.amazonaws.com/alx-intranet.hbtn.io/uploads/medias/2021/3/50af78a28a081e809856d4cdbde2d7ca9d4aa93d.jpg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIARDDGGGOUSBVO6H7D%2F20230114%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20230114T184500Z&X-Amz-Expires=86400&X-Amz-SignedHeaders=host&X-Amz-Signature=8dda42f1c7706a06a17645a717a3bed6766afa8a385386824374406ace54bef7) 5 | 6 | ## Intro 7 | __The power of ```typedef``` in C!__ In this session, we will focus on how ```typedef``` can be used to improve the readability and maintainability of your C code. We'll cover everything from the basics of typedef to advanced applications, and explore how it can be used to simplify complex data structures and make your code more intuitive. Buckle up as we demystify this often-underutilized feature of the C programming language 8 | 9 | ## Resources 10 | Here is a curated list of resources to help you get started. 11 | 1. [Introduction to structures](https://s3.amazonaws.com/alx-intranet.hbtn.io/uploads/misc/2021/1/6eb80c79c99f6125450a0dc11b300d46238d1a5a.pdf?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIARDDGGGOUSBVO6H7D%2F20221016%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20221016T131613Z&X-Amz-Expires=86400&X-Amz-SignedHeaders=host&X-Amz-Signature=85c5048af18c4a72170da4a901b7f2f30db32f1d84b13b02dbd5cb9b2f93cdec) 12 | 2. [The struct keyword in C](https://en.wikipedia.org/wiki/Struct_(C_programming_language)) 13 | 3. [How should you doucument your structures ](https://github.com/holbertonschool/Betty/wiki/Documentation:-Data-structures) 14 | 4. [The typedef statement](https://publications.gbdirect.co.uk//c_book/chapter8/typedef.html) 15 | 5. [The art of C strucure packing](http://www.catb.org/esr/structure-packing/) 16 | 6. [What does Google say about structures](https://www.google.com/search?q=Structures+in+C) 17 | 18 | ## Learning objectives 19 | At the end of this session, you should be able to [explain to anyone](https://fs.blog/feynman-learning-technique/) the following concepts without the help of Google 20 | 21 | 22 | * [X] What are structures, when, why and how to use them 23 | * [X] How to use typedef 24 | 25 | ## Quizes 26 | [Quiz](./quiz.md) -------------------------------------------------------------------------------- /0x0C-more_malloc_free/quiz.md: -------------------------------------------------------------------------------- 1 | ![img](https://assets.imaginablefutures.com/media/images/ALX_Logo.max-200x150.png) 2 | > More on dynamic memory management quizes 3 | 4 | #### Question #0 5 | What is wrong with this code? 6 | 7 | ```c 8 | int cp(void) 9 | { 10 | char *s; 11 | 12 | s = malloc(12); 13 | strcpy(s, "Best School"); 14 | return (0); 15 | } 16 | ``` 17 | * [ ] You can’t call ```strcpy``` with a string literal 18 | * [ ] You don’t have enough space to store the copy of the string “Best School” 19 | * [X] There is no comment 20 | * [X] ```malloc``` can fail so we should check its return value all the time before using the pointers returned by the function. 21 | 22 | #### Question #1 23 | You can do this: 24 | ```c 25 | char *s; 26 | 27 | s = strdup("Best School"); 28 | if (s != NULL) 29 | { 30 | free(s); 31 | } 32 | ``` 33 | * [X] Yes 34 | * [ ] No 35 | 36 | #### Question #2 37 | You can do this: 38 | ```c 39 | char str[] = "Best School"; 40 | 41 | free (str); 42 | ``` 43 | * [ ] Yes 44 | * [X] No 45 | 46 | #### Question #3 47 | If I want to copy the string “Best School” into a new space in memory, I can use this statement to reserve enough space for it (select all valid answers): 48 | 49 | * [ ] malloc(11) 50 | * [X] malloc(strlen(“Best School”) + 1) 51 | * [ ] malloc(sizeof(“Best School”) + 1) 52 | * [ ] malloc(strlen(“Best School”)) 53 | * [X] malloc(sizeof(“Best School”)) 54 | * [X] malloc(12) 55 | 56 | #### Question #4 57 | malloc returns a pointer 58 | 59 | * [X] True 60 | * [ ] False 61 | 62 | #### Question #5 63 | To allocate enough space for an array of 10 integers (on a 64bit, Linux machine), I can use: 64 | 65 | * [X] malloc(10 * sizeof(int)) 66 | * [ ] malloc(64 * 10) 67 | * [ ] malloc(10 * int) 68 | 69 | #### Question #6 70 | What will you see on the terminal? 71 | ```c 72 | int main(void) 73 | { 74 | int *ptr; 75 | 76 | *ptr = 98; 77 | printf("%d\n", *ptr); 78 | return (0); 79 | } 80 | ``` 81 | * [ ] It does not compile 82 | * [ ] 0 83 | * [ ] 98 84 | * [X] Segmentation fault. 85 | > ### Tip 86 | > Atempting to dereference an unitialized pointer or pointer pointing to an undefined memmory location will cuase a segmentation fault. 87 | 88 | #### Question #7 89 | You can do this 90 | ```c 91 | free("Best School"); 92 | ``` 93 | * [ ] Yes 94 | * [X] No 95 | 96 | #### Question #8 97 | The memory space reserved when calling ```malloc``` is on: 98 | * [ ] The stack 99 | * [X] The heap 100 | 101 | #### Question #9 102 | malloc returns an address 103 | 104 | * [ ] False 105 | * [X] True 106 | -------------------------------------------------------------------------------- /0x1A-hash_tables/README.md: -------------------------------------------------------------------------------- 1 | ![img](https://assets.imaginablefutures.com/media/images/ALX_Logo.max-200x150.png) 2 | > Hash tables 3 | 4 | ## Intro 5 | Hash tables are a fundamental data structure that are used in a wide range of applications, from databases to web browsers to programming languages. They are designed to efficiently store and retrieve data, and they are an essential tool for any developer who wants to write efficient and scalable software. In this session, we'll take a deep dive into hash tables, exploring how they work and how they can be used to solve common problems in computer science. 6 | 7 | ## Resources 8 | 1. [Data Structures - Hash Tables](https://www.youtube.com/watch?v=MfhjkfocRR0) 9 | 2. [Hash function](https://en.wikipedia.org/wiki/Hash_function) 10 | 3. [Hash table](https://en.wikipedia.org/wiki/Hash_table) 11 | 4. [All about hashtables](https://www.digitalocean.com/community/tutorials/hash-table-in-c-plus-plus) 12 | 5. [Why hash tables and not arrays](https://stackoverflow.com/questions/31930046/what-is-a-hash-table-and-how-do-you-make-it-in-c) 13 | 14 | ## Learning objectives 15 | After going through the above resources, you should be able to explain the following conepts to anyone. 16 | 17 | * [X] What is a hash function 18 | * [X] What makes a good hash function 19 | * [X] What is a hash table, how do they work and how to use them 20 | * [X] What is a collision and what are the main ways of dealing with collisions in the context of a hash table 21 | * [X] What are the advantages and drawbacks of using hash tables 22 | * [X] What are the most common use cases of hash tables 23 | 24 | ## More info 25 | ### Data structures 26 | Please use these data structures for the project. 27 | ```c 28 | /** 29 | * struct hash_node_s - Node of a hash table 30 | * 31 | * @key: The key, string 32 | * The key is unique in the HashTable 33 | * @value: The value corresponding to a key 34 | * @next: A pointer to the next node of the List 35 | */ 36 | typedef struct hash_node_s 37 | { 38 | char *key; 39 | char *value; 40 | struct hash_node_s *next; 41 | } hash_node_t; 42 | 43 | /** 44 | * struct hash_table_s - Hash table data structure 45 | * 46 | * @size: The size of the array 47 | * @array: An array of size @size 48 | * Each cell of this array is a pointer to the first node of a linked list, 49 | * because we want our HashTable to use a Chaining collision handling 50 | */ 51 | typedef struct hash_table_s 52 | { 53 | unsigned long int size; 54 | hash_node_t **array; 55 | } hash_table_t; 56 | ``` 57 | 58 | ### Python dictionaries 59 | Python dictionaries are implemented using hash tables. When you will be done with this project, you will be able to better understand the power and simplicity of Python dictionaries. So much is actually happening when you type ```d = {'a': 1, 'b': 2}```, but everything looks so simple for the user. Python doesn’t use the exact same implementation than the one you will work on today though. If you are curious on how it works under the hood, here is a good blog post about [how dictionaries are implemented in python 2.7](http://www.laurentluce.com/posts/python-dictionary-implementation/) 60 | 61 | Note that all dictionaries are not implemented using hash tables and there is a difference between a dictionary and a hash table. [Read more](https://stackoverflow.com/questions/2061222/what-is-the-true-difference-between-a-dictionary-and-a-hash-table) -------------------------------------------------------------------------------- /0x0C-more_malloc_free/README.md: -------------------------------------------------------------------------------- 1 | ![img](https://assets.imaginablefutures.com/media/images/ALX_Logo.max-200x150.png) 2 | > More on dynamic memory management 3 | 4 | ## Intro 5 | In this session, we will explore the dynamic memory allocation and deallocation in C programming. We will discuss the use of the malloc and free functions, the advantages and disadvantages of dynamic memory allocation, and various pitfalls to watch out for. Hang on as we dive deep into the world of malloc and free and learn how to effectively manage memory in C programming. 6 | ## About 7 | Before we have a look at static and dynamic memory allocation, What exactly is memory allocation in the first place? 8 | 9 | Memory allocation is the process of allocating physical or virtual memory space to computer programs and services. It divided into two. 10 | 11 | 1. Static memory allocation. 12 | 2. Dynamic memory allocation. 13 | 14 | ## Dynamic VS static memory allocation in C. 15 | Below are the major difference between the two. 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 |
Dynamic Memory allocationStatic Memory allocation
Memory is allocated at run timeMemory is allocated at compile time
Memory can be increased while executing programMemory can't be increased while executing program
Used in Linked ListUsed in Arrays
Allocation is done from the Heap MemoryAllocation is done from the Stack Memory
Allocated memory can be released and used again if not required anymoreMemory allocated cannot be reused
More Efficient memory management methodLess Efficient memory managenent method
51 | 52 |
53 | Dynamic memory allocation in C using malloc() 54 | 55 | So What exactly is malloc() and why is it important? 56 | 57 | The malloc() function is used to allocate a certain amount of memory durign execution of a program. 58 | It will request a block of memory ( as specified in the malloc call) from the heap and once the request is granted, the Operating system will reserve the requested block of memory and malloc will return a pointer to the reserved space. 59 | 60 | When the amount of memory is no longer needed anymore, you need to return it to the operating system by calling free() 61 | 62 | The prototype for the malloc funtion is void *malloc(size_t size); where size is the amount of memory you want to allocate in bytes 63 | 64 | The malloc funtion returns a void pointer, which begs the next question, [Should I cast the result of malloc?](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) 65 |
66 | 67 |
68 | Memory allocation using calloc() 69 | How to use calloc 70 |
71 | The basic syntax when using calloc is 72 | 73 | ptr = (cast-type*)calloc(n, element-size); 74 | 75 |
76 | 77 |
78 | 79 | ## Resources 80 | [Difference between calloc and malloc ](https://www.geeksforgeeks.org/difference-between-malloc-and-calloc-with-examples/) 81 | 82 | ## Man/Help 83 | 84 | - exit(3) 85 | - calloc 86 | - malloc 87 | - free 88 | 89 | ## Learning objectives 90 | * [X] How to use the ```exit``` function 91 | * [X] What are the functions ``calloc``` and ```realloc``` from the standard library and how to use them 92 | 93 | ## Quizes 94 | [Quiz](./quiz.md) -------------------------------------------------------------------------------- /0x0D-preprocessor/quiz.md: -------------------------------------------------------------------------------- 1 | ![img](https://assets.imaginablefutures.com/media/images/ALX_Logo.max-200x150.png) 2 | > Preprocessor and macros quizes 3 | 4 | #### Question #0 5 | 6 | This is the correct way to define the macro SUB: 7 | ```c 8 | #define SUB(a, b) a - b 9 | ``` 10 | * [ ] No, it should be written this way: 11 | ```c 12 | #define SUB(a, b) (a) - (b) 13 | ``` 14 | * [ ] Yes 15 | * [ ] No, it should be written this way: 16 | ```c 17 | #define SUB(a, b) (a - b) 18 | ``` 19 | * [X] No, it should be written this way: 20 | ```c 21 | #define SUB(a, b) ((a) - (b)) 22 | ``` 23 | 24 | #### Question #1 25 | The macro ```__FILE__``` expands to the name of the current input file, in the form of a C string constant. 26 | 27 | * [X] True 28 | * [ ] False 29 | 30 | #### Question #2 31 | This portion of code is actually using the library stdlib. 32 | ```c 33 | #include 34 | ``` 35 | * [ ] True 36 | * [X] False 37 | 38 | #### Question #3 39 | ```NULL``` is a macro 40 | 41 | * [X] True 42 | * [ ] False 43 | 44 | #### Question #4 45 | What is the ```gcc``` option that runs only the preprocessor? 46 | 47 | * [ ] -p 48 | * [ ] -preprocessor 49 | * [X] -E 50 | * [ ] -P 51 | * [ ] -a 52 | * [ ] -cisfun 53 | * [ ] -pedantic 54 | 55 | #### Question #5 56 | The preprocessor generates assembly code 57 | 58 | * [ ] True 59 | * [X] False 60 | 61 | #### Question #6 62 | The preprocessor generates object code 63 | 64 | * [ ] True 65 | * [X] False 66 | 67 | #### Question #7 68 | Why should we use include guards in our header files? 69 | 70 | * [ ] Because we said so, and we should never ask why. 71 | * [X] To avoid the problem of double inclusion when dealing with the include directive. 72 | 73 | 74 | #### Question #8 75 | What are the steps of compilation? 76 | 77 | * [ ] 1.preprocessor 2.compiler 3. linker 4. assembler 78 | * [ ] 1.compiler 2. preprocessor 3. assembler 4. linker 79 | * [X] 1.preprocessor 2.compiler 3. assembler 4. linker 80 | 81 | #### Question #9 82 | What does the macro ```TABLESIZE``` expand to? 83 | ```c 84 | #define BUFSIZE 1020 85 | #define TABLESIZE BUFSIZE 86 | #undef BUFSIZE 87 | #define BUFSIZE 37 88 | ``` 89 | * [ ] nothing 90 | * [ ] 1020 91 | * [X] 37 92 | 93 | #### Question #10 94 | This code will try to allocate 1024 bytes in the heap: 95 | ```c 96 | #define BUFFER_SIZE 1024 97 | malloc(BUFFER_SIZE) 98 | ``` 99 | * [X] True 100 | * [ ] False 101 | 102 | #### Question #11 103 | The preprocessor removes all comments 104 | 105 | * [X] True 106 | * [ ] False 107 | 108 | #### Question #12 109 | What will be the last 5 lines of the output of the command ```gcc -E ``` on this code? 110 | ```c 111 | #include 112 | 113 | int main(void) 114 | { 115 | NULL; 116 | return (EXIT_SUCCESS); 117 | } 118 | ``` 119 | * [X] 120 | ```c 121 | int main(void) 122 | { 123 | ((void *)0); 124 | return (0); 125 | } 126 | ``` 127 | 128 | * [ ] 129 | ```c 130 | int main(void) 131 | { 132 | 0; 133 | return (0); 134 | } 135 | ``` 136 | * [ ] 137 | ```c 138 | int main() 139 | { 140 | 0; 141 | return (0); 142 | } 143 | ``` 144 | * [ ] 145 | ```c 146 | int main(void) 147 | { 148 | '\0'; 149 | return (0); 150 | } 151 | ``` 152 | 153 | #### Question #13 154 | What will be the output of this program? (on a standard 64 bits, Linux machine) 155 | ```c 156 | #include 157 | #include 158 | 159 | #define int char 160 | 161 | int main(void) 162 | { 163 | int i; 164 | 165 | i = 5; 166 | printf ("sizeof(i) = %lu", sizeof(i)); 167 | return (EXIT_SUCCESS); 168 | } 169 | ``` 170 | * [ ] sizeof(i) = 8 171 | * [X] sizeof(i) = 1 172 | * [ ] sizeof(i) = 4 173 | * [ ] It does not compile 174 | * [ ] Segmentation Fault 175 | * [ ] sizeof(i) = 5 176 | 177 | #### Question #14 178 | The preprocessor links our code with libraries. 179 | 180 | * [ ] True 181 | * [X] False 182 | -------------------------------------------------------------------------------- /0x15-file_io/quiz.md: -------------------------------------------------------------------------------- 1 | ![img](https://assets.imaginablefutures.com/media/images/ALX_Logo.max-200x150.png) 2 | > File I/O quizes 3 | 4 | 5 | #### Question #0 6 | Why? #AlwaysAskWhy 7 | * [ ] Because this will be the second opened file descriptor for my process 8 | * [ ] I don’t care I never ask why, just let me access the tasks! 9 | * [X] Because most of the time, I will already have stdin (value 0), stdout (value 1) and stderr (value 2) opened when my program starts executing. 10 | * [ ] Because this will be the first opened file descriptor and we start counting starting from 1 11 | * [ ] Because this will be the first opened file descriptor and in CS we start counting starting from 0 12 | * [ ] Because this will be the third opened file descriptor for my process 13 | 14 | #### Question #1 15 | What happens if you try to write “Best” to the standard input on Ubuntu 14.04 LTS? 16 | 17 | * [ ] The text will be printed on the terminal but I can’t pipe it 18 | * [ ] Segmentation fault 19 | * [ ] Nothing 20 | * [X] The text will be printed on the terminal on the standard output 21 | > #### Tips: 22 | > Just try it:) 23 | 24 | #### Question #2 25 | What is the ```oflag``` used to open a file in mode read + write? 26 | 27 | * [X] ```O_RDWR``` 28 | * [ ] ```O_WRONLY``` 29 | * [ ] ```O_RDONLY``` 30 | 31 | #### Question #3 32 | What system call would you use to write to a file descriptor? (select all correct answers) 33 | * [X] write 34 | * [ ] fprint 35 | * [ ] printf 36 | 37 | #### Question #4 38 | is open a function or a system call? (select all valid answers) 39 | * [ ] it’s a library call 40 | * [X] it’s a function 41 | * [X] it’s a sytem call 42 | * [ ] it’s a kernel routine 43 | * [ ] it's a function provided by the kernel 44 | 45 | #### Question #5 46 | What is the ```unistd``` symbolic constant for the standard output? 47 | * [ ] STDERR_FILENO 48 | * [ ] STDIN_FILENO 49 | * [X] STDOUT_FILENO 50 | 51 | #### Question #6 52 | What is the ```unistd``` symbolic constant for the Standard error? 53 | * [X] STDERR_FILENO 54 | * [ ] STDIN_FILENO 55 | * [ ] STDOUT_FILENO 56 | 57 | #### Question #7 58 | Which of these answers are the equivalent of ```O_RDWR``` on Ubuntu 14.04 LTS? (select all correct answers): 59 | * [X] 2 60 | * [X] (O_WRONLY << 1) 61 | * [ ] 3 | 2 62 | * [ ] (O_RDONLY << 1) 63 | * [ ] (O_RDONLY && O_WRONLY) 64 | * [ ] 0 65 | * [ ] 3 66 | * [ ] O_WRONLY 67 | * [ ] (O_RDONLY & O_WRONLY) 68 | * [ ] (O_RDONLY + O_WRONLY) 69 | * [ ] 1 70 | * [X] 1 << 1 71 | * [ ] O_RDONLY 72 | * [X] 3 & 2 73 | * [ ] (O_RDONLY | O_WRONLY) 74 | > ### Tips: 75 | >Use printf or read the headers to see the definitions/values of these macros. 76 | 77 | #### Question #8 78 | What is the unistd symbolic constant for the standard input? 79 | * [ ] STDERR_FILENO 80 | * [X] STDIN_FILENO 81 | * [ ] STDOUT_FILENO 82 | 83 | #### Question #9 84 | Most of the time, on a classic, modern Linux system, what will be the value of the first file descriptor you will get after opening a new file with ```open``` (if ```open``` succeeds of course): 85 | * [ ] 2 86 | * [ ] 6 87 | * [ ] 4 88 | * [ ] 1 89 | * [ ] 0 90 | * [ ] 5 91 | * [X] 3 92 | 93 | #### Question #10 94 | What is the return value of the system call ```open``` if it fails? 95 | 96 | * [ ] 98 97 | * [ ] 0 98 | * [X] -1 99 | 100 | #### Question #11 101 | What is the correct combination of ```oflags``` used to open a file with the mode write only, create it if it doesn’t exist and append new content at the end if it already exists? 102 | * [X] ```O_WRONLY | O_CREAT | O_APPEND``` 103 | * [ ] ```O_WRONLY``` 104 | * [ ] ```O_WRONLY | O_CREAT | O_EXCL``` 105 | * [ ] ```O_RDWR | O_CREAT | O_APPEND``` 106 | 107 | #### Question #12 108 | Without context, on Ubuntu 14.04 LTS, ```write``` is a … (please select all correct answers): 109 | * [ ] library call 110 | * [X] executable 111 | * [X] system call 112 | * [ ] kernel routine 113 | * [ ] game 114 | 115 | #### Question #13 116 | When I am using ```O_WRONLY | O_CREAT | O_APPEND``` -> the | are bitwise operators. 117 | * [X] True 118 | * [ ] False 119 | 120 | #### Question #14 121 | What is the ```oflag``` used to open a file with the mode read only? 122 | * [ ] ```O_RDWR``` 123 | * [ ] ```O_WRONLY``` 124 | * [X] ```O_RDONLY``` 125 | --------------------------------------------------------------------------------