├── 0x00-hello_world ├── 0-preprocessor ├── 1-compiler ├── 100-intel ├── 101-quote.c ├── 2-assembler ├── 3-name ├── 4-puts.c ├── 5-printf.c ├── 6-size.c └── README.md ├── 0x01-variables_if_else_while ├── 0-positive_or_negative.c ├── 1-last_digit.c ├── 100-print_comb3.c ├── 101-print_comb4.c ├── 102-print_comb5.c ├── 2-print_alphabet.c ├── 3-print_alphabets.c ├── 4-print_alphabt.c ├── 5-print_numbers.c ├── 6-print_numberz.c ├── 7-print_tebahpla.c ├── 8-print_base16.c ├── 9-print_comb.c └── README.md ├── 0x02-functions_nested_loops ├── 0-putchar.c ├── 1-alphabet.c ├── 10-add.c ├── 100-times_table.c ├── 101-natural.c ├── 102-fibonacci.c ├── 103-fibonacci.c ├── 104-fibonacci.c ├── 11-print_to_98.c ├── 2-print_alphabet_x10.c ├── 3-islower.c ├── 4-isalpha.c ├── 5-sign.c ├── 6-abs.c ├── 7-print_last_digit.c ├── 8-24_hours.c ├── 9-times_table.c ├── README.md └── main.h ├── 0x03-debugging ├── 0-main.c ├── 1-main.c ├── 2-largest_number.c ├── 3-main_b.c ├── 3-print_remaining_days.c ├── README.md └── main.h ├── 0x04-more_functions_nested_loops ├── 0-isupper.c ├── 1-isdigit.c ├── 10-print_triangle.c ├── 100-prime_factor.c ├── 101-print_number.c ├── 2-mul.c ├── 3-print_numbers ├── 3-print_numbers.c ├── 4-print_most_numbers ├── 4-print_most_numbers.c ├── 5-more_numbers ├── 5-more_numbers.c ├── 6-lines ├── 6-print_line.c ├── 7-print_diagonal.c ├── 8-print_square.c ├── 9-fizz_buzz ├── 9-fizz_buzz.c ├── README.md ├── _putchar.c ├── main.c └── main.h ├── 0x05-pointers_arrays_strings ├── 0-reset_to_98.c ├── 1-swap.c ├── 100-atoi.c ├── 101-keygen.c ├── 2-strlen.c ├── 3-puts.c ├── 4-print_rev.c ├── 5-rev_string.c ├── 6-puts2.c ├── 7-puts_half.c ├── 8-print_array.c ├── 9-strcpy.c ├── README.md └── main.h ├── 0x06-pointers_arrays_strings ├── 0-strcat ├── 0-strcat.c ├── 1-strncat ├── 1-strncat.c ├── 100-rot13.c ├── 101-print_number.c ├── 101-print_numbers ├── 102-magic.c ├── 103-add ├── 103-infinite_add.c ├── 104-buffer ├── 104-print_buffer.c ├── 2-strncpy ├── 2-strncpy.c ├── 3-strcmp ├── 3-strcmp.c ├── 4-rev_array ├── 4-rev_array.c ├── 5-string_toupper ├── 5-string_toupper.c ├── 6-cap ├── 6-cap_string.c ├── 7-1337 ├── 7-leet.c ├── README.md ├── main.c └── main.h ├── 0x07-pointers_arrays_strings ├── 0-memset ├── 0-memset.c ├── 1-memcpy ├── 1-memcpy.c ├── 100-set_string ├── 100-set_string.c ├── 101-crackme_password ├── 2-strchr ├── 2-strchr.c ├── 3-strspn ├── 3-strspn.c ├── 4-strpbrk ├── 4-strpbrk.c ├── 5-strstr ├── 5-strstr.c ├── 7-print_chessboard ├── 7-print_chessboard.c ├── 8-print_diagsums ├── 8-print_diagsums.c ├── README.md ├── _putchar.c ├── crackme2 ├── main.c └── main.h ├── 0x08-recursion ├── 0-puts_recursion ├── 0-puts_recursion.c ├── 1-print_rev_recursion.c ├── 100-is_palindrome.c ├── 100-palindrome ├── 101-wildcmp ├── 101-wildcmp.c ├── 2-strlen_recursion ├── 2-strlen_recursion.c ├── 3-factorial ├── 3-factorial.c ├── 4-pow ├── 4-pow_recursion.c ├── 5-sqrt ├── 5-sqrt_recursion.c ├── 6-is_prime_number.c ├── README.md ├── _putchar.c ├── main.c └── main.h ├── 0x09-static_libraries ├── 0-isupper.c ├── 0-isupper.o ├── 0-memset.c ├── 0-memset.o ├── 0-strcat.c ├── 0-strcat.o ├── 1-isdigit.c ├── 1-isdigit.o ├── 1-memcpy.c ├── 1-memcpy.o ├── 1-strncat.c ├── 1-strncat.o ├── 100-atoi.c ├── 100-atoi.o ├── 2-strchr.c ├── 2-strchr.o ├── 2-strlen.c ├── 2-strlen.o ├── 2-strncpy.c ├── 2-strncpy.o ├── 3-islower.c ├── 3-islower.o ├── 3-puts.c ├── 3-puts.o ├── 3-strcmp.c ├── 3-strcmp.o ├── 3-strspn.c ├── 3-strspn.o ├── 4-isalpha.c ├── 4-isalpha.o ├── 4-strpbrk.c ├── 4-strpbrk.o ├── 5-strstr.c ├── 5-strstr.o ├── 6-abs.c ├── 6-abs.o ├── 9-strcpy.c ├── 9-strcpy.o ├── README.md ├── _putchar.c ├── _putchar.o ├── create_static_lib.sh ├── libmy.a ├── main.c ├── main.h └── quote ├── 0x0A-argc_argv ├── 0-whatsmyname.c ├── 1-args.c ├── 100-change.c ├── 2-args.c ├── 3-mul.c ├── 4-add.c ├── README.md ├── add ├── args ├── change ├── main.h ├── mul ├── mynameis └── nargs ├── 0x0B-malloc_free ├── 0-create_array.c ├── 1-strdup.c ├── 100-argstostr.c ├── 101-strtow.c ├── 2-str_concat.c ├── 3-alloc_grid.c ├── 4-free_grid.c ├── README.md ├── a ├── args ├── c ├── f ├── g ├── main.c ├── main.h ├── s └── strtow ├── 0x0C-more_malloc_free ├── .101-mul.c.swp ├── .2-calloc.c.swp ├── .3-array_range.c.swp ├── 0-malloc_checked.c ├── 1-string_nconcat ├── 1-string_nconcat.c ├── 100-realloc ├── 100-realloc.c ├── 101-mul ├── 101-mul.c ├── 2-calloc ├── 2-calloc.c ├── 3-array_range ├── 3-array_range.c ├── README.md ├── _putchar.c ├── a ├── main.c └── main.h ├── 0x0D-preprocessor ├── .main.c.swo ├── .main.c.swp ├── 0-object_like_macro.h ├── 02-main.c ├── 1-pi.h ├── 2-main.c ├── 3-function_like_macro.h ├── 4-sum.h ├── README.md ├── a ├── b ├── c ├── cc ├── d ├── e └── main.c ├── 0x0E-structures_typedef ├── 1-init_dog.c ├── 2-print_dog.c ├── 4-new_dog.c ├── 5-free_dog.c ├── README.md ├── a ├── b ├── c ├── dog.h ├── e ├── f └── main.c ├── 0x0F-function_pointers ├── 0-print_name.c ├── 1-array_iterator.c ├── 100-main_opcodes.c ├── 2-int_index.c ├── 3-calc.h ├── 3-get_op_func.c ├── 3-main.c ├── 3-op_functions.c ├── README.md ├── a ├── b ├── c ├── calc ├── function_pointers.h ├── main └── main.c ├── 0x10-variadic_functions ├── 0-sum_them_all.c ├── 1-print_numbers.c ├── 2-print_strings.c ├── 3-print_all.c ├── README.md ├── a ├── b ├── c ├── d ├── main.c └── variadic_functions.h ├── 0x12-singly_linked_lists ├── 0-print_list.c ├── 1-list_len.c ├── 100-first.c ├── 101-hello_holberton.asm ├── 2-add_node.c ├── 3-add_node_end.c ├── 4-free_list.c ├── README.md ├── a ├── b ├── c ├── d ├── e ├── lists.h └── main.c ├── 0x13-more_singly_linked_lists ├── 0-print_listint.c ├── 1-listint_len.c ├── 10-delete_nodeint.c ├── 100-reverse_listint.c ├── 101-print_listint_safe.c ├── 2-add_nodeint.c ├── 3-add_nodeint_end.c ├── 4-free_listint.c ├── 5-free_listint2.c ├── 6-pop_listint.c ├── 7-get_nodeint.c ├── 8-sum_listint.c ├── 9-insert_nodeint.c ├── README.md ├── lists.h └── main.c ├── 0x14-bit_manipulation ├── .3-set_bit.c.swp ├── .main.h.swp ├── 0-binary_to_uint.c ├── 1-print_binary.c ├── 2-get_bit.c ├── 3-set_bit.c ├── 4-clear_bit.c ├── 5-flip_bits.c ├── README.md ├── _putchar.c ├── a ├── b ├── c ├── d ├── e ├── f ├── main.c └── main.h ├── 0x15-file_io ├── 0-read_textfile.c ├── 1-create_file.c ├── 100-elf_header.c ├── 2-append_text_to_file.c ├── 3-cp.c ├── README.md └── main.h ├── 0x17-doubly_linked_lists ├── 0-print_dlistint.c ├── 1-dlistint_len.c ├── 100-password ├── 102-result ├── 103-keygen.c ├── 2-add_dnodeint.c ├── 3-add_dnodeint_end.c ├── 4-free_dlistint.c ├── 5-get_dnodeint.c ├── 6-sum_dlistint.c ├── 7-insert_dnodeint.c ├── 8-delete_dnodeint.c ├── README.md └── lists.h ├── 0x18-dynamic_libraries ├── .gitignore ├── 1-create_dynamic_lib.sh ├── 100-operations.so ├── 101-make_me_wish.sh ├── README.md ├── liball.so ├── libdynamic.so └── main.h ├── 0x1A-hash_tables ├── 0-hash_table_create.c ├── 1-djb2.c ├── 100-sorted_hash_table.c ├── 2-key_index.c ├── 3-hash_table_set.c ├── 4-hash_table_get.c ├── 5-hash_table_print.c ├── 6-hash_table_delete.c ├── README.md └── hash_tables.h ├── 0x1C-makefiles ├── 0-Makefile ├── 1-Makefile ├── 100-Makefile ├── 2-Makefile ├── 3-Makefile ├── 4-Makefile ├── 5-island_perimeter.py ├── README.md ├── m.h ├── main.c └── school.c ├── 0x1E-search_algorithms ├── 0-linear.c ├── 1-binary ├── 1-binary.c ├── 100-jump.c ├── 100-main.c ├── 101-O ├── 102-interpolation ├── 102-interpolation.c ├── 102-main.c ├── 103-exponential.c ├── 104-advanced_binary.c ├── 105-jump_list.c ├── 106-linear_skip.c ├── 107-O ├── 108-O ├── 2-O ├── 3-O ├── 4-O ├── 5-O ├── 6-O ├── README.md ├── listint │ ├── create_list.c │ ├── free_list.c │ └── print_list.c ├── search_algos.h ├── skiplist │ ├── create_skiplist.c │ ├── free_skiplist.c │ └── print_skiplist.c └── test │ ├── 0-main.c │ ├── 1-main.c │ ├── 103-main.c │ ├── 104-main.c │ ├── 105-main.c │ └── 106-main.c ├── README.md ├── test └── test.c /0x00-hello_world/0-preprocessor: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -E $CFILE -o c 3 | -------------------------------------------------------------------------------- /0x00-hello_world/1-compiler: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -c $CFILE 3 | -------------------------------------------------------------------------------- /0x00-hello_world/100-intel: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -S -masm=intel $CFILE 3 | -------------------------------------------------------------------------------- /0x00-hello_world/101-quote.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * main - prints exactly "and that piece of art is useful" 6 | * - Dora Korpar, 2015-10-19", 7 | * followed by a new line, to the standard error. 8 | * Return: Always 0 (success) 9 | */ 10 | int main(void) 11 | { 12 | write(2, "and that piece of art is useful\" - Dora Korpar, 2015-10-19\n", 59); 13 | return (1); 14 | } 15 | -------------------------------------------------------------------------------- /0x00-hello_world/2-assembler: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -S $CFILE 3 | -------------------------------------------------------------------------------- /0x00-hello_world/3-name: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc $CFILE -o cisfun 3 | -------------------------------------------------------------------------------- /0x00-hello_world/4-puts.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints the phrase "Programming is like bulding a multilingual puzzle 5 | * Return: 0 if exited properly, non-zero otherwise 6 | */ 7 | int main(void) 8 | { 9 | puts("\"Programming is like building a multilingual puzzle"); 10 | return (0); 11 | } 12 | -------------------------------------------------------------------------------- /0x00-hello_world/5-printf.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * * main - prints the phrase with proper grammar, 5 | * * but the outcome is a piece of art, 6 | * * Return: 0 if exited properly, non-zero otherwise 7 | */ 8 | int main(void) 9 | { 10 | printf("with proper grammar, but the outcome is a piece of art,\n"); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x00-hello_world/6-size.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints the size of various types 5 | * Return: 0 if exited properly, non-zero otherwise 6 | */ 7 | int main(void) 8 | { 9 | printf("Size of a char: %d byte(s)\n", sizeof(char)); 10 | printf("Size of an int: %d byte(s)\n", sizeof(int)); 11 | printf("Size of a long int: %d byte(s)\n", sizeof(long int)); 12 | printf("Size of a long long int: %d byte(s)\n", sizeof(long long int)); 13 | printf("Size of a float: %d byte(s)\n", sizeof(float)); 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x00-hello_world/README.md: -------------------------------------------------------------------------------- 1 | # Welcome to 0x00. C - Hello, World 2 | ### An introduction to C programming 3 | 4 | `0-preprocessor` - Runs a C file through the preprocessor and save the result into another file. 5 | * The C file name will be saved in the variable `$CFILE` 6 | * The output should be saved in the file `c` 7 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/0-positive_or_negative.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /** 6 | * main - assigns a random number to variable in each time 7 | * it executes an prints it 8 | * Return: Always 0 (success) 9 | */ 10 | int main(void) 11 | { 12 | int n; 13 | 14 | srand(time(0)); 15 | n = rand() - RAND_MAX / 2; 16 | if (n > 0) 17 | printf("%d is positive\n", n); 18 | else if (n == 0) 19 | printf("%d is zero\n", n); 20 | else if (n < 0) 21 | printf("%d is negative\n", n); 22 | return (0); 23 | } 24 | 25 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/1-last_digit.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /** 6 | * main - prints the last digit of the number 7 | * stored in the variable n 8 | * Return: Always 0 (success) 9 | */ 10 | int main(void) 11 | { 12 | int n; 13 | 14 | srand(time(0)); 15 | n = rand() - RAND_MAX / 2; 16 | printf("Last digit of %d is %d ", n, n % 10); 17 | if (n % 10 > 5) 18 | printf("and is greater than 5\n"); 19 | else if (n % 10 == 0) 20 | printf("and is 0\n"); 21 | else if (n % 10 < 6 && n % 10 != 0) 22 | printf("and is less than 6 and not 0\n"); 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/100-print_comb3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints all the possible different combinations of two digits 5 | * Return: Always 0 (success) 6 | */ 7 | int main(void) 8 | { 9 | int a, b; 10 | 11 | for (a = 48; a <= 56; a++) 12 | { 13 | for (b = 49; b <= 57; b++) 14 | { 15 | if (b > a) 16 | { 17 | putchar(a); 18 | putchar(b); 19 | if (a != 56 || b != 57) 20 | { 21 | putchar(','); 22 | putchar(' '); 23 | } 24 | } 25 | } 26 | } 27 | putchar('\n'); 28 | return (0); 29 | } 30 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/101-print_comb4.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints all possible different combinations of three digits 5 | * Return: always 0 (success) 6 | */ 7 | int main(void) 8 | { 9 | int a, b, c; 10 | 11 | for (a = 48; a < 58; a++) 12 | { 13 | for (b = 49; b < 58; b++) 14 | { 15 | for (c = 50; c < 58; c++) 16 | { 17 | if (c > b && b > a) 18 | { 19 | putchar(a); 20 | putchar(b); 21 | putchar(c); 22 | if (a != 55 || b != 56) 23 | { 24 | putchar(','); 25 | putchar(' '); 26 | } 27 | } 28 | } 29 | } 30 | } 31 | putchar('\n'); 32 | return (0); 33 | } 34 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/102-print_comb5.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - print all possible combinations of two two-digit numbers 5 | * Return: Always 0 (success) 6 | */ 7 | int main(void) 8 | { 9 | int a, b; 10 | 11 | for (a = 0; a < 100; a++) 12 | { 13 | for (b = 0; b < 100; b++) 14 | { 15 | if (a < b) 16 | { 17 | putchar((a / 10) + 48); 18 | putchar((a % 10) + 48); 19 | putchar(' '); 20 | putchar((b / 10) + 48); 21 | putchar((b % 10) + 48); 22 | if (a != 98 || b != 99) 23 | { 24 | putchar(','); 25 | putchar(' '); 26 | } 27 | } 28 | } 29 | } 30 | putchar('\n'); 31 | return (0); 32 | } 33 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/2-print_alphabet.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints the alphabet in lowercase, 5 | * followed by a newline 6 | * Return: Always 0 (success) 7 | */ 8 | int main(void) 9 | { 10 | char ch; 11 | 12 | for (ch = 'a'; ch <= 'z'; ch++) 13 | { 14 | putchar(ch); 15 | } 16 | putchar('\n'); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/3-print_alphabets.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints the alphabet in lowercase, and then in uppercase, 5 | * followed by a newline 6 | * Return: always 0 (success) 7 | */ 8 | int main(void) 9 | { 10 | int ch; 11 | 12 | for (ch = 'a'; ch <= 'z'; ch++) 13 | putchar(ch); 14 | for (ch = 'A'; ch <= 'Z'; ch++) 15 | putchar(ch); 16 | putchar('\n'); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/4-print_alphabt.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints the alphabet in lowercase, 5 | * then followed by a new line, except q and e 6 | * Return: Always 0 (success) 7 | */ 8 | int main(void) 9 | { 10 | char ch = 'a'; 11 | 12 | while (ch <= 'z') 13 | { 14 | if (ch != 'e' && ch != 'q') 15 | { 16 | putchar(ch); 17 | } 18 | ch++; 19 | } 20 | putchar('\n'); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/5-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints all single digit numbers of base 10 5 | * starting from 0 6 | * followed by a newline. 7 | * Return: Always 0 (success) 8 | */ 9 | int main(void) 10 | { 11 | int n; 12 | 13 | for (n = 0; n < 10; n++) 14 | printf("%d", n); 15 | printf("\n"); 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/6-print_numberz.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints all single digit numbers of base 10 starting from 0, 5 | * followed by a newline, using putchar 6 | * Return: Always 0 (success) 7 | */ 8 | int main(void) 9 | { 10 | int n; 11 | 12 | for (n = 48; n < 58; n++) 13 | { 14 | putchar(n); 15 | } 16 | putchar('\n'); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/7-print_tebahpla.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints the lowercase alphabet in reverse, 5 | * followed by a newline, using putchar 6 | * Return: Always 0 (success) 7 | */ 8 | int main(void) 9 | { 10 | char ch; 11 | 12 | for (ch = 'z'; ch >= 'a'; ch--) 13 | { 14 | putchar(ch); 15 | } 16 | putchar('\n'); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/8-print_base16.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - print all numbers of base 16 in lowercase, 5 | * followed by a newline, using putchar 6 | * Return: Always 0 (success) 7 | */ 8 | int main(void) 9 | { 10 | int n; 11 | char ch; 12 | 13 | for (n = 48; n < 58; n++) 14 | { 15 | putchar(n); 16 | } 17 | for (ch = 'a'; ch <= 'f'; ch++) 18 | { 19 | putchar(ch); 20 | } 21 | putchar('\n'); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/9-print_comb.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - print all possible combinations of single-digit numbers 5 | * Return: Always 0 (success) 6 | */ 7 | int main(void) 8 | { 9 | int n; 10 | 11 | for (n = 48; n < 58; n++) 12 | { 13 | putchar(n); 14 | if (n != 57) 15 | { 16 | putchar(','); 17 | putchar(' '); 18 | } 19 | } 20 | putchar('\n'); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/README.md: -------------------------------------------------------------------------------- 1 | # Welcome to 0x01-variables_if_else_while #2-C 2 | 3 | `0-positive_or_negative.c` - 4 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/0-putchar.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - print _putchar, followed by a newline 5 | * Return: Always 0 (success) 6 | */ 7 | int main(void) 8 | { 9 | _putchar('_'); 10 | _putchar('p'); 11 | _putchar('u'); 12 | _putchar('t'); 13 | _putchar('c'); 14 | _putchar('h'); 15 | _putchar('a'); 16 | _putchar('r'); 17 | _putchar('\n'); 18 | 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/1-alphabet.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_alphabet - prints the alphabet, in lowercase, followed by a newline 5 | * Return: Always 0 (success) 6 | */ 7 | void print_alphabet(void) 8 | { 9 | char ch; 10 | 11 | ch = 'a'; 12 | 13 | while (ch <= 'z') 14 | { 15 | _putchar(ch); 16 | ch++; 17 | } 18 | _putchar('\n'); 19 | } 20 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/10-add.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * add - adds two integers and returns the result. 5 | * @a: int to be added to b 6 | * @b: int to be added to c 7 | * Return: the sum of a and b 8 | */ 9 | int add(int a, int b) 10 | { 11 | int c; 12 | 13 | c = a + b; 14 | 15 | return (c); 16 | } 17 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/100-times_table.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_times_table - prints the n times table, starting from 0 5 | * @n: number of times table 6 | */ 7 | void print_times_table(int n) 8 | { 9 | int a; 10 | int b; 11 | int c; 12 | 13 | if (n >= 0 && n <= 15) 14 | { 15 | for (a = 0; a <= n; a++) 16 | { 17 | for (b = 0; b <= n; b++) 18 | { 19 | c = b * a; 20 | if (b == 0) 21 | { 22 | _putchar(c + '0'); 23 | } else if (c < 10 && b != 0) 24 | { 25 | _putchar(','); 26 | _putchar(' '); 27 | _putchar(' '); 28 | _putchar(' '); 29 | _putchar(c + '0'); 30 | } else if (c >= 10 && c < 100) 31 | { 32 | _putchar(','); 33 | _putchar(' '); 34 | _putchar(' '); 35 | _putchar((c / 10) + '0'); 36 | _putchar((c % 10) + '0'); 37 | } else if (c >= 0) 38 | { 39 | _putchar(','); 40 | _putchar(' '); 41 | _putchar((c / 100) + '0'); 42 | _putchar(((c / 10) % 10) + '0'); 43 | _putchar((c % 10) + '0'); 44 | } 45 | } 46 | _putchar('\n'); 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/101-natural.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - computes and prints the sum of all multiples 5 | * of 3 or 5 below 1024 6 | * Return: Always 0 (success) 7 | */ 8 | int main(void) 9 | { 10 | unsigned long int sum3; 11 | unsigned long int sum5; 12 | unsigned long int sum; 13 | int i; 14 | 15 | sum3 = 0; 16 | sum5 = 0; 17 | sum = 0; 18 | 19 | for (i = 0; i < 1024; ++i) 20 | { 21 | if ((i % 3) == 0) 22 | { 23 | sum3 = sum3 + i; 24 | } else if ((i % 5) == 0) 25 | { 26 | sum5 = sum5 + i; 27 | } 28 | } 29 | sum = sum3 + sum5; 30 | printf("%lu\n", sum); 31 | return (0); 32 | } 33 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/102-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints the first 50 Fibonacci numbers, starting 5 | * with 1 and 2, follwed by a new line. 6 | * Return: Always 0 (success) 7 | */ 8 | int main(void) 9 | { 10 | long int a, b, c, next; 11 | 12 | b = 1; 13 | 14 | c = 2; 15 | 16 | for (a = 1; a <= 50; ++a) 17 | { 18 | if (b != 20365011074) 19 | { 20 | printf("%ld, ", b); 21 | } else 22 | { 23 | printf("%ld\n", b); 24 | } 25 | next = b + c; 26 | b = c; 27 | c = next; 28 | } 29 | return (0); 30 | } 31 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/103-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - finds and prints the sum of even-valued terms 5 | * followed by a new line 6 | * Return: Always 0 (success) 7 | */ 8 | int main(void) 9 | { 10 | int a; 11 | unsigned long int b, c, next, sum; 12 | 13 | b = 1; 14 | c = 2; 15 | sum = 0; 16 | 17 | for (a = 1; a <= 89; ++a) 18 | { 19 | if (b < 4000000 && (b % 2) == 0) 20 | { 21 | sum = sum + b; 22 | } 23 | next = b + c; 24 | b = c; 25 | c = next; 26 | } 27 | printf("%lu\n", sum); 28 | return (0); 29 | } 30 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/104-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - finds and prints the first 98 Fibonacci numbers, 5 | * starting with 1 and 2 6 | * followed by a new line. 7 | * Return: Always 0 (success) 8 | */ 9 | int main(void) 10 | { 11 | unsigned long int a, b, c, b1, b2, c1, c2; 12 | 13 | b = 1; 14 | c = 2; 15 | 16 | printf("%lu", b); 17 | 18 | for (a = 1; a < 91; a++) 19 | { 20 | printf(", %lu", c); 21 | c = c + b; 22 | b = c - b; 23 | } 24 | b1 = b / 1000000000; 25 | b2 = b % 1000000000; 26 | c1 = c / 1000000000; 27 | c2 = c % 1000000000; 28 | 29 | for (a = 92; a < 99; ++a) 30 | { 31 | printf(", %lu", c1 + (c2 / 1000000000)); 32 | printf("%lu", c2 % 1000000000); 33 | c1 = c1 + b1; 34 | b1 = c1 - b1; 35 | c2 = c2 + b2; 36 | b2 = c2 - b2; 37 | } 38 | printf("\n"); 39 | return (0); 40 | } 41 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/11-print_to_98.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * print_to_98 - prints all the natural numbers from n to 98, 6 | * followed by a new line. 7 | * @n: print from this number 8 | */ 9 | void print_to_98(int n) 10 | { 11 | int a; 12 | int b; 13 | 14 | if (n <= 98) 15 | { 16 | for (a = n; a <= 98; a++) 17 | { 18 | if (a != 98) 19 | printf("%d, ", a); 20 | else if (a == 98) 21 | printf("%d\n", a); 22 | } 23 | } else if (n >= 98) 24 | { 25 | for (b = n; b >= 98; b--) 26 | { 27 | if (b != 98) 28 | printf("%d, ", b); 29 | else if (b == 98) 30 | printf("%d\n", b); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/2-print_alphabet_x10.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_alphabet_x10 - prints 10 times the alphabet, in lowercase, 5 | * followed by a new line 6 | */ 7 | void print_alphabet_x10(void) 8 | { 9 | char ch; 10 | int i; 11 | 12 | i = 0; 13 | 14 | while (i < 10) 15 | { 16 | ch = 'a'; 17 | while (ch <= 'z') 18 | { 19 | _putchar(ch); 20 | ch++; 21 | } 22 | _putchar('\n'); 23 | i++; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/3-islower.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _islower - checks for lowercase character 5 | * @c: the character to check 6 | * Return: 1 if c is lowercase, 0 otherwise 7 | */ 8 | int _islower(int c) 9 | { 10 | return (c >= 'a' && c <= 'z'); 11 | } 12 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/4-isalpha.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _isalpha - checks for alphabetic character 4 | * @c: the character to be checked 5 | * Return: 1 if c is a letter, lowercase or uppercase, 6 | * 0 otherwise 7 | */ 8 | int _isalpha(int c) 9 | { 10 | return ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')); 11 | } 12 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/5-sign.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_sign - prints the sign of number 5 | * @n: the int to be checked 6 | * Return: 1 and prints + if n is greater than zero 7 | * 0 and prints 0 if n is zero 8 | * -1 and prints - if n is less than zero 9 | */ 10 | int print_sign(int n) 11 | { 12 | if (n > 0) 13 | { 14 | _putchar('+'); 15 | return (1); 16 | } else if (n == 0) 17 | { 18 | _putchar(48); 19 | return (0); 20 | } else if (n < 0) 21 | { 22 | _putchar('-'); 23 | } 24 | return (-1); 25 | } 26 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/6-abs.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _abs - prints the absolute value of an integer 5 | * @n: the int to be checked 6 | * Return: the absolute value int 7 | */ 8 | int _abs(int n) 9 | { 10 | if (n >= 0) 11 | { 12 | return (n); 13 | } 14 | return (-n); 15 | } 16 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/7-print_last_digit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_last_digit - prints the last digit of a number 5 | * @n: the int to extract the last digit from 6 | * Return: value of the last digit 7 | */ 8 | int print_last_digit(int n) 9 | { 10 | int a; 11 | 12 | if (n < 0) 13 | n = -n; 14 | 15 | a = n % 10; 16 | 17 | if (a < 0) 18 | a = -a; 19 | _putchar(a + '0'); 20 | 21 | return (a); 22 | } 23 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/8-24_hours.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * jack_bauer - prints every minute of the day of Jack Bauer, 5 | * starting from 00:00 to 23:59 6 | */ 7 | void jack_bauer(void) 8 | { 9 | int i; 10 | int j; 11 | 12 | i = 0; 13 | 14 | while (i < 24) 15 | { 16 | j = 0; 17 | while (j < 60) 18 | { 19 | _putchar((i / 10) + '0'); 20 | _putchar((i % 10) + '0'); 21 | _putchar(':'); 22 | _putchar((j / 10) + '0'); 23 | _putchar((j % 10) + '0'); 24 | _putchar('\n'); 25 | j++; 26 | } 27 | i++; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/9-times_table.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * times_table - prints the 9 times table, startin from 0 4 | */ 5 | void times_table(void) 6 | { 7 | int a; 8 | int b; 9 | int c; 10 | 11 | for (a = 0; a < 10; a++) 12 | { 13 | for (b = 0; b < 10; b++) 14 | { 15 | c = b * a; 16 | if (b == 0) 17 | { 18 | _putchar(c + '0'); 19 | } 20 | if (c < 10 && b != 0) 21 | { 22 | _putchar(','); 23 | _putchar(' '); 24 | _putchar(' '); 25 | _putchar(c + '0'); 26 | } else if (c >= 10) 27 | { 28 | _putchar(','); 29 | _putchar(' '); 30 | _putchar((c / 10) + '0'); 31 | _putchar((c % 10) + '0'); 32 | } 33 | } 34 | _putchar('\n'); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/README.md: -------------------------------------------------------------------------------- 1 | # Welcome to 0x02-functions_nested_loops 2 | 3 | `0-putchar.c` - Prints _putchar , followed by a new line. 4 | * The program should return 0 5 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/main.h: -------------------------------------------------------------------------------- 1 | int _putchar(char ch); 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 | int add(int, int); 12 | void print_to_98(int n); 13 | void print_times_table(int n); 14 | -------------------------------------------------------------------------------- /0x03-debugging/0-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - tests function that prints if integer is positive or negative 5 | * Return: 0 6 | */ 7 | 8 | int main(void) 9 | { 10 | int i; 11 | 12 | i = 0; 13 | positive_or_negative(i); 14 | 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x03-debugging/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - causes an infinite loop 5 | * Return: 0 6 | */ 7 | 8 | int main(void) 9 | { 10 | int i; 11 | 12 | printf("Infinite loop incoming :(\n"); 13 | 14 | i = 0; 15 | 16 | /* while (i < 10) */ 17 | /* { */ 18 | /* putchar(i); */ 19 | /* } */ 20 | 21 | printf("Infinite loop avoided! \\o/\n"); 22 | 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x03-debugging/2-largest_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * largest_number - returns the largest of 3 numbers 5 | * @a: first integer 6 | * @b: second integer 7 | * @c: third integer 8 | * Return: largest number 9 | */ 10 | 11 | int largest_number(int a, int b, int c) 12 | { 13 | int largest; 14 | 15 | if (a >= b && a >= c) 16 | { 17 | largest = a; 18 | } 19 | else if (b >= a && b >= c) 20 | { 21 | largest = b; 22 | } 23 | else 24 | { 25 | largest = c; 26 | } 27 | 28 | return (largest); 29 | } 30 | -------------------------------------------------------------------------------- /0x03-debugging/3-main_b.c: -------------------------------------------------------------------------------- 1 | nclude "main.h" 2 | 3 | /** 4 | * main - takes a date and prints how many days are left in the year, taking 5 | * leap years into account 6 | * Return: 0 7 | */ 8 | 9 | int main(void) 10 | { 11 | int month; 12 | int day; 13 | int year; 14 | 15 | month = 2; 16 | day = 29; 17 | year = 2000; 18 | 19 | printf("Date: %02d/%02d/%04d\n", month, day , year); 20 | 21 | day = convert_day(month, day); 22 | 23 | print_remaining_days(month, day, year); 24 | 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /0x03-debugging/3-print_remaining_days.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * * print_remaining_days - takes a date and prints how many days are 6 | * * left in the year, taking leap years into account 7 | * * @month: month in number format 8 | * * @day: day of month 9 | * * @year: year 10 | * * Return: void 11 | */ 12 | 13 | void print_remaining_days(int month, int day, int year) 14 | { 15 | if ((year % 100 == 0 && year % 400 == 0) || (year % 4 == 0)) 16 | { 17 | if (month > 2 && day >= 60) 18 | { 19 | day++; 20 | } 21 | 22 | printf("Day of the year: %d\n", day); 23 | printf("Remaining days: %d\n", 366 - day); 24 | } 25 | else 26 | { 27 | if (month == 2 && day == 60) 28 | { 29 | printf("Invalid date: %02d/%02d/%04d\n", month, day - 31, year); 30 | } 31 | else 32 | { 33 | printf("Day of the year: %d\n", day); 34 | printf("Remaining days: %d\n", 365 - day); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /0x03-debugging/README.md: -------------------------------------------------------------------------------- 1 | # Welcome to the 0x03-debugging directory 2 | -------------------------------------------------------------------------------- /0x03-debugging/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | #include 5 | 6 | void positive_or_negative(int i); 7 | int largest_number(int a, int b, int c); 8 | int convert_day(int month, int day); 9 | void print_remaining_days(int month, int day, int year); 10 | 11 | #endif /* MAIN_H */ 12 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/0-isupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isupper - checks for uppercase character 5 | * @c: the character to be checked 6 | * Return: 1 if c is uppercase, 0 otherwise 7 | */ 8 | int _isupper(int c) 9 | { 10 | if (c >= 'A' && c <= 'Z') 11 | { 12 | return (1); 13 | } 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /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: int to be checked 6 | * Return: 1 if c is a digit, 0 otherwise 7 | */ 8 | int _isdigit(int c) 9 | { 10 | if (c >= '0' && c <= '9') 11 | { 12 | return (1); 13 | } 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/10-print_triangle.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_triangle - prints a triangle, followed by a new line 5 | * @size: size of the triangle 6 | */ 7 | void print_triangle(int size) 8 | { 9 | if (size <= 0) 10 | { 11 | _putchar('\n'); 12 | } else 13 | { 14 | int i, j; 15 | 16 | for (i = 1; i <= size; i++) 17 | { 18 | for (j = i; j < size; j++) 19 | { 20 | _putchar(' '); 21 | } 22 | 23 | for (j = 1; j <= i; j++) 24 | { 25 | _putchar('#'); 26 | } 27 | 28 | _putchar('\n'); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/100-prime_factor.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "math.h" 3 | 4 | /** 5 | * main - finds and prints the largest prime factor of the number 612852475143 6 | * followed by a new line 7 | * Return: Always 0 (Success) 8 | */ 9 | int main(void) 10 | { 11 | long int n; 12 | long int max; 13 | long int i; 14 | 15 | n = 612852475143; 16 | max = -1; 17 | 18 | while (n % 2 == 0) 19 | { 20 | max = 2; 21 | n /= 2; 22 | } 23 | 24 | for (i = 3; i <= sqrt(n); i = i + 2) 25 | { 26 | while (n % i == 0) 27 | { 28 | max = i; 29 | n = n / i; 30 | } 31 | } 32 | 33 | if (n > 2) 34 | max = n; 35 | 36 | printf("%ld\n", max); 37 | 38 | return (0); 39 | } 40 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/101-print_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_number - prints an integer 5 | * @n: integer to be printed 6 | */ 7 | void print_number(int n) 8 | { 9 | unsigned int n1; 10 | 11 | if (n < 0) 12 | { 13 | n1 = -n; 14 | _putchar('-'); 15 | } else 16 | { 17 | n1 = n; 18 | } 19 | 20 | if (n1 / 10) 21 | { 22 | print_number(n1 / 10); 23 | } 24 | 25 | _putchar((n1 % 10) + '0'); 26 | } 27 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/2-mul.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * mul - multiples two integers 5 | * @a: int to be multiplied to b 6 | * @b: int to be multiplied to a 7 | * Return: result 8 | */ 9 | int mul(int a, int b) 10 | { 11 | int c; 12 | 13 | c = a * b; 14 | 15 | return (c); 16 | } 17 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/3-print_numbers: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x04-more_functions_nested_loops/3-print_numbers -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/3-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_numbers - prints the numbers, from 0 to 9, 5 | * followed by a new line. 6 | * Return: Always 0 (success) 7 | */ 8 | void print_numbers(void) 9 | { 10 | int i; 11 | 12 | i = 0; 13 | while (i <= 9) 14 | { 15 | _putchar(i + '0'); 16 | i++; 17 | } 18 | _putchar('\n'); 19 | } 20 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/4-print_most_numbers: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x04-more_functions_nested_loops/4-print_most_numbers -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/4-print_most_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_most_numbers - prints the numbers, from 0 to 9, 5 | * except 2 and 4, followed by a new line 6 | */ 7 | void print_most_numbers(void) 8 | { 9 | int i = 0; 10 | 11 | while (i <= 9) 12 | { 13 | if (i != 2 && i != 4) 14 | { 15 | _putchar(i + '0'); 16 | } 17 | i++; 18 | } 19 | _putchar('\n'); 20 | } 21 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/5-more_numbers: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x04-more_functions_nested_loops/5-more_numbers -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/5-more_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * more_numbers - prints 10 times the numbers, from 0 to 14 5 | * followed by a new line 6 | */ 7 | void more_numbers(void) 8 | { 9 | char n, c; 10 | int i = 0; 11 | 12 | while (i < 10) 13 | { 14 | for (n = 0; n <= 14; n++) 15 | { 16 | c = n; 17 | if (n > 9) 18 | { 19 | _putchar('1'); 20 | c = n % 10; 21 | } 22 | _putchar('0' + c); 23 | } 24 | _putchar('0' + c); 25 | i++; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/6-lines: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x04-more_functions_nested_loops/6-lines -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/6-print_line.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_line - draws a straight line in the terminal 5 | * @n: number of times the character should be printed 6 | */ 7 | void print_line(int n) 8 | { 9 | if (n <= 0) 10 | { 11 | _putchar('\n'); 12 | } else 13 | { 14 | int i; 15 | for (i = 1; i <= n; i++) 16 | { 17 | _putchar('_'); 18 | } 19 | _putchar('\n'); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/7-print_diagonal.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_diagonal - draws a diagonal line on the terminal 5 | * @n: number of times the character \ should be printed 6 | */ 7 | void print_diagonal(int n) 8 | { 9 | if (n <= 0) 10 | { 11 | _putchar('\n'); 12 | } else 13 | { 14 | int i, j; 15 | 16 | for (i = 0; i < n; i++) 17 | { 18 | for (j = 0; j < n; j++) 19 | { 20 | if (j == i) 21 | _putchar('\\'); 22 | else if (j < i) 23 | _putchar(' '); 24 | } 25 | _putchar('\n'); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/8-print_square.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_square - prints a square, followed by a new line; 5 | * @size: size of the square 6 | */ 7 | void print_square(int size) 8 | { 9 | if (size <= 0) 10 | { 11 | _putchar('\n'); 12 | } else 13 | { 14 | int i, j; 15 | 16 | for (i = 0; i < size; i++) 17 | { 18 | for (j = 0; j < size; j++) 19 | { 20 | _putchar('#'); 21 | } 22 | _putchar('\n'); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/9-fizz_buzz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x04-more_functions_nested_loops/9-fizz_buzz -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/9-fizz_buzz.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - prints the numbers from 1 to 100, followed by a new line 6 | * but for multiples of three prints Fizz instead of the number 7 | * and for the multiples of five prints Buzz 8 | * Return: Always 0 (Success) 9 | */ 10 | int main(void) 11 | { 12 | int i; 13 | 14 | for (i = 1; i <= 100; i++) 15 | { 16 | if (i % 3 == 0 && i % 5 != 0) 17 | { 18 | printf(" Fizz"); 19 | } else if (i % 5 == 0 && i % 3 != 0) 20 | { 21 | printf(" Buzz"); 22 | } else if (i % 3 == 0 && i % 5 == 0) 23 | { 24 | printf(" FizzBuzz"); 25 | } else if (i == 1) 26 | { 27 | printf("%d", i); 28 | } else 29 | { 30 | printf(" %d", i); 31 | } 32 | } 33 | printf("\n"); 34 | 35 | return (0); 36 | } 37 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/README.md: -------------------------------------------------------------------------------- 1 | More functions nested loops 2 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | 4 | 5 | /** 6 | * 7 | * * main - check the code 8 | * 9 | * * 10 | * 11 | * * Return: Always 0. 12 | * 13 | * */ 14 | 15 | int main(void) 16 | 17 | { 18 | 19 | print_number(98); 20 | 21 | _putchar('\n'); 22 | 23 | print_number(402); 24 | 25 | _putchar('\n'); 26 | 27 | print_number(1024); 28 | 29 | _putchar('\n'); 30 | 31 | print_number(0); 32 | 33 | _putchar('\n'); 34 | 35 | print_number(-98); 36 | 37 | _putchar('\n'); 38 | 39 | return (0); 40 | 41 | } 42 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | #include 5 | 6 | int _isupper(int c); 7 | int _isdigit(int c); 8 | int mul(int a, int b); 9 | void print_numbers(void); 10 | void print_most_numbers(void); 11 | int _putchar(char c); 12 | void more_numbers(void); 13 | void print_line(int n); 14 | void print_diagonal(int n); 15 | void print_square(int size); 16 | void print_triangle(int size); 17 | void print_number(int n); 18 | 19 | #endif /*MAIN_H*/ 20 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/0-reset_to_98.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * reset_to_98 - takes a pointer to an int as parameter and 5 | * updates the value it points to to 98. 6 | * @n: pointer to the variable to update 7 | */ 8 | void reset_to_98(int *n) 9 | { 10 | *n = 98; 11 | } 12 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/1-swap.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * swap_int - swaps the values of two integers 5 | * @a: integer to be swapped 6 | * @b: integer to be swapped 7 | */ 8 | void swap_int(int *a, int *b) 9 | { 10 | int c; 11 | 12 | c = *a; 13 | *a = *b; 14 | *b = c; 15 | } 16 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/100-atoi.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _atoi - convert a string into an integer. 5 | * @s: string to be converted 6 | * Return: integer converted from the string 7 | */ 8 | int _atoi(char *s) 9 | { 10 | int sign = 1; 11 | unsigned int num = 0; 12 | 13 | do { 14 | if (*s == '-') 15 | sign *= -1; 16 | 17 | else if (*s >= '0' && *s <= '9') 18 | num = (num * 10) + (*s - '0'); 19 | 20 | else if (num > 0) 21 | break; 22 | } while (*s++); 23 | 24 | return (num * sign); 25 | } 26 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/101-keygen.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /** 6 | * main - program that generates random valid passwords 7 | * for the program 101-crackme 8 | * Return: Always 0 (success) 9 | */ 10 | int main(void) 11 | { 12 | int sum; 13 | char c; 14 | 15 | srand(time(NULL)); 16 | while (sum <= 2645) 17 | { 18 | c = rand() % 128; 19 | sum += c; 20 | putchar(c); 21 | } 22 | putchar(2772 - sum); 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/2-strlen.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strlen - returns the length of a string 5 | * @s: string length to be returned 6 | * Return: the length of the string 7 | */ 8 | int _strlen(char *s) 9 | { 10 | int a; 11 | 12 | a = 0; 13 | 14 | while (s[a] != '\0') 15 | { 16 | a++; 17 | } 18 | return (a); 19 | } 20 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/3-puts.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _puts - prints a string, followed by a new line, to stdout 5 | * @str: pointer to the string to print 6 | */ 7 | void _puts(char *str) 8 | { 9 | int a; 10 | 11 | for (a = 0; str[a] != '\0'; a++) 12 | { 13 | _putchar(str[a]); 14 | } 15 | _putchar('\n'); 16 | } 17 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/4-print_rev.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_rev - prints a string, in reverse, followed by a new line 5 | * @s: string to be printed 6 | */ 7 | void print_rev(char *s) 8 | { 9 | int a, b, len; 10 | 11 | a = 0; 12 | 13 | while (s[a] != '\0') 14 | { 15 | a++; 16 | } 17 | 18 | len = a; 19 | 20 | for (b = len - 1; b >= 0; b--) 21 | { 22 | _putchar(s[b]); 23 | } 24 | 25 | _putchar('\n'); 26 | } 27 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/5-rev_string.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * rev_string - reverses a string 5 | * @s: the string to be reversed 6 | */ 7 | void rev_string(char *s) 8 | { 9 | char ch; 10 | int a, b, c; 11 | 12 | a = 0; 13 | b = 0; 14 | 15 | while (s[b] != '\0') 16 | { 17 | b++; 18 | } 19 | c = b - 1; 20 | 21 | for (a = 0; a < b / 2; a++) 22 | { 23 | ch = s[a]; 24 | s[a] = s[c]; 25 | s[c--] = ch; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/6-puts2.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * puts2 - prints every other character of a string, starting with the first 5 | * character, followed by a new line. 6 | * @str: string to print characters from 7 | */ 8 | void puts2(char *str) 9 | { 10 | int a, b; 11 | 12 | a = 0; 13 | 14 | while (str[a] != '\0') 15 | { 16 | a++; 17 | } 18 | 19 | for (b = 0; b < a; b += 2) 20 | { 21 | _putchar(str[b]); 22 | } 23 | _putchar('\n'); 24 | } 25 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/7-puts_half.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * puts_half - prints half of a string, followed by a new line. 5 | * @str: string to be printed 6 | */ 7 | void puts_half(char *str) 8 | { 9 | int a, b, c; 10 | 11 | a = 0; 12 | 13 | while (str[a] != '\0') 14 | { 15 | a++; 16 | } 17 | 18 | if (a % 2 == 0) 19 | { 20 | for (c = a / 2; str[c] != '\0'; c++) 21 | { 22 | _putchar(str[c]); 23 | } 24 | } else if (a % 2) 25 | { 26 | for (b = (a - 1) / 2; b < a - 1; b++) 27 | { 28 | _putchar(str[b + 1]); 29 | } 30 | } 31 | _putchar('\n'); 32 | } 33 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/8-print_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * print_array - prints n elements of an array of integers, 5 | * followed by a new line. 6 | * @a: array to be printed 7 | * @n: number of elements to print 8 | */ 9 | void print_array(int *a, int n) 10 | { 11 | int i; 12 | 13 | for (i = 0; i < n; i++) 14 | { 15 | if (i == 0) 16 | printf("%d", a[i]); 17 | else 18 | printf(", %d", a[i]); 19 | } 20 | printf("\n"); 21 | } 22 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/9-strcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * *_strcpy - copies the string pointed to by src 5 | * including the terminating null byte (\0) 6 | * to the buffer pointed to by dest 7 | * @dest: pointer to the buffer in which we copy the string 8 | * @src: str to be copied 9 | * Return: the pointer to the dest 10 | */ 11 | char *_strcpy(char *dest, char *src) 12 | { 13 | 14 | int len, i; 15 | 16 | len = 0; 17 | 18 | while (src[len] != '\0') 19 | { 20 | len++; 21 | } 22 | 23 | for (i = 0; i < len; i++) 24 | { 25 | dest[i] = src[i]; 26 | } 27 | dest[i] = '\0'; 28 | 29 | return (dest); 30 | } 31 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | void reset_to_98(int *n); 5 | void swap_int(int *a, int *b); 6 | int _strlen(char *s); 7 | int _putchar(char c); 8 | void _puts(char *str); 9 | void print_rev(char *s); 10 | void rev_string(char *s); 11 | void puts2(char *str); 12 | void puts_half(char *str); 13 | void print_array(int *a, int n); 14 | char *_strcpy(char *dest, char *src); 15 | int _atoi(char *s); 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/0-strcat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x06-pointers_arrays_strings/0-strcat -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/0-strcat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * *_strcat - concatenates two strings. 5 | * @src: string to be appended 6 | * @dest: string to be appended to 7 | * Return: a pointer of the resulting string 8 | */ 9 | char *_strcat(char *dest, char *src) 10 | { 11 | int i = 0; 12 | int dl = 0; 13 | 14 | while (dest[i++]) 15 | dl++; 16 | for (i = 0; src[i]; i++) 17 | dest[dl++] = src[i]; 18 | return (dest); 19 | } 20 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/1-strncat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x06-pointers_arrays_strings/1-strncat -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/1-strncat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strncat - concatenate two strings 5 | * @dest: The destination value 6 | * @src: THe source value 7 | * @n: The limit of the concatenation 8 | * Return: A pointer to the resulting string dest 9 | */ 10 | char *_strncat(char *dest, char *src, int n) 11 | { 12 | int i = 0, dest_len = 0; 13 | 14 | while (dest[i++]) 15 | dest_len++; 16 | for (i = 0; src[i] && i < n; i++) 17 | dest[dest_len++] = src[i]; 18 | return (dest); 19 | } 20 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/100-rot13.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * rot13 - encodes a string in rot13 5 | * @s: string to be encoded 6 | * 7 | * Return: the resulting string 8 | */ 9 | char *rot13(char *s) 10 | { 11 | int i, j; 12 | 13 | char a[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; 14 | char b[] = "NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm"; 15 | 16 | for (i = 0; s[i] != '\0'; i++) 17 | { 18 | for (j = 0; a[j] != '\0'; j++) 19 | { 20 | if (s[i] == a[j]) 21 | { 22 | s[i] = b[j]; 23 | break; 24 | } 25 | } 26 | } 27 | 28 | return (s); 29 | } 30 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/101-print_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_number - prints an integer; 5 | * @n: integer to be printed; 6 | */ 7 | void print_number(int n) 8 | { 9 | unsigned int n1; 10 | 11 | if (n < 0) 12 | { 13 | n1 = -n; 14 | _putchar('-'); 15 | } else 16 | { 17 | n1 = n; 18 | } 19 | 20 | if (n1 / 10) 21 | print_number(n1 / 10); 22 | 23 | _putchar((n1 % 10) + '0'); 24 | } 25 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/101-print_numbers: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x06-pointers_arrays_strings/101-print_numbers -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/102-magic.c: -------------------------------------------------------------------------------- 1 | #include 2 | /* 3 | * main - prints the answer followed by a new line 4 | * Return: 0 Always success 5 | */ 6 | int main(void) 7 | { 8 | int n; 9 | int a[5]; 10 | int *p; 11 | 12 | a[2] = 1024; 13 | p = &n; 14 | /* 15 | * * write your line of code here... 16 | * * Remember: 17 | * * - you are not allowed to use a 18 | * * - you are not allowed to modify p 19 | * * - only one statement 20 | * * - you are not allowed to code anything else than this line of code 21 | * * 22 | */ 23 | *(p + 5) = 98; 24 | /* ...so that this prints 98\n */ 25 | printf("a[2] = %d\n", a[2]); 26 | return (0); 27 | } 28 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/103-add: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x06-pointers_arrays_strings/103-add -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/104-buffer: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x06-pointers_arrays_strings/104-buffer -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/104-print_buffer.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * print_line - prints a s bytes of a buffer 6 | * @c: buffer to print 7 | * @s: bytes of buffer to print 8 | * @l: line of buffer to print 9 | * Return: void 10 | */ 11 | void print_line(char *c, int s, int l) 12 | { 13 | int j, k; 14 | 15 | for (j = 0; j <= 9; j++) 16 | { 17 | if (j <= s) 18 | printf("%02x", c[l * 10 + j]); 19 | else 20 | printf(" "); 21 | if (j % 2) 22 | putchar(' '); 23 | } 24 | for (k = 0; k <= s; k++) 25 | { 26 | if (c[l * 10 + k] > 31 && c[l * 10 + k] < 127) 27 | putchar(c[l * 10 + k]); 28 | else 29 | putchar('.'); 30 | } 31 | } 32 | 33 | /** 34 | * print_buffer - prints a buffer 35 | * @b: buffer to print 36 | * @size: size of buffer 37 | * Return: void 38 | */ 39 | void print_buffer(char *b, int size) 40 | { 41 | int i; 42 | 43 | for (i = 0; i <= (size - 1) / 10 && size; i++) 44 | { 45 | printf("%08x: ", i * 10); 46 | if (i < size / 10) 47 | { 48 | print_line(b, 9, i); 49 | } 50 | else 51 | { 52 | print_line(b, size % 10 - 1, i); 53 | } 54 | putchar('\n'); 55 | } 56 | if (size == 0) 57 | putchar('\n'); 58 | } 59 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/2-strncpy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x06-pointers_arrays_strings/2-strncpy -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/2-strncpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strncpy - copies a string 5 | * @dest: destination string 6 | * @src: source of string 7 | * @n: number of bytes to copy 8 | * Return: Pointer to the resulting string 9 | */ 10 | char *_strncpy(char *dest, char *src, int n) 11 | { 12 | int i = 0; 13 | int dl = 0; 14 | 15 | while (src[i++]) 16 | dl++; 17 | 18 | for (i = 0; src[i] && i < n; i++) 19 | dest[i] = src[i]; 20 | 21 | for (i = dl; i < n; i++) 22 | dest[i] = '\0'; 23 | 24 | return (dest); 25 | } 26 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/3-strcmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x06-pointers_arrays_strings/3-strcmp -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/3-strcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strcmp - compares two strings 5 | * @s1: first string 6 | * @s2: second string 7 | * Return: less than 0 if s1 is less than s2, 0 if they're equal, 8 | * more than 0 if s1 is greater than s2 9 | */ 10 | int _strcmp(char *s1, char *s2) 11 | { 12 | while (*s1 == *s2) 13 | { 14 | if (*s1 == '\0') 15 | { 16 | return (0); 17 | } 18 | s1++; 19 | s2++; 20 | } 21 | return (*s1 - *s2); 22 | } 23 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/4-rev_array: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x06-pointers_arrays_strings/4-rev_array -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/4-rev_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * reverse_array - reverses the content of an array of integers 5 | * @a: array to be reversed 6 | * @n: number of elements in an array 7 | */ 8 | void reverse_array(int *a, int n) 9 | { 10 | int i; 11 | int dl; 12 | int tmp; 13 | 14 | dl = n - 1; 15 | 16 | for (i = 0; i < n / 2; i++) 17 | { 18 | tmp = a[i]; 19 | a[i] = a[dl]; 20 | a[dl--] = tmp; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/5-string_toupper: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x06-pointers_arrays_strings/5-string_toupper -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/5-string_toupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * string_toupper - changes all lowercase letters of a string 5 | * to uppercase 6 | * @s: string to be modified 7 | * Return: the resulting string 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] = s[i] - 32; 17 | } 18 | 19 | return (s); 20 | } 21 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/6-cap: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x06-pointers_arrays_strings/6-cap -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/6-cap_string.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * cap_string - capitalizes all words of a string. 5 | * @str: string to be modified 6 | * Return: the resulting string 7 | */ 8 | char *cap_string(char *str) 9 | { 10 | int index = 0; 11 | 12 | while (str[index]) 13 | { 14 | while (!(str[index] >= 'a' && str[index] <= 'z')) 15 | index++; 16 | 17 | if (str[index - 1] == ' ' || 18 | str[index - 1] == '\t' || 19 | str[index - 1] == '\n' || 20 | str[index - 1] == ',' || 21 | str[index - 1] == ';' || 22 | str[index - 1] == '.' || 23 | str[index - 1] == '!' || 24 | str[index - 1] == '?' || 25 | str[index - 1] == '"' || 26 | str[index - 1] == '(' || 27 | str[index - 1] == ')' || 28 | str[index - 1] == '{' || 29 | str[index - 1] == '}' || 30 | index == 0) 31 | str[index] -= 32; 32 | 33 | index++; 34 | } 35 | 36 | return (str); 37 | } 38 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/7-1337: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x06-pointers_arrays_strings/7-1337 -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/7-leet.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * leet - encodes a string into 1337. 5 | * @str: string to be encoded 6 | * Return: a pointer to the encoded string 7 | */ 8 | char *leet(char *str) 9 | { 10 | int indx1 = 0, indx2; 11 | char leet[8] = {'O', 'L', '?', 'E', 'A', '?', '?', 'T'}; 12 | 13 | while (str[indx1]) 14 | { 15 | for (indx2 = 0; indx2 <= 7; indx2++) 16 | { 17 | if (str[indx1] == leet[indx2] || 18 | str[indx1] - 32 == leet[indx2]) 19 | str[indx1] = indx2 + '0'; 20 | } 21 | 22 | indx1++; 23 | } 24 | 25 | return (str); 26 | } 27 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | #include 4 | 5 | 6 | 7 | /** 8 | * 9 | * * main - check the code 10 | * 11 | * * 12 | * 13 | * * Return: Always 0. 14 | * 15 | * */ 16 | 17 | int main(void) 18 | 19 | { 20 | 21 | char buffer[] = "This is a string!\0And this is the rest of the #buffer :)\1\2\3\4\5\6\7#cisfun\n\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\x20\x21\x34\x56#pointersarefun #infernumisfun\n"; 22 | 23 | 24 | 25 | printf("%s\n", buffer); 26 | 27 | printf("---------------------------------\n"); 28 | 29 | print_buffer(buffer, sizeof(buffer)); 30 | 31 | return (0); 32 | 33 | } 34 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | int _putchar(char c); 5 | char *_strcat(char *dest, char *src); 6 | char *_strncat(char *dest, char *src, int n); 7 | char *_strncpy(char *dest, char *src, int n); 8 | int _strcmp(char *s1, char *s2); 9 | void reverse_array(int *a, int n); 10 | char *string_toupper(char *); 11 | char *cap_string(char *str); 12 | char *leet(char *str); 13 | char *rot13(char *); 14 | void print_number(int n); 15 | char *infinite_add(char *n1, char *n2, char *r, int size_r); 16 | void print_buffer(char *b, int size); 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/0-memset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x07-pointers_arrays_strings/0-memset -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/0-memset.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _memset - fills memory with a constant byte 5 | * @s: memory area to be filled 6 | * @b: constant byte 7 | * @n: number of bytes 8 | * Return: the pointer to the memory area s 9 | */ 10 | char *_memset(char *s, char b, unsigned int n) 11 | { 12 | unsigned int i; 13 | 14 | for (i = 0; i < n; i++) 15 | { 16 | s[i] = b; 17 | } 18 | return (s); 19 | } 20 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/1-memcpy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x07-pointers_arrays_strings/1-memcpy -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/1-memcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _memcpy - copies memory area. 5 | * @src: memory area copied from 6 | * @dest: memory are copied to 7 | * @n: number of bytes 8 | * Return: pointer to dest 9 | */ 10 | char *_memcpy(char *dest, char *src, unsigned int n) 11 | { 12 | unsigned int i; 13 | 14 | for (i = 0; i < n; i++) 15 | { 16 | dest[i] = src[i]; 17 | } 18 | return (dest); 19 | } 20 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/100-set_string: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x07-pointers_arrays_strings/100-set_string -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/100-set_string.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * set_string - sets the value of a pointer to a char. 5 | * @s: pointer to pointer we need to set to 6 | * @to: string to set 7 | */ 8 | void set_string(char **s, char *to) 9 | { 10 | *s = to; 11 | } 12 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/101-crackme_password: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x07-pointers_arrays_strings/101-crackme_password -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/2-strchr: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x07-pointers_arrays_strings/2-strchr -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/2-strchr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _strchr - locate a character in a string 6 | * @s: string to check 7 | * @c: character to locate 8 | * Return: a pointer to the first occurrence of the character c 9 | * in the string s, or NULL if the character is not found 10 | */ 11 | char *_strchr(char *s, char c) 12 | { 13 | int a; 14 | 15 | while (1) 16 | { 17 | 18 | a = *s++; 19 | if (a == c) 20 | { 21 | return (s - 1); 22 | } 23 | if (a == 0) 24 | { 25 | return (NULL); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/3-strspn: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x07-pointers_arrays_strings/3-strspn -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/3-strspn.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strspn - gets the length of a preix substring 5 | * @s: string to evaluate 6 | * @accept: string containing the list of characters to match in s 7 | * Return: the number of bytes in the initial segment of 8 | * s which consist only of bytes from accept 9 | */ 10 | unsigned int _strspn(char *s, char *accept) 11 | { 12 | unsigned int i = 0; 13 | int j; 14 | 15 | 16 | while (*s) 17 | { 18 | for (j = 0; accept[j]; j++) 19 | { 20 | if (accept[j] == *s) 21 | { 22 | i++; 23 | break; 24 | } 25 | 26 | else if ((accept[j + 1]) == '\0') 27 | return (i); 28 | } 29 | s++; 30 | } 31 | 32 | return (i); 33 | } 34 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/4-strpbrk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x07-pointers_arrays_strings/4-strpbrk -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/4-strpbrk.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _strpbrk - searches a string for any of a set of bytes 6 | * @s: string to be searched 7 | * @accept: string containing the bytes 8 | * Return: a pointer to the byte in s that matches one of 9 | * the bytes in accept, or NULL if no such byte is found 10 | */ 11 | char *_strpbrk(char *s, char *accept) 12 | { 13 | int i; 14 | 15 | while (*s) 16 | { 17 | for (i = 0; accept[i]; i++) 18 | { 19 | if (accept[i] == *s) 20 | return (s); 21 | } 22 | s++; 23 | } 24 | return ('\0'); 25 | } 26 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/5-strstr: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x07-pointers_arrays_strings/5-strstr -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/5-strstr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _strstr - locates a substring 6 | * @haystack: string to search in 7 | * @needle: substring to be located 8 | * Return: a pointer to the beginning of the located 9 | * substring, or NULL if the substring is not found. 10 | */ 11 | char *_strstr(char *haystack, char *needle) 12 | { 13 | int i; 14 | 15 | if (*needle == 0) 16 | return (haystack); 17 | while (*haystack) 18 | { 19 | i = 0; 20 | 21 | if (haystack[i] == needle[i]) 22 | { 23 | do { 24 | if (needle[i + 1] == '\0') 25 | return (haystack); 26 | i++; 27 | } while (haystack[i] == needle[i]); 28 | } 29 | haystack++; 30 | } 31 | return ('\0'); 32 | } 33 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/7-print_chessboard: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x07-pointers_arrays_strings/7-print_chessboard -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/7-print_chessboard.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_chessboard - prints a chessboard 5 | * @a: board to print 6 | */ 7 | void print_chessboard(char (*a)[8]) 8 | { 9 | int i; 10 | int j; 11 | 12 | for (i = 0; i < 8; i++) 13 | { 14 | for (j = 0; j < 8; j++) 15 | { 16 | _putchar(a[i][j]); 17 | } 18 | _putchar('\n'); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/8-print_diagsums: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x07-pointers_arrays_strings/8-print_diagsums -------------------------------------------------------------------------------- /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: square matrix of which we print the sum of diagonals 8 | * @size: size of the matrix 9 | */ 10 | void print_diagsums(int *a, int size) 11 | { 12 | int i; 13 | 14 | unsigned int sum1, sum2; 15 | 16 | sum1 = 0; 17 | sum2 = 0; 18 | 19 | for (i = 0; i < size; i++) 20 | { 21 | sum1 += a[(size * i) + i]; 22 | sum2 += a[(size * (i + 1)) - (i + 1)]; 23 | } 24 | 25 | printf("%d, %d\n", sum1, sum2); 26 | } 27 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | # Welcome to 0x07-pointers_arrays_strings 2 | 3 | 4 | `File 0-memset.c` is a function that fills memory with a constant byte. 5 | 6 | 7 | `File 1-memcpy.c` is a function that copies memory area. 8 | 9 | 10 | `File 2-strchr.c` is a function that locates a character in a string. 11 | 12 | 13 | `FILE 3-strspn.c` is a function that gets the length of a prefix substring. 14 | 15 | 16 | `FILE 4-strpbrk.c` is a function that seraches a string for any of a set of bytes. 17 | 18 | 19 | `FILE 5-strstr.c` is a function that locates a substring. 20 | 21 | 22 | `FILE 7-print_chessboard.c` is a function that prints a chessboard. 23 | 24 | 25 | `File _putchar` is a function that replaces the standard library function putchar(). 26 | 27 | 28 | `FILE 8-print_diagsums.c` is a function that prints the sum of the two diagonals of a square matrix of integers. 29 | 30 | 31 | `FILE 100-set_string.c` is a function taht sets the value of a pointer to a char. 32 | 33 | 34 | `FILE 101-crackme_password` contains the password for the crackme2 executable. 35 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _putchar - writes the character of c to stdout 5 | * @c: the character to print 6 | * Return: On success 1 7 | * On error, -1 is returned , and erroris set appropriately 8 | */ 9 | int _putchar(char c) 10 | { 11 | return (write(1, &c, 1)); 12 | } 13 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/crackme2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x07-pointers_arrays_strings/crackme2 -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | #include 4 | 5 | 6 | 7 | /** 8 | * 9 | * * main - check the code 10 | * 11 | * * 12 | * 13 | * * Return: Always 0. 14 | * 15 | * */ 16 | 17 | int main(void) 18 | 19 | { 20 | 21 | char *s0 = "Bob Dylan"; 22 | 23 | char *s1 = "Robert Allen"; 24 | 25 | 26 | 27 | printf("%s, %s\n", s0, s1); 28 | 29 | set_string(&s1, s0); 30 | 31 | printf("%s, %s\n", s0, s1); 32 | 33 | return (0); 34 | 35 | } 36 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | char *_memset(char *s, char b, unsigned int n); 5 | char *_memcpy(char *dest, char *src, unsigned int n); 6 | char *_strchr(char *s, char c); 7 | unsigned int _strspn( char *s, char *accept); 8 | char *_strpbrk(char *s, char *accept); 9 | char *_strstr(char *haystack, char *needle); 10 | void print_chessboard(char(*a)[8]); 11 | int _putchar(char c); 12 | void print_diagsums(int *a, int size); 13 | void set_string(char **s, char *to); 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /0x08-recursion/0-puts_recursion: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x08-recursion/0-puts_recursion -------------------------------------------------------------------------------- /0x08-recursion/0-puts_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _puts_recursion - prints a string, followed by a new line 5 | * @s: string to print 6 | */ 7 | void _puts_recursion(char *s) 8 | { 9 | if (*s == '\0') 10 | { 11 | _putchar('\n'); 12 | return; 13 | } 14 | _putchar(*s); 15 | _puts_recursion(s + 1); 16 | } 17 | -------------------------------------------------------------------------------- /0x08-recursion/1-print_rev_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _print_rev_recursion - prints a string in reverse 5 | * @s: string to print 6 | */ 7 | void _print_rev_recursion(char *s) 8 | { 9 | if (*s) 10 | { 11 | _print_rev_recursion(s + 1); 12 | _putchar(*s); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /0x08-recursion/100-is_palindrome.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | int check_pal(char *s, int i, int len); 4 | int _strlen_recursion(char *s); 5 | 6 | /** 7 | * is_palindrome - checks if a string is a palindrome 8 | * @s: string to reverse 9 | * Return: 1 if it is, 0 it's not 10 | */ 11 | int is_palindrome(char *s) 12 | { 13 | if (*s == 0) 14 | return (1); 15 | return (check_pal(s, 0, _strlen_recursion(s))); 16 | } 17 | 18 | /** 19 | * _strlen_recursion - returns the length of a string 20 | * @s: string to calculate the length of 21 | * Return: length of the string 22 | */ 23 | int _strlen_recursion(char *s) 24 | { 25 | if (*s == '\0') 26 | return (0); 27 | return (1 + _strlen_recursion(s + 1)); 28 | } 29 | 30 | /** 31 | * check_pal - checks the characters recursively for palindrome 32 | * @s: string to check 33 | * @i: iterator 34 | * @len: length of the string 35 | * Return: 1 if palindrome, 0 if not 36 | */ 37 | int check_pal(char *s, int i, int len) 38 | { 39 | if (*(s + i) != *(s + len - 1)) 40 | return (0); 41 | if (i >= len) 42 | return (1); 43 | return (check_pal(s, i + 1, len - 1)); 44 | } 45 | -------------------------------------------------------------------------------- /0x08-recursion/100-palindrome: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x08-recursion/100-palindrome -------------------------------------------------------------------------------- /0x08-recursion/101-wildcmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x08-recursion/101-wildcmp -------------------------------------------------------------------------------- /0x08-recursion/2-strlen_recursion: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x08-recursion/2-strlen_recursion -------------------------------------------------------------------------------- /0x08-recursion/2-strlen_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strlen_recursion - returns the length of a string 5 | * @s: string to calculate the length of 6 | * Return: length of the string 7 | */ 8 | int _strlen_recursion(char *s) 9 | { 10 | if (*s == '\0') 11 | return (0); 12 | return (1 + _strlen_recursion(s + 1)); 13 | } 14 | -------------------------------------------------------------------------------- /0x08-recursion/3-factorial: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x08-recursion/3-factorial -------------------------------------------------------------------------------- /0x08-recursion/3-factorial.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * factorial - returns the factorial of a number 5 | * @n: number to return the factorial from 6 | * Return: factorial of n 7 | */ 8 | int factorial(int n) 9 | { 10 | if (n < 0) 11 | return (-1); 12 | if (n == 0) 13 | return (1); 14 | return (n * factorial(n - 1)); 15 | } 16 | -------------------------------------------------------------------------------- /0x08-recursion/4-pow: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x08-recursion/4-pow -------------------------------------------------------------------------------- /0x08-recursion/4-pow_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _pow_recursion - returns the value of x raised to the power of y 5 | * @x: value to raise 6 | * @y: power 7 | * Return: result of the power 8 | */ 9 | int _pow_recursion(int x, int y) 10 | { 11 | if (y < 0) 12 | return (-1); 13 | if (y == 0) 14 | return (1); 15 | return (x * _pow_recursion(x, y - 1)); 16 | } 17 | -------------------------------------------------------------------------------- /0x08-recursion/5-sqrt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x08-recursion/5-sqrt -------------------------------------------------------------------------------- /0x08-recursion/5-sqrt_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | int actual_sqrt_recursion(int n, int i); 4 | 5 | /** 6 | * _sqrt_recursion - returns the natural square root of a number 7 | * @n: number to calculate the square root of 8 | * Return: the resulting square root 9 | */ 10 | int _sqrt_recursion(int n) 11 | { 12 | if (n < 0) 13 | return (-1); 14 | return (actual_sqrt_recursion(n, 0)); 15 | } 16 | 17 | /** 18 | * actual_sqrt_recursion - recurses to find the natural 19 | * square root of a number 20 | * @n: number to calculate the sqaure root of 21 | * @i: iterator 22 | * Return: the resulting square root 23 | */ 24 | int actual_sqrt_recursion(int n, int i) 25 | { 26 | if (i * i > n) 27 | return (-1); 28 | if (i * i == n) 29 | return (i); 30 | return (actual_sqrt_recursion(n, i + 1)); 31 | } 32 | -------------------------------------------------------------------------------- /0x08-recursion/6-is_prime_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | int actual_prime(int n, int i); 4 | 5 | /** 6 | * is_prime_number - says if an integer is a prime number or not 7 | * @n: number to evaluate 8 | * Return: 1 if n is a prime number, 0 if not 9 | */ 10 | int is_prime_number(int n) 11 | { 12 | if (n <= 1) 13 | return (0); 14 | return (actual_prime(n, n - 1)); 15 | } 16 | 17 | /** 18 | * actual_prime - calculates if a number is prime recursively 19 | * @n: number to evaluate 20 | * @i: iterator 21 | * Return: 1 if n is prime, 0 if not 22 | */ 23 | int actual_prime(int n, int i) 24 | { 25 | if (i == 1) 26 | return (1); 27 | if (n % i == 0 && i > 0) 28 | return (0); 29 | return (actual_prime(n, i - 1)); 30 | } 31 | -------------------------------------------------------------------------------- /0x08-recursion/README.md: -------------------------------------------------------------------------------- 1 | # Welcome to 0x08-recursion 2 | 3 | `FILE 0-puts_recursion.c` is a function that prints a string. 4 | 5 | `FILE 1-print_rev_recursion.c` is a function that prints a string in reverse. 6 | 7 | `FILE 2-strlen_recursion.c` is a function that returns the length of a string. 8 | 9 | `FILE 3-factorial.c` is a function that returns the factorial of a given number. 10 | 11 | `FILE 4-pow_recursion.c` is function that returns the value of x raised to the power of y. 12 | 13 | `FILE 5-sqrt_recursion.c` is a function that returns the natural square root of a number. 14 | 15 | `FILE 6-is_prime_number.c` is a function that returns 1 if the input integer is a prime number. 16 | 17 | `FILE 100-is_palindrome.c` is a function that returns 1 if a string is a palindrome and 0 if not. 18 | 19 | `FILE 101-wildcmp.c` is a function that compares two strings and returns 1 if the strings can be considered identical, otherwise return 0. 20 | -------------------------------------------------------------------------------- /0x08-recursion/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _putchar - writes the character c to stdout 5 | * @c: The character to print 6 | * Return: On success 1. 7 | * On error, -1 is returned, and errno is set appropriately. 8 | */ 9 | int _putchar(char c) 10 | { 11 | return (write(1, &c, 1)); 12 | } 13 | -------------------------------------------------------------------------------- /0x08-recursion/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | void _puts_recursion(char *s); 5 | int _putchar(char c); 6 | void _print_rev_recursion(char *s); 7 | int _strlen_recursion(char *s); 8 | int factorial(int n); 9 | int _pow_recursion(int x, int y); 10 | int _sqrt_recursion(int n); 11 | int is_prime_number(int n); 12 | int is_palindrome(char *s); 13 | int wildcmp(char *s1, char *s2); 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /0x09-static_libraries/0-isupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isupper - checks for uppercase character 5 | * @c: the character to be checked 6 | * Return: 1 if c is uppercase, 0 otherwise 7 | */ 8 | int _isupper(int c) 9 | { 10 | if (c >= 'A' && c <= 'Z') 11 | { 12 | return (1); 13 | } 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x09-static_libraries/0-isupper.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x09-static_libraries/0-isupper.o -------------------------------------------------------------------------------- /0x09-static_libraries/0-memset.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _memset - fills memory with a constant byte 5 | * @s: memory area to be filled 6 | * @b: constant byte 7 | * @n: number of bytes 8 | * Return: the pointer to the memory area s 9 | */ 10 | char *_memset(char *s, char b, unsigned int n) 11 | { 12 | unsigned int i; 13 | 14 | for (i = 0; i < n; i++) 15 | { 16 | s[i] = b; 17 | } 18 | return (s); 19 | } 20 | -------------------------------------------------------------------------------- /0x09-static_libraries/0-memset.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x09-static_libraries/0-memset.o -------------------------------------------------------------------------------- /0x09-static_libraries/0-strcat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * *_strcat - concatenates two strings. 5 | * @src: string to be appended 6 | * @dest: string to be appended to 7 | * Return: a pointer of the resulting string 8 | */ 9 | char *_strcat(char *dest, char *src) 10 | { 11 | int i = 0; 12 | int dl = 0; 13 | 14 | while (dest[i++]) 15 | dl++; 16 | for (i = 0; src[i]; i++) 17 | dest[dl++] = src[i]; 18 | return (dest); 19 | } 20 | -------------------------------------------------------------------------------- /0x09-static_libraries/0-strcat.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x09-static_libraries/0-strcat.o -------------------------------------------------------------------------------- /0x09-static_libraries/1-isdigit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isdigit - checks for a digit (0 through 9) 5 | * @c: int to be checked 6 | * Return: 1 if c is a digit, 0 otherwise 7 | */ 8 | int _isdigit(int c) 9 | { 10 | if (c >= '0' && c <= '9') 11 | { 12 | return (1); 13 | } 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x09-static_libraries/1-isdigit.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x09-static_libraries/1-isdigit.o -------------------------------------------------------------------------------- /0x09-static_libraries/1-memcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _memcpy - copies memory area. 5 | * @src: memory area copied from 6 | * @dest: memory are copied to 7 | * @n: number of bytes 8 | * Return: pointer to dest 9 | */ 10 | char *_memcpy(char *dest, char *src, unsigned int n) 11 | { 12 | unsigned int i; 13 | 14 | for (i = 0; i < n; i++) 15 | { 16 | dest[i] = src[i]; 17 | } 18 | return (dest); 19 | } 20 | -------------------------------------------------------------------------------- /0x09-static_libraries/1-memcpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x09-static_libraries/1-memcpy.o -------------------------------------------------------------------------------- /0x09-static_libraries/1-strncat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strncat - concatenate two strings 5 | * @dest: The destination value 6 | * @src: THe source value 7 | * @n: The limit of the concatenation 8 | * Return: A pointer to the resulting string dest 9 | */ 10 | char *_strncat(char *dest, char *src, int n) 11 | { 12 | int i = 0, dest_len = 0; 13 | 14 | while (dest[i++]) 15 | dest_len++; 16 | for (i = 0; src[i] && i < n; i++) 17 | dest[dest_len++] = src[i]; 18 | return (dest); 19 | } 20 | -------------------------------------------------------------------------------- /0x09-static_libraries/1-strncat.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x09-static_libraries/1-strncat.o -------------------------------------------------------------------------------- /0x09-static_libraries/100-atoi.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _atoi - convert a string into an integer. 5 | * @s: string to be converted 6 | * Return: integer converted from the string 7 | */ 8 | int _atoi(char *s) 9 | { 10 | int sign = 1; 11 | unsigned int num = 0; 12 | 13 | do { 14 | if (*s == '-') 15 | sign *= -1; 16 | 17 | else if (*s >= '0' && *s <= '9') 18 | num = (num * 10) + (*s - '0'); 19 | 20 | else if (num > 0) 21 | break; 22 | } while (*s++); 23 | 24 | return (num * sign); 25 | } 26 | -------------------------------------------------------------------------------- /0x09-static_libraries/100-atoi.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x09-static_libraries/100-atoi.o -------------------------------------------------------------------------------- /0x09-static_libraries/2-strchr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _strchr - locate a character in a string 6 | * @s: string to check 7 | * @c: character to locate 8 | * Return: a pointer to the first occurrence of the character c 9 | * in the string s, or NULL if the character is not found 10 | */ 11 | char *_strchr(char *s, char c) 12 | { 13 | int a; 14 | 15 | while (1) 16 | { 17 | 18 | a = *s++; 19 | if (a == c) 20 | { 21 | return (s - 1); 22 | } 23 | if (a == 0) 24 | { 25 | return (NULL); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /0x09-static_libraries/2-strchr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x09-static_libraries/2-strchr.o -------------------------------------------------------------------------------- /0x09-static_libraries/2-strlen.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strlen - returns the length of a string 5 | * @s: string length to be returned 6 | * Return: the length of the string 7 | */ 8 | int _strlen(char *s) 9 | { 10 | int a; 11 | 12 | a = 0; 13 | 14 | while (s[a] != '\0') 15 | { 16 | a++; 17 | } 18 | return (a); 19 | } 20 | -------------------------------------------------------------------------------- /0x09-static_libraries/2-strlen.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x09-static_libraries/2-strlen.o -------------------------------------------------------------------------------- /0x09-static_libraries/2-strncpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strncpy - copies a string 5 | * @dest: destination string 6 | * @src: source of string 7 | * @n: number of bytes to copy 8 | * Return: Pointer to the resulting string 9 | */ 10 | char *_strncpy(char *dest, char *src, int n) 11 | { 12 | int i = 0; 13 | int dl = 0; 14 | 15 | while (src[i++]) 16 | dl++; 17 | 18 | for (i = 0; src[i] && i < n; i++) 19 | dest[i] = src[i]; 20 | 21 | for (i = dl; i < n; i++) 22 | dest[i] = '\0'; 23 | 24 | return (dest); 25 | } 26 | -------------------------------------------------------------------------------- /0x09-static_libraries/2-strncpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x09-static_libraries/2-strncpy.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-islower.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _islower - checks for lowercase character 5 | * @c: the character to check 6 | * Return: 1 if c is lowercase, 0 otherwise 7 | */ 8 | int _islower(int c) 9 | { 10 | return (c >= 'a' && c <= 'z'); 11 | } 12 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-islower.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x09-static_libraries/3-islower.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-puts.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _puts - prints a string, followed by a new line, to stdout 5 | * @str: pointer to the string to print 6 | */ 7 | void _puts(char *str) 8 | { 9 | int a; 10 | 11 | for (a = 0; str[a] != '\0'; a++) 12 | { 13 | _putchar(str[a]); 14 | } 15 | _putchar('\n'); 16 | } 17 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-puts.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x09-static_libraries/3-puts.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-strcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strcmp - compares two strings 5 | * @s1: first string 6 | * @s2: second string 7 | * Return: less than 0 if s1 is less than s2, 0 if they're equal, 8 | * more than 0 if s1 is greater than s2 9 | */ 10 | int _strcmp(char *s1, char *s2) 11 | { 12 | while (*s1 == *s2) 13 | { 14 | if (*s1 == '\0') 15 | { 16 | return (0); 17 | } 18 | s1++; 19 | s2++; 20 | } 21 | return (*s1 - *s2); 22 | } 23 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-strcmp.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x09-static_libraries/3-strcmp.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-strspn.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strspn - gets the length of a preix substring 5 | * @s: string to evaluate 6 | * @accept: string containing the list of characters to match in s 7 | * Return: the number of bytes in the initial segment of 8 | * s which consist only of bytes from accept 9 | */ 10 | unsigned int _strspn(char *s, char *accept) 11 | { 12 | unsigned int i = 0; 13 | int j; 14 | 15 | 16 | while (*s) 17 | { 18 | for (j = 0; accept[j]; j++) 19 | { 20 | if (accept[j] == *s) 21 | { 22 | i++; 23 | break; 24 | } 25 | 26 | else if ((accept[j + 1]) == '\0') 27 | return (i); 28 | } 29 | s++; 30 | } 31 | 32 | return (i); 33 | } 34 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-strspn.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x09-static_libraries/3-strspn.o -------------------------------------------------------------------------------- /0x09-static_libraries/4-isalpha.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isalpha - checks for alphabetic character 5 | * @c: the character to be checked 6 | * Return: 1 if c is a letter, lowercase or uppercase, 7 | * 0 otherwise 8 | */ 9 | int _isalpha(int c) 10 | { 11 | return ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')); 12 | } 13 | -------------------------------------------------------------------------------- /0x09-static_libraries/4-isalpha.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x09-static_libraries/4-isalpha.o -------------------------------------------------------------------------------- /0x09-static_libraries/4-strpbrk.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _strpbrk - searches a string for any of a set of bytes 6 | * @s: string to be searched 7 | * @accept: string containing the bytes 8 | * Return: a pointer to the byte in s that matches one of 9 | * the bytes in accept, or NULL if no such byte is found 10 | */ 11 | char *_strpbrk(char *s, char *accept) 12 | { 13 | int i; 14 | 15 | while (*s) 16 | { 17 | for (i = 0; accept[i]; i++) 18 | { 19 | if (accept[i] == *s) 20 | return (s); 21 | } 22 | s++; 23 | } 24 | return ('\0'); 25 | } 26 | -------------------------------------------------------------------------------- /0x09-static_libraries/4-strpbrk.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x09-static_libraries/4-strpbrk.o -------------------------------------------------------------------------------- /0x09-static_libraries/5-strstr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _strstr - locates a substring 6 | * @haystack: string to search in 7 | * @needle: substring to be located 8 | * Return: a pointer to the beginning of the located 9 | * substring, or NULL if the substring is not found. 10 | */ 11 | char *_strstr(char *haystack, char *needle) 12 | { 13 | int i; 14 | 15 | if (*needle == 0) 16 | return (haystack); 17 | while (*haystack) 18 | { 19 | i = 0; 20 | 21 | if (haystack[i] == needle[i]) 22 | { 23 | do { 24 | if (needle[i + 1] == '\0') 25 | return (haystack); 26 | i++; 27 | } while (haystack[i] == needle[i]); 28 | } 29 | haystack++; 30 | } 31 | return ('\0'); 32 | } 33 | -------------------------------------------------------------------------------- /0x09-static_libraries/5-strstr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x09-static_libraries/5-strstr.o -------------------------------------------------------------------------------- /0x09-static_libraries/6-abs.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _abs - computes the absolute value of an integer 5 | * @n: the int to be checked 6 | * Return: the absolute value of int 7 | */ 8 | int _abs(int n) 9 | { 10 | if (n >= 0) 11 | { 12 | return (n); 13 | } 14 | return (-n); 15 | } 16 | -------------------------------------------------------------------------------- /0x09-static_libraries/6-abs.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x09-static_libraries/6-abs.o -------------------------------------------------------------------------------- /0x09-static_libraries/9-strcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * *_strcpy - copies the string pointed to by src 5 | * including the terminating null byte (\0) 6 | * to the buffer pointed to by dest 7 | * @dest: pointer to the buffer in which we copy the string 8 | * @src: str to be copied 9 | * Return: the pointer to the dest 10 | */ 11 | char *_strcpy(char *dest, char *src) 12 | { 13 | 14 | int len, i; 15 | 16 | len = 0; 17 | 18 | while (src[len] != '\0') 19 | { 20 | len++; 21 | } 22 | 23 | for (i = 0; i < len; i++) 24 | { 25 | dest[i] = src[i]; 26 | } 27 | dest[i] = '\0'; 28 | 29 | return (dest); 30 | } 31 | -------------------------------------------------------------------------------- /0x09-static_libraries/9-strcpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x09-static_libraries/9-strcpy.o -------------------------------------------------------------------------------- /0x09-static_libraries/README.md: -------------------------------------------------------------------------------- 1 | Static Libraries 2 | 3 | FILE - libmy.a is a static library containing a list of functions. 4 | -------------------------------------------------------------------------------- /0x09-static_libraries/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _putchar - writes the character c to stdout 5 | * @c: The character to print 6 | * Return: On success 1. 7 | * On error, -1 is returned, and error is set appropriately. 8 | */ 9 | int _putchar(char c) 10 | { 11 | return (write(1, &c, 1)); 12 | } 13 | -------------------------------------------------------------------------------- /0x09-static_libraries/_putchar.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x09-static_libraries/_putchar.o -------------------------------------------------------------------------------- /0x09-static_libraries/create_static_lib.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -Wall -pedantic -Werror -Wextra -c *.c 3 | ar cr liball.a *.o 4 | -------------------------------------------------------------------------------- /0x09-static_libraries/libmy.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x09-static_libraries/libmy.a -------------------------------------------------------------------------------- /0x09-static_libraries/main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | 4 | 5 | int main(void) 6 | 7 | { 8 | 9 | _puts("\"At the end of the day, my goal was to be the best hacker\"\n\t- Kevin Mitnick"); 10 | 11 | return (0); 12 | 13 | } 14 | -------------------------------------------------------------------------------- /0x09-static_libraries/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | int _putchar(char c); 5 | int _islower(int c); 6 | int _isalpha(int c); 7 | int _abs(int n); 8 | int _isupper(int c); 9 | int _isdigit(int c); 10 | int _strlen(char *s); 11 | void _puts(char *s); 12 | char *_strcpy(char *dest, char *src); 13 | int _atoi(char *s); 14 | char *_strcat(char *dest, char *src); 15 | char *_strncat(char *dest, char *src, int n); 16 | char *_strncpy(char *dest, char *src, int n); 17 | int _strcmp(char *s1, char *s2); 18 | char *_memset(char *s, char b, unsigned int n); 19 | char *_memcpy(char *dest, char *src, unsigned int n); 20 | char *_strchr(char *s, char c); 21 | unsigned int _strspn(char *s, char *accept); 22 | char *_strpbrk(char *s, char *accept); 23 | char *_strstr(char *haystack, char *needle); 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /0x09-static_libraries/quote: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x09-static_libraries/quote -------------------------------------------------------------------------------- /0x0A-argc_argv/0-whatsmyname.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - prints the name of a program 6 | * @argc: number of arguments 7 | * @argv: array of arguments 8 | * Return: Always 0 (success) 9 | */ 10 | int main(int argc, char *argv[]) 11 | { 12 | printf("%s\n", argv[argc * 0]); 13 | 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x0A-argc_argv/1-args.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - prints the number of arguments passed into it. 6 | * @argc: number of arguments 7 | * @argv: array of arguments 8 | * Return: Always 0 (success) 9 | */ 10 | int main(int argc, char *argv[] __attribute__ ((unused))) 11 | { 12 | printf("%d\n", argc - 1); 13 | return (0); 14 | } 15 | -------------------------------------------------------------------------------- /0x0A-argc_argv/100-change.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - print the minimum number of coins to make 6 | * change for an amount of money 7 | * @argc: number of arguments 8 | * @argv: array of arguments 9 | * Return: 1 (Error) otherwise 0 (success) 10 | */ 11 | int main(int argc, char *argv[]) 12 | { 13 | int num, j, result; 14 | int coins[] = {25, 10, 5, 2, 1}; 15 | 16 | if (argc != 2) 17 | { 18 | printf("Error\n"); 19 | return (1); 20 | } 21 | 22 | num = atoi(argv[1]); 23 | result = 0; 24 | 25 | if (num < 0) 26 | { 27 | printf("0\n"); 28 | return (0); 29 | } 30 | 31 | for (j = 0; j < 5 && num >= 0; j++) 32 | { 33 | while (num >= coins[j]) 34 | { 35 | result++; 36 | num -= coins[j]; 37 | } 38 | } 39 | printf("%d\n", result); 40 | return (0); 41 | } 42 | -------------------------------------------------------------------------------- /0x0A-argc_argv/2-args.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - prints all the arguments it receives 6 | * @argc: number of arguments 7 | * @argv: array of arguments 8 | * Return: Always 0 (success) 9 | */ 10 | int main(int argc, char *argv[]) 11 | { 12 | int i; 13 | 14 | for (i = 0; i < argc; i++) 15 | { 16 | printf("%s\n", argv[i]); 17 | } 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /0x0A-argc_argv/3-mul.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - multiplies two numbers 6 | * @argc: number of arguments 7 | * @argv: array of arguments 8 | * Return: 1, otherwise 0 if two arguments not received 9 | */ 10 | int main(int argc, char *argv[]) 11 | { 12 | int i, j; 13 | 14 | if (argc == 1 || argc == 2) 15 | { 16 | printf("Error\n"); 17 | return (1); 18 | } 19 | else 20 | { 21 | j = 1; 22 | 23 | for (i = 1; i < 3; i++) 24 | j *= atoi(argv[i]); 25 | 26 | printf("%d\n", j); 27 | } 28 | return (0); 29 | } 30 | -------------------------------------------------------------------------------- /0x0A-argc_argv/4-add.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | #include 5 | 6 | /** 7 | * main - adds positive numbers 8 | * @argc: number of arguments 9 | * @argv: array of arguments 10 | * Return: 1, otherwise 0 if number contains non digit symbols 11 | */ 12 | int main(int argc, char *argv[]) 13 | { 14 | int i, j, length, sum; 15 | char *ptr; 16 | 17 | if (argc < 2) 18 | printf("0\n"); 19 | 20 | else 21 | { 22 | sum = 0; 23 | for (i = 1; i < argc; i++) 24 | { 25 | ptr = argv[i]; 26 | length = strlen(ptr); 27 | 28 | for (j = 0; j < length; j++) 29 | { 30 | if (isdigit(*(ptr + j)) == 0) 31 | { 32 | printf("Error\n"); 33 | return (1); 34 | } 35 | } 36 | sum += atoi(argv[i]); 37 | } 38 | printf("%d\n", sum); 39 | } 40 | return (0); 41 | } 42 | 43 | -------------------------------------------------------------------------------- /0x0A-argc_argv/README.md: -------------------------------------------------------------------------------- 1 | # Welcome to 0x0A-argc_argv 2 | 3 | `FILE 0-whatsmyname.c` is a program that prints its name, followed by a new line. 4 | 5 | `FILE 1-args.c` is a program that prints the number of arguments passed into it. 6 | 7 | `FILE 2-args.c` a program that prints all arguments it receives. 8 | 9 | `FILE 3-mul.c` is a program that multiplies two numbers. 10 | 11 | `FILE 4-add.c` is a program that adds positive numbers. 12 | 13 | `FILE 100-change.c` is a program that prints the minimum number of coins to make change for an amount of money. 14 | -------------------------------------------------------------------------------- /0x0A-argc_argv/add: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x0A-argc_argv/add -------------------------------------------------------------------------------- /0x0A-argc_argv/args: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x0A-argc_argv/args -------------------------------------------------------------------------------- /0x0A-argc_argv/change: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x0A-argc_argv/change -------------------------------------------------------------------------------- /0x0A-argc_argv/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | int _putchar(char c); 5 | int atoi(char *s); 6 | 7 | #endif 8 | -------------------------------------------------------------------------------- /0x0A-argc_argv/mul: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x0A-argc_argv/mul -------------------------------------------------------------------------------- /0x0A-argc_argv/mynameis: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x0A-argc_argv/mynameis -------------------------------------------------------------------------------- /0x0A-argc_argv/nargs: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x0A-argc_argv/nargs -------------------------------------------------------------------------------- /0x0B-malloc_free/0-create_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * create_array - creates an array of chars, and initiates it 6 | * with a specific char. 7 | * @size: size of array 8 | * @c: character 9 | * Return: a pointer to the array, or NULL if it fails 10 | */ 11 | char *create_array(unsigned int size, char c) 12 | { 13 | char *ch; 14 | unsigned int i; 15 | 16 | if (size == 0) 17 | return (NULL); 18 | ch = malloc(size * sizeof(c)); 19 | 20 | 21 | if (ch == NULL) 22 | return (NULL); 23 | 24 | for (i = 0; i < size; i++) 25 | ch[i] = c; 26 | 27 | return (ch); 28 | } 29 | -------------------------------------------------------------------------------- /0x0B-malloc_free/1-strdup.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _strdup - returns a pointer to a newly allocated space in memory 6 | * which contains a copy of the string given as a parameter. 7 | * @str: string to be duplicated 8 | * Return: a pointer to the duplicated string or NULL 9 | */ 10 | char *_strdup(char *str) 11 | { 12 | char *newstr; 13 | unsigned int i; 14 | int j; 15 | 16 | if (str == NULL) 17 | return (NULL); 18 | 19 | for (i = 0; str[i]; i++) 20 | j++; 21 | 22 | newstr = (char *)malloc(i + 1 * sizeof(char)); 23 | 24 | if (newstr == NULL) 25 | return (NULL); 26 | 27 | for (i = 0; str[i]; i++) 28 | newstr[i] = str[i]; 29 | 30 | newstr[j] = '\0'; 31 | 32 | return (newstr); 33 | } 34 | -------------------------------------------------------------------------------- /0x0B-malloc_free/100-argstostr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * *argstostr - concatenates all the arguments of the program 6 | * @ac: number of arguments 7 | * @av: array of arguments 8 | * Return: Pointer to the new string (Success), NULL (Error) 9 | */ 10 | char *argstostr(int ac, char **av) 11 | { 12 | int i, j, k, length; 13 | char *str; 14 | 15 | if (ac == 0 || av == NULL) 16 | return (NULL); 17 | 18 | length = 0; 19 | for (i = 0; i < ac; i++) 20 | { 21 | for (j = 0; av[i][j] != '\0'; j++) 22 | length++; 23 | length++; 24 | } 25 | 26 | str = malloc(sizeof(char) * (length + 1)); 27 | 28 | if (str == NULL) 29 | return (NULL); 30 | 31 | k = 0; 32 | 33 | for (i = 0; i < ac; i++) 34 | { 35 | for (j = 0; av[i][j] != '\0'; j++) 36 | { 37 | str[k] = av[i][j]; 38 | k++; 39 | } 40 | str[k] = '\n'; 41 | k++; 42 | } 43 | return (str); 44 | } 45 | -------------------------------------------------------------------------------- /0x0B-malloc_free/2-str_concat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * *str_concat - concatenates two strings 6 | * @s1: first string 7 | * @s2: second string 8 | * Return: NULL if concatenate fails, otherwise a pointer 9 | * pointing to a newly allocated space in memory 10 | */ 11 | char *str_concat(char *s1, char *s2) 12 | { 13 | char *concat_str; 14 | int a; 15 | int b = 0; 16 | int c = 0; 17 | 18 | if (s1 == NULL) 19 | s1 = ""; 20 | 21 | if (s2 == NULL) 22 | s2 = ""; 23 | 24 | for (a = 0; s1[a] || s2[a]; a++) 25 | c++; 26 | 27 | concat_str = malloc(sizeof(char) * c); 28 | 29 | if (concat_str == NULL) 30 | return (NULL); 31 | 32 | for (a = 0; s1[a]; a++) 33 | concat_str[b++] = s1[a]; 34 | 35 | for (a = 0; s2[a]; a++) 36 | concat_str[b++] = s2[a]; 37 | 38 | return (concat_str); 39 | } 40 | -------------------------------------------------------------------------------- /0x0B-malloc_free/3-alloc_grid.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * **alloc_grid - returns a pointer to a 2 dimensional array of integers. 6 | * @width: The width of the 2-dimensional array 7 | * @height: The height of the 2-dimensional array 8 | * Return: If width <= 0, height <= 0, or the function fails - NULL, 9 | * otherwise a pointer to the 2-dimensional array of integers. 10 | */ 11 | int **alloc_grid(int width, int height) 12 | { 13 | int **twodimen; 14 | int hgt; 15 | int wid; 16 | 17 | if (width <= 0 || height <= 0) 18 | return (NULL); 19 | 20 | twodimen = malloc(sizeof(int *) * height); 21 | 22 | if (twodimen == NULL) 23 | return (NULL); 24 | 25 | for (hgt = 0; hgt < height; hgt++) 26 | { 27 | twodimen[hgt] = malloc(sizeof(int) * width); 28 | 29 | if (twodimen[hgt] == NULL) 30 | { 31 | for (; hgt >= 0; hgt--) 32 | free(twodimen[hgt]); 33 | 34 | free(twodimen); 35 | return (NULL); 36 | } 37 | } 38 | for (hgt = 0; hgt < height; hgt++) 39 | { 40 | for (wid = 0; wid < width; wid++) 41 | twodimen[hgt][wid] = 0; 42 | } 43 | 44 | return (twodimen); 45 | } 46 | -------------------------------------------------------------------------------- /0x0B-malloc_free/4-free_grid.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * free_grid - frees a 2 dimensional grid previously created by 6 | * the alloc_grid function. 7 | * @grid: the 2-dimensional array of integers to be freed 8 | * @height: the height of grid 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/README.md: -------------------------------------------------------------------------------- 1 | # Welcome to 0x0B-malloc_free 2 | 3 | `FILE 0-create_array.c` is a function that creates an array of chars, and initiates it with a specific char. 4 | 5 | 6 | `FILE 1-strdup.c` is a function that returns a pointer to a newly allocated space in memory, which contains a copy of the string given as a parameter. 7 | 8 | 9 | `FILE 2-str_concat.c` is a function that concatenates two strings. 10 | 11 | 12 | `FILE 3-alloc_grid.c` is a function that returns a pointer to a 2 dimensional array of integers. 13 | 14 | 15 | `FILE 4-free_grid.c` is a function that frees a 2 dimensional grid previously created by your alloc_grid function. 16 | 17 | 18 | `FILE 100-argstostr.c` is a function that concatenates all the arguments of your program. 19 | 20 | 21 | `FILE 101-strtow.c` is a function that splits a string into words. 22 | -------------------------------------------------------------------------------- /0x0B-malloc_free/a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x0B-malloc_free/a -------------------------------------------------------------------------------- /0x0B-malloc_free/args: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x0B-malloc_free/args -------------------------------------------------------------------------------- /0x0B-malloc_free/c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x0B-malloc_free/c -------------------------------------------------------------------------------- /0x0B-malloc_free/f: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x0B-malloc_free/f -------------------------------------------------------------------------------- /0x0B-malloc_free/g: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x0B-malloc_free/g -------------------------------------------------------------------------------- /0x0B-malloc_free/main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | #include 4 | 5 | #include 6 | 7 | 8 | 9 | /** 10 | * 11 | * * print_tab - Prints an array of string 12 | * 13 | * * @tab: The array to print 14 | * 15 | * * 16 | * 17 | * * Return: nothing 18 | * 19 | * */ 20 | 21 | void print_tab(char **tab) 22 | 23 | { 24 | 25 | int i; 26 | 27 | 28 | 29 | for (i = 0; tab[i] != NULL; ++i) 30 | 31 | { 32 | 33 | printf("%s\n", tab[i]); 34 | 35 | } 36 | 37 | } 38 | 39 | 40 | 41 | /** 42 | * 43 | * * main - check the code for ALX School students. 44 | * 45 | * * 46 | * 47 | * * Return: 1 if an error occurred, 0 otherwise 48 | * 49 | * */ 50 | 51 | int main(void) 52 | 53 | { 54 | 55 | char **tab; 56 | 57 | 58 | 59 | tab = strtow(" ALX School #cisfun "); 60 | 61 | if (tab == NULL) 62 | 63 | { 64 | 65 | printf("Failed\n"); 66 | 67 | return (1); 68 | 69 | } 70 | 71 | print_tab(tab); 72 | 73 | return (0); 74 | 75 | } 76 | -------------------------------------------------------------------------------- /0x0B-malloc_free/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | char *create_array(unsigned int size, char c); 5 | char *_strdup(char *str); 6 | char *str_concat(char *s1, char *s2); 7 | int **alloc_grid(int width, int height); 8 | void free_grid(int **grid, int height); 9 | char *argstostr(int ac, char **av); 10 | char **strtow(char *str); 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /0x0B-malloc_free/s: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x0B-malloc_free/s -------------------------------------------------------------------------------- /0x0B-malloc_free/strtow: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x0B-malloc_free/strtow -------------------------------------------------------------------------------- /0x0C-more_malloc_free/.101-mul.c.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x0C-more_malloc_free/.101-mul.c.swp -------------------------------------------------------------------------------- /0x0C-more_malloc_free/.2-calloc.c.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x0C-more_malloc_free/.2-calloc.c.swp -------------------------------------------------------------------------------- /0x0C-more_malloc_free/.3-array_range.c.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x0C-more_malloc_free/.3-array_range.c.swp -------------------------------------------------------------------------------- /0x0C-more_malloc_free/0-malloc_checked.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * malloc_checked - allocated memory using malloc 6 | * @b: number of bytes to allocate 7 | * Return: a pointer to the allocated memory 8 | */ 9 | void *malloc_checked(unsigned int b) 10 | { 11 | void *ptr; 12 | 13 | ptr = malloc(b); 14 | 15 | if (ptr == NULL) 16 | exit(98); 17 | 18 | return (ptr); 19 | } 20 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/1-string_nconcat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x0C-more_malloc_free/1-string_nconcat -------------------------------------------------------------------------------- /0x0C-more_malloc_free/1-string_nconcat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * string_nconcat - concatenates two strings 6 | * @s1: First string 7 | * @s2: Second string 8 | * @n: number of bytes from s2 to concatenate to s1 9 | * Return: pointer to the resulting string 10 | */ 11 | char *string_nconcat(char *s1, char *s2, unsigned int n) 12 | { 13 | char *s; 14 | unsigned int i = 0; 15 | unsigned int j = 0; 16 | unsigned int len = 0; 17 | unsigned int len1 = 0; 18 | 19 | while (s1 && s1[len]) 20 | len++; 21 | while (s2 && s2[len1]) 22 | len1++; 23 | 24 | if (n < len1) 25 | s = malloc((len + n + 1) * sizeof(char)); 26 | else 27 | s = malloc((len + len1 + 1) * sizeof(char)); 28 | 29 | if (!s) 30 | return (NULL); 31 | 32 | while (i < len) 33 | { 34 | s[i] = s1[i]; 35 | i++; 36 | } 37 | 38 | while (n < len1 && i < (len + n)) 39 | s[i++] = s2[j++]; 40 | 41 | while (n >= len1 && i < (len1 + len)) 42 | s[i++] = s2[j++]; 43 | 44 | s[i] = '\0'; 45 | 46 | return (s); 47 | } 48 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/100-realloc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x0C-more_malloc_free/100-realloc -------------------------------------------------------------------------------- /0x0C-more_malloc_free/100-realloc.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _realloc - reallocates a memory block by using malloc and free 6 | * @ptr: poointer to the memory previoously allocated by malloc 7 | * @old_size: size of the allocated memory for ptr 8 | * @new_size: new size of the new memory block 9 | * Return: pointer to new allocated memory block 10 | */ 11 | void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size) 12 | { 13 | char *ptr1; 14 | char *old_ptr; 15 | unsigned int i; 16 | 17 | if (new_size == old_size) 18 | return (ptr); 19 | 20 | if (new_size == 0 && ptr) 21 | { 22 | free(ptr); 23 | return (NULL); 24 | } 25 | 26 | if (!ptr) 27 | return (malloc(new_size)); 28 | 29 | ptr1 = malloc(new_size); 30 | if (!ptr1) 31 | return (NULL); 32 | 33 | old_ptr = ptr; 34 | 35 | if (new_size < old_size) 36 | { 37 | for (i = 0; i < new_size; i++) 38 | ptr1[i] = old_ptr[i]; 39 | } 40 | 41 | if (new_size > old_size) 42 | { 43 | for (i = 0; i < old_size; i++) 44 | ptr1[i] = old_ptr[i]; 45 | } 46 | 47 | free(ptr); 48 | return (ptr1); 49 | } 50 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/101-mul: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x0C-more_malloc_free/101-mul -------------------------------------------------------------------------------- /0x0C-more_malloc_free/2-calloc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x0C-more_malloc_free/2-calloc -------------------------------------------------------------------------------- /0x0C-more_malloc_free/2-calloc.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * *_memset - fills memory with a constant byte 6 | * @s: memory area to be filled 7 | * @b: char to copy 8 | * @n: number of times to copy b 9 | * Return: pointer to the memory area s 10 | */ 11 | char *_memset(char *s, char b, unsigned int n) 12 | { 13 | unsigned int i; 14 | 15 | for (i = 0; i < n; i++) 16 | { 17 | s[i] = b; 18 | } 19 | return (s); 20 | } 21 | 22 | /** 23 | * *_calloc - allocates memory for an array 24 | * @nmemb: number of elements in the array 25 | * @size: size of each element 26 | * Return: pointer to allocated memory 27 | */ 28 | void *_calloc(unsigned int nmemb, unsigned int size) 29 | { 30 | char *ptr; 31 | 32 | if (nmemb == 0 || size == 0) 33 | return (NULL); 34 | 35 | ptr = malloc(nmemb * size); 36 | 37 | if (ptr == NULL) 38 | return (NULL); 39 | 40 | _memset(ptr, 0, nmemb * size); 41 | 42 | return (ptr); 43 | } 44 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/3-array_range: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x0C-more_malloc_free/3-array_range -------------------------------------------------------------------------------- /0x0C-more_malloc_free/3-array_range.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * *array_range - creates an array of integers 6 | * @min: minimum range of values stored 7 | * @max: maximum range of values stored and number of elements 8 | * Return: pointer to the new array 9 | */ 10 | int *array_range(int min, int max) 11 | { 12 | int *ptr; 13 | int i; 14 | int size; 15 | 16 | if (min > max) 17 | return (NULL); 18 | 19 | size = max - min + 1; 20 | 21 | ptr = malloc(sizeof(int) * size); 22 | 23 | if (ptr == NULL) 24 | return (NULL); 25 | 26 | for (i = 0; min <= max; i++) 27 | ptr[i] = min++; 28 | 29 | return (ptr); 30 | } 31 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/README.md: -------------------------------------------------------------------------------- 1 | # Welcome to 0x0C-more_malloc_free 2 | 3 | `FILE 0-malloc_checked.c` is a function that allocates memory using malloc. 4 | 5 | 6 | `FILE 1-string_nconcat.c` is a function that concatenates two strings. 7 | 8 | 9 | `FILE 2-calloc.c` is a function that allovates memory for an array, using malloc. 10 | 11 | 12 | `FILE 3-array_range.c` is a function that creates an array of integers. 13 | 14 | `FILE 100-realloc.c` is a function that reallocates a memory block using malloc and free. 15 | 16 | `FILE 101-mul.c` is a program that multiplies two positive numbers. 17 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _putchar - writes the character of c to stdout 5 | * @c: the character to print 6 | * Return: On success 1 7 | * On error, -1 is returned , and erroris set appropriately 8 | */ 9 | int _putchar(char c) 10 | { 11 | return (write(1, &c, 1)); 12 | } 13 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x0C-more_malloc_free/a -------------------------------------------------------------------------------- /0x0C-more_malloc_free/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | void *malloc_checked(unsigned int b); 5 | char *string_nconcat(char *s1, char *s2, unsigned int n); 6 | void *_calloc(unsigned int nmemb, unsigned int size); 7 | int *array_range(int min, int max); 8 | void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size); 9 | int _putchar(char c); 10 | 11 | #endif /*MAIN_H*/ 12 | -------------------------------------------------------------------------------- /0x0D-preprocessor/.main.c.swo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x0D-preprocessor/.main.c.swo -------------------------------------------------------------------------------- /0x0D-preprocessor/.main.c.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x0D-preprocessor/.main.c.swp -------------------------------------------------------------------------------- /0x0D-preprocessor/0-object_like_macro.h: -------------------------------------------------------------------------------- 1 | #ifndef OBJECT_LIKE_MACRO_H 2 | #define OBJECT_LIKE_MACRO_H 3 | 4 | #define SIZE 1024 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /0x0D-preprocessor/02-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints the name of the file 5 | * Return: Always 0 (Success) 6 | */ 7 | int main(void) 8 | { 9 | printf("%s\n", __FILE__); 10 | return (0); 11 | } 12 | -------------------------------------------------------------------------------- /0x0D-preprocessor/1-pi.h: -------------------------------------------------------------------------------- 1 | #ifndef PI_H 2 | #define PI_H 3 | 4 | #define PI 3.14159265359 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /0x0D-preprocessor/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints the name of the file 5 | * Return: Always 0 (Success) 6 | */ 7 | int main(void) 8 | { 9 | printf("%s\n", __FILE__); 10 | return (0); 11 | } 12 | -------------------------------------------------------------------------------- /0x0D-preprocessor/3-function_like_macro.h: -------------------------------------------------------------------------------- 1 | #ifndef FUNCTION_LIKE_MACRO_H 2 | #define FUNCTION_LIKE_MACRO_H 3 | 4 | #define ABS(x) ((x) < (0) ? -(x) : (x)) 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /0x0D-preprocessor/4-sum.h: -------------------------------------------------------------------------------- 1 | #ifndef SUM_H 2 | #define SUM_H 3 | 4 | #define SUM(x, y) ((x) + (y)) 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /0x0D-preprocessor/README.md: -------------------------------------------------------------------------------- 1 | `FILE 0-object_like_macro.h` is a header file that defines a macro named size and an abbreviation for the token 1024. 2 | 3 | `FILE 1-pi.h` is a header file that defines a macro named PI and an abbreviation for the token 3.14159265359. 4 | 5 | `FILE 2-main.c` is a program that prints the name of the file it was complied from. 6 | 7 | `FILE 3-function_like_macro.h` is a function-like macro ABS(x) that computes the absolute value of a number x. 8 | 9 | `FILE 4-sum.h` is a function-like macro SUM(x, y) that computes the sum of the numbers x and y. 10 | -------------------------------------------------------------------------------- /0x0D-preprocessor/a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x0D-preprocessor/a -------------------------------------------------------------------------------- /0x0D-preprocessor/b: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x0D-preprocessor/b -------------------------------------------------------------------------------- /0x0D-preprocessor/c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x0D-preprocessor/c -------------------------------------------------------------------------------- /0x0D-preprocessor/cc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x0D-preprocessor/cc -------------------------------------------------------------------------------- /0x0D-preprocessor/d: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x0D-preprocessor/d -------------------------------------------------------------------------------- /0x0D-preprocessor/e: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x0D-preprocessor/e -------------------------------------------------------------------------------- /0x0D-preprocessor/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "4-sum.h" 4 | 5 | #include "4-sum.h" 6 | 7 | 8 | 9 | /** 10 | * 11 | * * main - check the code 12 | * 13 | * * 14 | * 15 | * * Return: Always 0. 16 | * 17 | * */ 18 | 19 | int main(void) 20 | 21 | { 22 | 23 | int s; 24 | 25 | 26 | 27 | s = SUM(98, 1024); 28 | 29 | printf("%d\n", s); 30 | 31 | return (0); 32 | 33 | } 34 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/1-init_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | 4 | /** 5 | * init_dog - initializes a variable of type struct dog 6 | * @d: pointer to struct dog to initialize 7 | * @name: Name to initialize 8 | * @age: Age to initilaize 9 | * @owner: Owner to initialize 10 | */ 11 | void init_dog(struct dog *d, char *name, float age, char *owner) 12 | { 13 | if (d == NULL) 14 | d = malloc(sizeof(struct dog)); 15 | d->name = name; 16 | d->age = age; 17 | d->owner = owner; 18 | } 19 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/2-print_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * print_dog - prints a struct dog 7 | * @d: struct dog to print 8 | */ 9 | void print_dog(struct dog *d) 10 | { 11 | if (d == NULL) 12 | return; 13 | 14 | if (d->name == NULL) 15 | d->name = "(nil)"; 16 | if (d->owner == NULL) 17 | d->owner = "(nil)"; 18 | 19 | printf("Name: %s\nAge: %f\nOwner: %s\n", d->name, d->age, d->owner); 20 | } 21 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/5-free_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | 4 | /** 5 | * free_dog - frees dogs 6 | * @d: struct dog to free 7 | */ 8 | void free_dog(dog_t *d) 9 | { 10 | if (d) 11 | { 12 | free(d->name); 13 | free(d->owner); 14 | free(d); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/README.md: -------------------------------------------------------------------------------- 1 | FILE dog.h defines a new type struct dog with other elements. 2 | 3 | FILE 1-init_dog.c is a function that initialize a variable of type struct dog. 4 | 5 | FILE 2-print_dog.c is a function that prints a struct dog. 6 | 7 | FILE 4-new_dog.c is a function that creates a new dog. 8 | 9 | FILE 5-free_dog.c is a function that free dogs. 10 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x0E-structures_typedef/a -------------------------------------------------------------------------------- /0x0E-structures_typedef/b: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x0E-structures_typedef/b -------------------------------------------------------------------------------- /0x0E-structures_typedef/c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x0E-structures_typedef/c -------------------------------------------------------------------------------- /0x0E-structures_typedef/dog.h: -------------------------------------------------------------------------------- 1 | #ifndef DOG_H 2 | #define DOG_H 3 | 4 | /** 5 | * struct dog - a new type describing a dog 6 | * @name: Name of the dog 7 | * @age: Age of the dog 8 | * @owner: Owner of the dog 9 | * 10 | * Description: file that defines a new type struct dog 11 | */ 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 | void free_dog(dog_t *d); 29 | char *_strcpy(char *dest, char *src); 30 | int _strlen(char *s); 31 | 32 | #endif /*DOG_H*/ 33 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/e: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x0E-structures_typedef/e -------------------------------------------------------------------------------- /0x0E-structures_typedef/f: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x0E-structures_typedef/f -------------------------------------------------------------------------------- /0x0E-structures_typedef/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "dog.h" 4 | 5 | 6 | 7 | /** 8 | * 9 | * * main - check the code 10 | * 11 | * * 12 | * 13 | * * Return: Always 0. 14 | * 15 | * */ 16 | 17 | int main(void) 18 | 19 | { 20 | 21 | dog_t *my_dog; 22 | 23 | 24 | 25 | my_dog = new_dog("Poppy", 3.5, "Bob"); 26 | 27 | printf("My name is %s, and I am %.1f :) - Woof!\n", my_dog->name, my_dog->age); 28 | 29 | free_dog(my_dog); 30 | 31 | return (0); 32 | 33 | } 34 | -------------------------------------------------------------------------------- /0x0F-function_pointers/0-print_name.c: -------------------------------------------------------------------------------- 1 | #include "function_pointers.h" 2 | #include 3 | 4 | /** 5 | * print_name - prints a name 6 | * @name: name to print 7 | * @f: pointer to the printing function 8 | */ 9 | void print_name(char *name, void (*f)(char *)) 10 | { 11 | if (!name || !f) 12 | return; 13 | f(name); 14 | } 15 | -------------------------------------------------------------------------------- /0x0F-function_pointers/1-array_iterator.c: -------------------------------------------------------------------------------- 1 | #include "function_pointers.h" 2 | #include 3 | 4 | /** 5 | * array_iterator - executes a function given as a parameter 6 | * on each element of an array. 7 | * @array: array to iterate over 8 | * @size: size of the array 9 | * @action: pointer to function used 10 | */ 11 | void array_iterator(int *array, size_t size, void (*action)(int)) 12 | { 13 | unsigned int i; 14 | 15 | if (!array || !action) 16 | return; 17 | 18 | for (i = 0; i < size; i++) 19 | action(array[i]); 20 | } 21 | -------------------------------------------------------------------------------- /0x0F-function_pointers/100-main_opcodes.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * main - prints its own opcodes 6 | * @argc: number of arguments 7 | * @argv: array of arguments 8 | * Return: Always 0 (Success) 9 | */ 10 | int main(int argc, char *argv[]) 11 | { 12 | int bytes; 13 | int index; 14 | int (*address)(int, char **) = main; 15 | unsigned char opcode; 16 | 17 | if (argc != 2) 18 | { 19 | printf("Error\n"); 20 | exit(1); 21 | } 22 | 23 | bytes = atoi(argv[1]); 24 | 25 | if (bytes < 0) 26 | { 27 | printf("Error\n"); 28 | exit(2); 29 | } 30 | 31 | for (index = 0; index < bytes; index++) 32 | { 33 | opcode = *(unsigned char *)address; 34 | printf("%.2x", opcode); 35 | 36 | if (index == bytes - 1) 37 | continue; 38 | printf(" "); 39 | 40 | address++; 41 | } 42 | 43 | printf("\n"); 44 | 45 | return (0); 46 | } 47 | -------------------------------------------------------------------------------- /0x0F-function_pointers/2-int_index.c: -------------------------------------------------------------------------------- 1 | #include "function_pointers.h" 2 | 3 | /** 4 | * int_index - searches for an integer. 5 | * @array: array to search in 6 | * @size: size of the array 7 | * @cmp: pointer to the comparing function 8 | * Return: index of the first element for which the cmp 9 | * function does not return 0, or -1 if no match is found 10 | * or size is negative 11 | */ 12 | int int_index(int *array, int size, int (*cmp)(int)) 13 | { 14 | int i; 15 | 16 | if (array && cmp) 17 | { 18 | for (i = 0; i < size; i++) 19 | { 20 | if (cmp(array[i]) != 0) 21 | return (i); 22 | } 23 | } 24 | return (-1); 25 | } 26 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-calc.h: -------------------------------------------------------------------------------- 1 | #ifndef CALC_H 2 | #define CALC_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | /** 9 | * struct op - Struct op 10 | * @op: The operator 11 | * @f: The function associated 12 | */ 13 | typedef struct op 14 | { 15 | char *op; 16 | int (*f)(int a, int b); 17 | } op_t; 18 | 19 | int op_add(int a, int b); 20 | int op_sub(int a, int b); 21 | int op_mul(int a, int b); 22 | int op_div(int a, int b); 23 | int op_mod(int a, int b); 24 | int (*get_op_func(char *s))(int, int); 25 | 26 | #endif 27 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-get_op_func.c: -------------------------------------------------------------------------------- 1 | #include "3-calc.h" 2 | 3 | /** 4 | * get_op_func - function pointer that selects the correct 5 | * function to perform the operation asked by the user 6 | * @s: the operator given by the user 7 | * Return: pointer to the function that corresponds to the 8 | * operator given as a parameter 9 | */ 10 | int (*get_op_func(char *s))(int, int) 11 | { 12 | op_t ops[] = { 13 | {"+", op_add}, 14 | {"-", op_sub}, 15 | {"*", op_mul}, 16 | {"/", op_div}, 17 | {"%", op_mod}, 18 | {NULL, NULL} 19 | }; 20 | int i; 21 | 22 | i = 0; 23 | 24 | while (ops[i].op) 25 | { 26 | if (strcmp(ops[i].op, s) == 0) 27 | return (ops[i].f); 28 | i++; 29 | } 30 | 31 | return (NULL); 32 | } 33 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-main.c: -------------------------------------------------------------------------------- 1 | #include "3-calc.h" 2 | 3 | /** 4 | * main - program that perfroms simple operations 5 | * @argc: number of arguments 6 | * @argv: array of arguments 7 | * Return: Always 0 (Success) 8 | */ 9 | int main(int argc, char *argv[]) 10 | { 11 | int arg1, arg2, result; 12 | char o; 13 | int (*func)(int, int); 14 | 15 | if (argc != 4) 16 | { 17 | printf("Error\n"); 18 | exit(98); 19 | } 20 | 21 | arg1 = atoi(argv[1]); 22 | arg2 = atoi(argv[3]); 23 | 24 | func = get_op_func(argv[2]); 25 | 26 | if (!func) 27 | { 28 | printf("Error\n"); 29 | exit(99); 30 | } 31 | 32 | o = *argv[2]; 33 | 34 | if ((o == '/' || o == '%') && arg2 == 0) 35 | { 36 | printf("Error\n"); 37 | exit(100); 38 | } 39 | 40 | result = func(arg1, arg2); 41 | 42 | printf("%d\n", result); 43 | 44 | return (0); 45 | } 46 | -------------------------------------------------------------------------------- /0x0F-function_pointers/README.md: -------------------------------------------------------------------------------- 1 | `FILE 0-print_name.c` is a function that prints a name. 2 | 3 | `FILE 1-array_iterator.c` is a function that executes a function given as a parameter on each element of an array. 4 | 5 | `FILE 2-int_index.c` is a function that searches for an integer. 6 | 7 | `FILES 3-main.c, 3-op_functions.c, 3-get_op_func.c, 3-calc.h` are all part of a program that performs simple operations. 8 | -------------------------------------------------------------------------------- /0x0F-function_pointers/a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x0F-function_pointers/a -------------------------------------------------------------------------------- /0x0F-function_pointers/b: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x0F-function_pointers/b -------------------------------------------------------------------------------- /0x0F-function_pointers/c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x0F-function_pointers/c -------------------------------------------------------------------------------- /0x0F-function_pointers/calc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x0F-function_pointers/calc -------------------------------------------------------------------------------- /0x0F-function_pointers/function_pointers.h: -------------------------------------------------------------------------------- 1 | #ifndef FUNCTION_POINTERS_H 2 | #define FUNCTION_POINTERS_H 3 | 4 | #include 5 | 6 | void print_name(char *name, void (*f)(char *)); 7 | void array_iterator(int *array, size_t size, void (*action)(int)); 8 | int int_index(int *array, int size, int (*cmp)(int)); 9 | 10 | #endif 11 | -------------------------------------------------------------------------------- /0x0F-function_pointers/main: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x0F-function_pointers/main -------------------------------------------------------------------------------- /0x10-variadic_functions/0-sum_them_all.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | 3 | /** 4 | * sum_them_all - returns the sum of all its parameters. 5 | * @n: number of arguments passed to the function 6 | * Return: the resulting sum 7 | */ 8 | int sum_them_all(const unsigned int n, ...) 9 | { 10 | unsigned int i; 11 | int sum = 0; 12 | va_list list; 13 | 14 | va_start(list, n); 15 | 16 | for (i = 0; i < n; i++) 17 | sum += va_arg(list, int); 18 | 19 | va_end(list); 20 | 21 | return (sum); 22 | } 23 | -------------------------------------------------------------------------------- /0x10-variadic_functions/1-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | 3 | /** 4 | * print_numbers - prints numbers 5 | * @separator: string to be printed between numbers 6 | * @n: number of integers passed to the function 7 | */ 8 | void print_numbers(const char *separator, const unsigned int n, ...) 9 | { 10 | unsigned int i; 11 | va_list list; 12 | 13 | va_start(list, n); 14 | 15 | for (i = 0; i < n; i++) 16 | { 17 | if (!separator) 18 | printf("%d", va_arg(list, int)); 19 | else if (separator && i == 0) 20 | printf("%d", va_arg(list, int)); 21 | else 22 | printf("%s%d", separator, va_arg(list, int)); 23 | } 24 | 25 | va_end(list); 26 | 27 | printf("\n"); 28 | } 29 | -------------------------------------------------------------------------------- /0x10-variadic_functions/2-print_strings.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | 3 | /** 4 | * print_strings - prints strings 5 | * @separator: separator between strings 6 | * @n: number of arguments 7 | */ 8 | void print_strings(const char *separator, const unsigned int n, ...) 9 | { 10 | unsigned int i; 11 | char *str; 12 | 13 | va_list list; 14 | 15 | va_start(list, n); 16 | 17 | for (i = 0; i < n; i++) 18 | { 19 | str = va_arg(list, char *); 20 | if (!str) 21 | str = "(nil)"; 22 | if (!separator) 23 | printf("%s", str); 24 | else if (separator && i == 0) 25 | printf("%s", str); 26 | else 27 | printf("%s%s", separator, str); 28 | } 29 | 30 | printf("\n"); 31 | 32 | va_end(list); 33 | } 34 | -------------------------------------------------------------------------------- /0x10-variadic_functions/3-print_all.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | 3 | /** 4 | * print_all - prints anything 5 | * @format: list of types of arguments passed to the function 6 | */ 7 | void print_all(const char * const format, ...) 8 | { 9 | int i = 0; 10 | char *str, *sep = ""; 11 | 12 | va_list list; 13 | 14 | va_start(list, format); 15 | 16 | if (format) 17 | { 18 | while (format[i]) 19 | { 20 | switch (format[i]) 21 | { 22 | case 'c': 23 | printf("%s%c", sep, va_arg(list, int)); 24 | break; 25 | case 'i': 26 | printf("%s%d", sep, va_arg(list, int)); 27 | break; 28 | case 'f': 29 | printf("%s%f", sep, va_arg(list, double)); 30 | break; 31 | case 's': 32 | str = va_arg(list, char *); 33 | if (!str) 34 | str = "(nil)"; 35 | printf("%s%s", sep, str); 36 | break; 37 | default: 38 | i++; 39 | continue; 40 | } 41 | sep = ", "; 42 | i++; 43 | } 44 | } 45 | 46 | printf("\n"); 47 | va_end(list); 48 | } 49 | -------------------------------------------------------------------------------- /0x10-variadic_functions/README.md: -------------------------------------------------------------------------------- 1 | FILE 0-sum_them_all.c is a function that returns the sum of all its parameters. 2 | 3 | FILE 1-print_numbers.c is a function that prints numbers, followed by a new line. 4 | 5 | FILE 2-print_strings.c is a function that prints strings. 6 | 7 | FILE 3-print_all.c is a function that prints anything. 8 | -------------------------------------------------------------------------------- /0x10-variadic_functions/a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x10-variadic_functions/a -------------------------------------------------------------------------------- /0x10-variadic_functions/b: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x10-variadic_functions/b -------------------------------------------------------------------------------- /0x10-variadic_functions/c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x10-variadic_functions/c -------------------------------------------------------------------------------- /0x10-variadic_functions/d: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x10-variadic_functions/d -------------------------------------------------------------------------------- /0x10-variadic_functions/main.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | 3 | 4 | 5 | /** 6 | * 7 | * * main - check the code 8 | * 9 | * * 10 | * 11 | * * Return: Always 0. 12 | * 13 | * */ 14 | 15 | int main(void) 16 | 17 | { 18 | 19 | print_all("ceis", 'B', 3, "stSchool"); 20 | 21 | return (0); 22 | 23 | } 24 | -------------------------------------------------------------------------------- /0x10-variadic_functions/variadic_functions.h: -------------------------------------------------------------------------------- 1 | #ifndef VARIADIC_FUNCTIONS_H 2 | #define VARIADIC_FUNCTIONS_H 3 | 4 | #include 5 | #include 6 | 7 | int sum_them_all(const unsigned int n, ...); 8 | void print_numbers(const char *separator, const unsigned int n, ...); 9 | void print_strings(const char *separator, const unsigned int n, ...); 10 | void print_all(const char * const format, ...); 11 | 12 | #endif /*VARIADIC_FUNCTIONS_H*/ 13 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/0-print_list.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /** 5 | * print_list - prints all the elements of a list_t 6 | * list. 7 | * @h: pointer to the list_t list to print 8 | * Return: the number of nodes printed 9 | */ 10 | size_t print_list(const list_t *h) 11 | { 12 | size_t s = 0; 13 | 14 | while (h) 15 | { 16 | if (!h->str) 17 | printf("[0] (nil)\n"); 18 | else 19 | printf("[%u] %s\n", h->len, h->str); 20 | h = h->next; 21 | s++; 22 | } 23 | 24 | return (s); 25 | } 26 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/1-list_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /** 5 | * list_len - returns the number of elements in a linked 6 | * list_t list. 7 | * @h: pointer to the list_t list 8 | * Return: number of elements in h 9 | */ 10 | size_t list_len(const list_t *h) 11 | { 12 | size_t n = 0; 13 | 14 | while (h) 15 | { 16 | n++; 17 | h = h->next; 18 | } 19 | return (n); 20 | } 21 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/100-first.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void first(void) __attribute__ ((constructor)); 4 | 5 | /** 6 | * first - prints a sentence before the main function 7 | * is executed 8 | */ 9 | void first(void) 10 | { 11 | printf("You're beat! and yet, you must allow,\n"); 12 | printf("I bore my house upon my back!\n"); 13 | } 14 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/2-add_node.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * add_node - adds a new node at the end of a list_t list. 7 | * @head: double pointer to the list_t list 8 | * @str: new string to add in the node 9 | * Return: the address of the new element, or NULL if it fails 10 | */ 11 | list_t *add_node(list_t **head, const char *str) 12 | { 13 | list_t *new; 14 | unsigned int len = 0; 15 | 16 | while (str[len]) 17 | len++; 18 | 19 | new = malloc(sizeof(list_t)); 20 | if (!new) 21 | return (NULL); 22 | 23 | new->str = strdup(str); 24 | new->len = len; 25 | new->next = (*head); 26 | (*head) = new; 27 | 28 | return (*head); 29 | } 30 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/3-add_node_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * add_node_end - adds a new node at the end of a list_t list. 7 | * @head: double pointer to the list_t list 8 | * @str: string to put in the new node 9 | * Return: address of the new element, or NULL if it failed 10 | */ 11 | list_t *add_node_end(list_t **head, const char *str) 12 | { 13 | list_t *new; 14 | list_t *temp = *head; 15 | unsigned int len = 0; 16 | 17 | while (str[len]) 18 | len++; 19 | 20 | new = malloc(sizeof(list_t)); 21 | if (!new) 22 | return (NULL); 23 | 24 | new->str = strdup(str); 25 | new->len = len; 26 | new->next = NULL; 27 | 28 | if (*head == NULL) 29 | { 30 | *head = new; 31 | return (new); 32 | } 33 | 34 | while (temp->next) 35 | temp = temp->next; 36 | 37 | temp->next = new; 38 | 39 | return (new); 40 | } 41 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/4-free_list.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /** 5 | * free_list - frees a linked list 6 | * @head: list_t list to be freed 7 | */ 8 | void free_list(list_t *head) 9 | { 10 | list_t *temp; 11 | 12 | while (head) 13 | { 14 | temp = head->next; 15 | free(head->str); 16 | free(head); 17 | head = temp; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/README.md: -------------------------------------------------------------------------------- 1 | `FILE 0-print_list.c` is a function that prints all the elements of a list_t list. 2 | 3 | `FILE 1-list_len.c` is a function that returns the number of elements in a linked list_t list. 4 | 5 | `FILE 2-add_node.c` is a function that adds a new node at the beginning of a list_t list. 6 | 7 | `FILE 3-add_node_end.c` is a function that adds a new node at the end of a list_t list. 8 | 9 | `FILE 4-free_list.c` is a function that frees a list_t list. 10 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x12-singly_linked_lists/a -------------------------------------------------------------------------------- /0x12-singly_linked_lists/b: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x12-singly_linked_lists/b -------------------------------------------------------------------------------- /0x12-singly_linked_lists/c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x12-singly_linked_lists/c -------------------------------------------------------------------------------- /0x12-singly_linked_lists/d: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x12-singly_linked_lists/d -------------------------------------------------------------------------------- /0x12-singly_linked_lists/e: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x12-singly_linked_lists/e -------------------------------------------------------------------------------- /0x12-singly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef LISTS_H 2 | #define LISTS_H 3 | 4 | #include 5 | 6 | /** 7 | * struct list_s - singly linked list 8 | * @str: string - (malloc'ed string) 9 | * @len: length of the string 10 | * @next: points to the next node 11 | * 12 | * Description: singly linked list node structure 13 | */ 14 | typedef struct list_s 15 | { 16 | char *str; 17 | unsigned int len; 18 | struct list_s *next; 19 | } list_t; 20 | 21 | size_t print_list(const list_t *h); 22 | size_t list_len(const list_t *h); 23 | list_t *add_node(list_t **head, const char *str); 24 | list_t *add_node_end(list_t **head, const char *str); 25 | void free_list(list_t *head); 26 | 27 | #endif /*LISTS_H*/ 28 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | 5 | #include 6 | 7 | #include "lists.h" 8 | 9 | 10 | 11 | /** 12 | * 13 | * * main - check the code 14 | * 15 | * * 16 | * 17 | * * Return: Always 0. 18 | * 19 | * */ 20 | 21 | int main(void) 22 | 23 | { 24 | 25 | list_t *head; 26 | 27 | 28 | 29 | head = NULL; 30 | 31 | add_node_end(&head, "Bob"); 32 | 33 | add_node_end(&head, "&"); 34 | 35 | add_node_end(&head, "Kris"); 36 | 37 | add_node_end(&head, "love"); 38 | 39 | add_node_end(&head, "asm"); 40 | 41 | print_list(head); 42 | 43 | free_list(head); 44 | 45 | head = NULL; 46 | 47 | return (0); 48 | 49 | } 50 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/0-print_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * print_listint - prints all the elements of a listint_t list. 5 | * @h: linked list of type listint_t to print 6 | * Return: number of nodes 7 | */ 8 | size_t print_listint(const listint_t *h) 9 | { 10 | size_t num = 0; 11 | 12 | while (h) 13 | { 14 | printf("%d\n", h->n); 15 | num++; 16 | h = h->next; 17 | } 18 | 19 | return (num); 20 | } 21 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/1-listint_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * listint_len - returns the number of elements in a linked listint_t list. 5 | * @h: linked list of type listint_t to traverse 6 | * Return: number of nodes 7 | */ 8 | size_t listint_len(const listint_t *h) 9 | { 10 | size_t num = 0; 11 | 12 | while (h) 13 | { 14 | num++; 15 | h = h->next; 16 | } 17 | 18 | return (num); 19 | } 20 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/10-delete_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * delete_nodeint_at_index - deletes a node in a linked list at a certain index 5 | * @head: pointer to the first element in the list 6 | * @index: index of the node to delete 7 | * Return: 1 (Success), or -1 (Fail) 8 | */ 9 | int delete_nodeint_at_index(listint_t **head, unsigned int index) 10 | { 11 | listint_t *temp = *head; 12 | listint_t *current = NULL; 13 | unsigned int i = 0; 14 | 15 | if (*head == NULL) 16 | return (-1); 17 | if (index == 0) 18 | { 19 | *head = (*head)->next; 20 | free(temp); 21 | return (1); 22 | } 23 | 24 | while (i < index - 1) 25 | { 26 | if (!temp || !(temp->next)) 27 | return (-1); 28 | temp = temp->next; 29 | i++; 30 | } 31 | 32 | current = temp->next; 33 | temp->next = current->next; 34 | free(current); 35 | 36 | return (1); 37 | } 38 | -------------------------------------------------------------------------------- /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 | * Return: pointer to the first node in the new list 7 | */ 8 | listint_t *reverse_listint(listint_t **head) 9 | { 10 | listint_t *prev = NULL; 11 | listint_t *next = NULL; 12 | 13 | while (*head) 14 | { 15 | next = (*head)->next; 16 | (*head)->next = prev; 17 | prev = *head; 18 | *head = next; 19 | } 20 | 21 | *head = prev; 22 | 23 | return (*head); 24 | } 25 | -------------------------------------------------------------------------------- /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 | * Return: number of nodes in the list 7 | */ 8 | size_t print_listint_safe(const listint_t *head) 9 | { 10 | size_t num = 0; 11 | long int diff; 12 | 13 | while (head) 14 | { 15 | diff = head - head->next; 16 | num++; 17 | printf("[%p] %d\n", (void *)head, head->n); 18 | if (diff > 0) 19 | head = head->next; 20 | else 21 | { 22 | printf("-> [%p] %d\n", (void *)head->next, head->next->n); 23 | break; 24 | } 25 | } 26 | 27 | return (num); 28 | } 29 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/2-add_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_nodeint - adds a new node at the beginning of a listint_t list. 5 | * @head: pointer to the first node in the list 6 | * @n: data to insert in that new node 7 | * Return: pointer to the new node, or NULL if it fails 8 | */ 9 | listint_t *add_nodeint(listint_t **head, const int n) 10 | { 11 | listint_t *new; 12 | 13 | new = malloc(sizeof(listint_t)); 14 | if (!new) 15 | return (NULL); 16 | 17 | new->n = n; 18 | new->next = *head; 19 | *head = new; 20 | 21 | return (new); 22 | } 23 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/3-add_nodeint_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_nodeint_end - adds a new node at the end of a listint_t list. 5 | * @head: pointer to the first element in the list 6 | * @n: data to insert in the new element 7 | * Return: pointer to the new node, or NULL if it fails 8 | */ 9 | listint_t *add_nodeint_end(listint_t **head, const int n) 10 | { 11 | listint_t *new; 12 | listint_t *temp = *head; 13 | 14 | new = malloc(sizeof(listint_t)); 15 | if (!new) 16 | return (NULL); 17 | 18 | new->n = n; 19 | new->next = NULL; 20 | 21 | if (*head == NULL) 22 | { 23 | *head = new; 24 | return (new); 25 | } 26 | 27 | while (temp->next) 28 | temp = temp->next; 29 | 30 | temp->next = new; 31 | 32 | return (new); 33 | } 34 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/4-free_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_listint - frees a linked list 5 | * @head: listint_t list to be freed 6 | */ 7 | void free_listint(listint_t *head) 8 | { 9 | listint_t *temp; 10 | 11 | while (head) 12 | { 13 | temp = head->next; 14 | free(head); 15 | head = temp; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/5-free_listint2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "lists.h" 4 | 5 | /** 6 | * free_listint2 - frees a listint_t list 7 | * @head: double pointer to first element of list 8 | */ 9 | 10 | void free_listint2(listint_t **head) 11 | { 12 | listint_t *temp; 13 | 14 | if (head == NULL) 15 | return; 16 | while (*head) 17 | { 18 | temp = (*head)->next; 19 | free(*head); 20 | *head = temp; 21 | } 22 | *head = NULL; 23 | } 24 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/6-pop_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * pop_listint - deletes the head node of a linked list 5 | * @head: pointer to the first element in the linked list 6 | * Return: the data inside the elements that was deleted, 7 | * or 0 if the list is empty 8 | */ 9 | int pop_listint(listint_t **head) 10 | { 11 | listint_t *temp; 12 | int num; 13 | 14 | if (!head || !*head) 15 | return (0); 16 | 17 | num = (*head)->n; 18 | temp = (*head)->next; 19 | free(*head); 20 | *head = temp; 21 | 22 | return (num); 23 | } 24 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/7-get_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * get_nodeint_at_index - returns the node at a certain index in a linked list 5 | * @head: first node in the linked list 6 | * @index: index of the node to return 7 | * Return: pointer to the node we're looking for, or NULL 8 | */ 9 | listint_t *get_nodeint_at_index(listint_t *head, unsigned int index) 10 | { 11 | unsigned int i = 0; 12 | listint_t *temp = head; 13 | 14 | while (temp && i < index) 15 | { 16 | temp = temp->next; 17 | i++; 18 | } 19 | 20 | return (temp ? temp : NULL); 21 | } 22 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/8-sum_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * sum_listint - calculates the sum of all the data in a listint_t list 5 | * @head: first node in the linked list 6 | * Return: resulting sum 7 | */ 8 | int sum_listint(listint_t *head) 9 | { 10 | int sum = 0; 11 | listint_t *temp = head; 12 | 13 | while (temp) 14 | { 15 | sum += temp->n; 16 | temp = temp->next; 17 | } 18 | 19 | return (sum); 20 | } 21 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/9-insert_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | 4 | /** 5 | * insert_nodeint_at_index - inserts a new node in a linked list, 6 | * at a given position 7 | * @head: pointer to the first node in the list 8 | * @idx: index where the new node is added 9 | * @n: data to insert in the new node 10 | * Return: pointer to the new node, or NULL 11 | */ 12 | listint_t *insert_nodeint_at_index(listint_t **head, unsigned int idx, int n) 13 | { 14 | unsigned int i; 15 | listint_t *new; 16 | listint_t *temp = *head; 17 | 18 | new = malloc(sizeof(listint_t)); 19 | if (!new || !head) 20 | return (NULL); 21 | 22 | new->n = n; 23 | new->next = NULL; 24 | 25 | if (idx == 0) 26 | { 27 | new->next = *head; 28 | *head = new; 29 | return (new); 30 | } 31 | 32 | for (i = 0; temp && i < idx; i++) 33 | { 34 | if (i == idx - 1) 35 | { 36 | new->next = temp->next; 37 | temp->next = new; 38 | return (new); 39 | } 40 | else 41 | temp = temp->next; 42 | } 43 | 44 | return (NULL); 45 | } 46 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef LISTS_H 2 | #define LISTS_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | /** 9 | * struct listint_s - singly linked list 10 | * @n: integer 11 | * @next: points to the next node 12 | * Description: singly linked list node structure 13 | */ 14 | typedef struct listint_s 15 | { 16 | int n; 17 | struct listint_s *next; 18 | } listint_t; 19 | 20 | size_t print_listint(const listint_t *h); 21 | size_t listint_len(const listint_t *h); 22 | listint_t *add_nodeint(listint_t **head, const int n); 23 | listint_t *add_nodeint_end(listint_t **head, const int n); 24 | void free_listint(listint_t *head); 25 | void free_listint2(listint_t **head); 26 | int pop_listint(listint_t **head); 27 | listint_t *get_nodeint_at_index(listint_t *head, unsigned int index); 28 | int sum_listint(listint_t *head); 29 | listint_t *insert_nodeint_at_index(listint_t **head, unsigned int idx, int n); 30 | int delete_nodeint_at_index(listint_t **head, unsigned int index); 31 | listint_t *reverse_listint(listint_t **head); 32 | size_t print_listint_safe(const listint_t *head); 33 | 34 | #endif /*LISTS_H*/ 35 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/.3-set_bit.c.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x14-bit_manipulation/.3-set_bit.c.swp -------------------------------------------------------------------------------- /0x14-bit_manipulation/.main.h.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x14-bit_manipulation/.main.h.swp -------------------------------------------------------------------------------- /0x14-bit_manipulation/0-binary_to_uint.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * binary_to_uint - converts a binary number to an unsigned int. 5 | * @b: A pointer to a string of 0 and 1 chars. 6 | * Return: If b is NULL or contains chars not 0 or 1 - 0. 7 | * Otherwise - the converted number. 8 | */ 9 | unsigned int binary_to_uint(const char *b) 10 | { 11 | unsigned int num = 0; 12 | int len = 0; 13 | 14 | if (b[len] == '\0') 15 | return (0); 16 | 17 | while ((b[len] == '0') || (b[len] == '1')) 18 | { 19 | num <<= 1; 20 | num += b[len] - '0'; 21 | len++; 22 | } 23 | 24 | return (num); 25 | } 26 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/1-print_binary.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_binary - Prints the binary representation of a number. 5 | * @n: The number to be printed in binary. 6 | */ 7 | void print_binary(unsigned long int n) 8 | { 9 | if (n > 1) 10 | print_binary(n >> 1); 11 | 12 | _putchar((n & 1) + '0'); 13 | } 14 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/2-get_bit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * get_bit - Gets the value of a bit at a given index. 5 | * @n: The bit. 6 | * @index: The index to get the value at - indices start at 0. 7 | * Return: If an error occurs - -1. 8 | * Otherwise - The value of bit at index. 9 | */ 10 | int get_bit(unsigned long int n, unsigned int index) 11 | { 12 | if (index >= (sizeof(unsigned long int) * 8)) 13 | return (-1); 14 | 15 | if ((n & (1 << index)) == 0) 16 | return (0); 17 | 18 | return (1); 19 | } 20 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/3-set_bit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * set_bit - Sets the value of a bit at a given index to 1. 5 | * @n: A pointer to the bit. 6 | * @index: The index to set the value at - indices start at 0. 7 | * Return: If an error occurs - -1. 8 | * Otherwise - 1. 9 | */ 10 | int set_bit(unsigned long int *n, unsigned int index) 11 | { 12 | if (index >= (sizeof(unsigned long int) * 8)) 13 | return (-1); 14 | 15 | *n ^= (1 << index); 16 | 17 | return (1); 18 | } 19 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/4-clear_bit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * clear_bit - Sets the value of a bit at a given index to 0. 5 | * @n: A pointer to the bit. 6 | * @index: The index to set the value at - indices start at 0. 7 | * Return: If an error occurs - -1. 8 | * Otherwise - 1. 9 | */ 10 | int clear_bit(unsigned long int *n, unsigned int index) 11 | { 12 | if (index >= (sizeof(unsigned long int) * 8)) 13 | return (-1); 14 | 15 | *n &= ~(1 << index); 16 | 17 | return (1); 18 | } 19 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/5-flip_bits.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * flip_bits - Counts the number of bits needed to be 5 | * flipped to get from one number to another. 6 | * @n: The number. 7 | * @m: The number to flip n to. 8 | * Return: The necessary number of bits to flip to get from n to m. 9 | */ 10 | unsigned int flip_bits(unsigned long int n, unsigned long int m) 11 | { 12 | unsigned long int xor = n ^ m, bits = 0; 13 | 14 | while (xor > 0) 15 | { 16 | bits += (xor & 1); 17 | xor >>= 1; 18 | } 19 | 20 | return (bits); 21 | } 22 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/README.md: -------------------------------------------------------------------------------- 1 | `FILE 0-binary_to_uint.c` is a function that converts a binary number to an unsigned int. 2 | 3 | `FILE 1-print_binary.c` is a function that prints the binary representation of a number. 4 | 5 | `FILE 2-get_bit.c` is a function that returns the value of a bit at a given index. 6 | 7 | `FILE 3-set_bit.c` is a function that sets the value of a bit to 1 at a given index. 8 | 9 | `FILE 4-clear_bit.c` is a function that sets the value of a bit to 0 at a given index. 10 | 11 | `FILE 5-flip_bits.c` is a function that returns the number of bits you would need to flip to get from one number to another. 12 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _putchar - writes the character c to stdout 5 | * @c: The character to print 6 | * Return: On success 1. 7 | * On error, -1 is returned, and errno is set appropriately. 8 | */ 9 | int _putchar(char c) 10 | { 11 | return (write(1, &c, 1)); 12 | } 13 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x14-bit_manipulation/a -------------------------------------------------------------------------------- /0x14-bit_manipulation/b: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x14-bit_manipulation/b -------------------------------------------------------------------------------- /0x14-bit_manipulation/c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x14-bit_manipulation/c -------------------------------------------------------------------------------- /0x14-bit_manipulation/d: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x14-bit_manipulation/d -------------------------------------------------------------------------------- /0x14-bit_manipulation/e: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x14-bit_manipulation/e -------------------------------------------------------------------------------- /0x14-bit_manipulation/f: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x14-bit_manipulation/f -------------------------------------------------------------------------------- /0x14-bit_manipulation/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "main.h" 4 | 5 | 6 | 7 | /** 8 | * 9 | * * main - check the code 10 | * 11 | * * 12 | * 13 | * * Return: Always 0. 14 | * 15 | * */ 16 | 17 | int main(void) 18 | 19 | { 20 | 21 | unsigned int n; 22 | 23 | 24 | 25 | n = flip_bits(1024, 1); 26 | 27 | printf("%u\n", n); 28 | 29 | n = flip_bits(402, 98); 30 | 31 | printf("%u\n", n); 32 | 33 | n = flip_bits(1024, 3); 34 | 35 | printf("%u\n", n); 36 | 37 | n = flip_bits(1024, 1025); 38 | 39 | printf("%u\n", n); 40 | 41 | return (0); 42 | 43 | } 44 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | int _putchar(char c); 5 | unsigned int binary_to_uint(const char *b); 6 | void print_binary(unsigned long int n); 7 | int get_bit(unsigned long int n, unsigned int index); 8 | int set_bit(unsigned long int *n, unsigned int index); 9 | int clear_bit(unsigned long int *n, unsigned int index); 10 | unsigned int flip_bits(unsigned long int n, unsigned long int m); 11 | 12 | #endif /*MAIN_H*/ 13 | -------------------------------------------------------------------------------- /0x15-file_io/0-read_textfile.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * read_textfile - Reads a text file and prints it to POSIX stdout. 6 | * @filename: A pointer to the name of the file. 7 | * @letters: The number of letters the 8 | * function should read and print. 9 | * 10 | * Return: If the function fails or filename is NULL - 0. 11 | * O/w - the actual number of bytes the function can read and print. 12 | */ 13 | ssize_t read_textfile(const char *filename, size_t letters) 14 | { 15 | ssize_t o, r, w; 16 | char *buffer; 17 | 18 | if (filename == NULL) 19 | return (0); 20 | 21 | buffer = malloc(sizeof(char) * letters); 22 | if (buffer == NULL) 23 | return (0); 24 | 25 | o = open(filename, O_RDONLY); 26 | r = read(o, buffer, letters); 27 | w = write(STDOUT_FILENO, buffer, r); 28 | 29 | if (o == -1 || r == -1 || w == -1 || w != r) 30 | { 31 | free(buffer); 32 | return (0); 33 | } 34 | 35 | free(buffer); 36 | close(o); 37 | 38 | return (w); 39 | } 40 | -------------------------------------------------------------------------------- /0x15-file_io/1-create_file.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * create_file - Creates a file. 5 | * @filename: A pointer to the name of the file to create. 6 | * @text_content: A pointer to a string to write to the file. 7 | * 8 | * Return: If the function fails - -1. 9 | * Otherwise - 1. 10 | */ 11 | 12 | int create_file(const char *filename, char *text_content) 13 | { 14 | int o, w, len = 0; 15 | 16 | if (filename == NULL) 17 | return (-1); 18 | 19 | if (text_content != NULL) 20 | { 21 | for (len = 0; text_content[len];) 22 | len++; 23 | } 24 | 25 | o = open(filename, O_CREAT | O_RDWR | O_TRUNC, 0600); 26 | w = write(o, text_content, len); 27 | 28 | if (o == -1 || w == -1) 29 | return (-1); 30 | 31 | close(o); 32 | 33 | return (1); 34 | } 35 | -------------------------------------------------------------------------------- /0x15-file_io/2-append_text_to_file.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * append_text_to_file - Appends text at the end of a file. 5 | * @filename: A pointer to the name of the file. 6 | * @text_content: The string to add to the end of the file. 7 | * 8 | * Return: If the function fails or filename is NULL - -1. 9 | * If the file does not exist the user lacks write permissions - -1. 10 | * Otherwise - 1. 11 | */ 12 | 13 | int append_text_to_file(const char *filename, char *text_content) 14 | { 15 | int o, w, len = 0; 16 | 17 | if (filename == NULL) 18 | return (-1); 19 | 20 | if (text_content != NULL) 21 | { 22 | for (len = 0; text_content[len];) 23 | len++; 24 | } 25 | 26 | o = open(filename, O_WRONLY | O_APPEND); 27 | w = write(o, text_content, len); 28 | 29 | if (o == -1 || w == -1) 30 | return (-1); 31 | 32 | close(o); 33 | 34 | return (1); 35 | } 36 | -------------------------------------------------------------------------------- /0x15-file_io/README.md: -------------------------------------------------------------------------------- 1 | # Welcome to 0x15-file_io 2 | -------------------------------------------------------------------------------- /0x15-file_io/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | ssize_t read_textfile(const char *filename, size_t letters); 10 | int create_file(const char *filename, char *text_content); 11 | int append_text_to_file(const char *filename, char *text_content); 12 | 13 | #endif /* MAIN_H */ 14 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/0-print_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * print_dlistint - prints all the elements of a 5 | * dlistint_t list 6 | * @h: head of the list 7 | * Return: the number of nodes 8 | */ 9 | size_t print_dlistint(const dlistint_t *h) 10 | { 11 | int count; 12 | 13 | count = 0; 14 | 15 | if (h == NULL) 16 | return (count); 17 | 18 | while (h->prev != NULL) 19 | h = h->prev; 20 | 21 | while (h != NULL) 22 | { 23 | printf("%d\n", h->n); 24 | count++; 25 | h = h->next; 26 | } 27 | 28 | return (count); 29 | } 30 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/1-dlistint_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * dlistint_len - returns the number of elements in 5 | * a double linked list 6 | * @h: head of the list 7 | * Return: the number of nodes 8 | */ 9 | size_t dlistint_len(const dlistint_t *h) 10 | { 11 | int count; 12 | 13 | count = 0; 14 | 15 | if (h == NULL) 16 | return (count); 17 | 18 | while (h->prev != NULL) 19 | h = h->prev; 20 | 21 | while (h != NULL) 22 | { 23 | count++; 24 | h = h->next; 25 | } 26 | 27 | return (count); 28 | } 29 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/100-password: -------------------------------------------------------------------------------- 1 | en C Pyfo neZ 2 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/102-result: -------------------------------------------------------------------------------- 1 | 906609 -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/2-add_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_dnodeint - adds a new node at the beginning 5 | * of a dlistint_t list 6 | * @head: head of the list 7 | * @n: value of the element 8 | * Return: the address of the new element 9 | */ 10 | dlistint_t *add_dnodeint(dlistint_t **head, const int n) 11 | { 12 | dlistint_t *new; 13 | dlistint_t *h; 14 | 15 | new = malloc(sizeof(dlistint_t)); 16 | if (new == NULL) 17 | return (NULL); 18 | 19 | new->n = n; 20 | new->prev = NULL; 21 | h = *head; 22 | 23 | if (h != NULL) 24 | { 25 | while (h->prev != NULL) 26 | h = h->prev; 27 | } 28 | 29 | new->next = h; 30 | 31 | if (h != NULL) 32 | h->prev = new; 33 | 34 | *head = new; 35 | 36 | return (new); 37 | } 38 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/3-add_dnodeint_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_dnodeint_end - adds a new node at the end 5 | * of a dlistint_t list 6 | * @head: head of the list 7 | * @n: value of the element 8 | * Return: the address of the new element 9 | */ 10 | dlistint_t *add_dnodeint_end(dlistint_t **head, const int n) 11 | { 12 | dlistint_t *h; 13 | dlistint_t *new; 14 | 15 | new = malloc(sizeof(dlistint_t)); 16 | if (new == NULL) 17 | return (NULL); 18 | 19 | new->n = n; 20 | new->next = NULL; 21 | 22 | h = *head; 23 | 24 | if (h != NULL) 25 | { 26 | while (h->next != NULL) 27 | h = h->next; 28 | h->next = new; 29 | } 30 | else 31 | { 32 | *head = new; 33 | } 34 | new->prev = h; 35 | 36 | return (new); 37 | } 38 | 39 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/4-free_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_dlistint - frees a dlistint_t list 5 | * @head: head of the list 6 | * Return: no return 7 | * void free_dlistint(dlistint_t *head) 8 | */ 9 | void free_dlistint(dlistint_t *head) 10 | { 11 | dlistint_t *tmp; 12 | 13 | if (head != NULL) 14 | while (head->prev != NULL) 15 | head = head->prev; 16 | 17 | while ((tmp = head) != NULL) 18 | { 19 | head = head->next; 20 | free(tmp); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/5-get_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * get_dnodeint_at_index - returns the 5 | * nth node of a dlistint_t linked list 6 | * @head: head of the list 7 | * @index: index of the nth node 8 | * Return: nth node 9 | */ 10 | dlistint_t *get_dnodeint_at_index(dlistint_t *head, unsigned int index) 11 | { 12 | unsigned int i; 13 | 14 | 15 | if (head == NULL) 16 | return (NULL); 17 | 18 | while (head->prev != NULL) 19 | head = head->prev; 20 | 21 | 22 | i = 0; 23 | 24 | while (head != NULL) 25 | { 26 | if (i == index) 27 | break; 28 | head = head->next; 29 | i++; 30 | } 31 | 32 | return (head); 33 | } 34 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/6-sum_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * sum_dlistint - returns the sum of all the data (n) 5 | * of a doubly linked list 6 | * @head: head of the list 7 | * Return: sum of the data 8 | */ 9 | int sum_dlistint(dlistint_t *head) 10 | { 11 | int sum; 12 | 13 | sum = 0; 14 | 15 | if (head != NULL) 16 | { 17 | while (head->prev != NULL) 18 | head = head->prev; 19 | 20 | while (head != NULL) 21 | { 22 | sum += head->n; 23 | head = head->next; 24 | } 25 | } 26 | 27 | return (sum); 28 | } 29 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/8-delete_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * delete_dnodeint_at_index - deletes the node at index of a 5 | * dlistint_t linked list 6 | * @head: head of the list 7 | * @index: index of the new node 8 | * Return: 1 if it succeeded, -1 if it failed 9 | */ 10 | int delete_dnodeint_at_index(dlistint_t **head, unsigned int index) 11 | { 12 | dlistint_t *h1; 13 | dlistint_t *h2; 14 | unsigned int i; 15 | 16 | h1 = *head; 17 | 18 | if (h1 != NULL) 19 | while (h1->prev != NULL) 20 | h1 = h1->prev; 21 | 22 | i = 0; 23 | 24 | while (h1 != NULL) 25 | { 26 | if (i == index) 27 | { 28 | if (i == 0) 29 | { 30 | *head = h1->next; 31 | if (*head != NULL) 32 | (*head)->prev = NULL; 33 | } 34 | else 35 | { 36 | h2->next = h1->next; 37 | 38 | if (h1->next != NULL) 39 | h1->next->prev = h2; 40 | } 41 | 42 | free(h1); 43 | return (1); 44 | 45 | } 46 | h2 = h1; 47 | h1 = h1->next; 48 | i++; 49 | } 50 | return (-1); 51 | 52 | } 53 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/README.md: -------------------------------------------------------------------------------- 1 | File: 0-print_dlistint.c is a function that prints all the elements of a dlistint_t list. 2 | 3 | File: 1-dlistint_len.c is a function that returns the number of elements in a linked dlistint_t list. 4 | 5 | File: 2-add_dnodeint.c is a function that adds a new node at the beginning of a dlistint_t list. 6 | 7 | File: 3-add_dnodeint_end.c is a function that adds a new node at the end of a dlistint_t list. 8 | 9 | File: 4-free_dlistint.c is a function that frees a dlistint_t list. 10 | 11 | File: 5-get_dnodeint.c is a function that returns the nth node of a dlistint_t linked list. 12 | 13 | File: 6-sum_dlistint.c is a function that returns the sum of all the data (n) of a dlistint_t linked list. 14 | 15 | File: 7-insert_dnodeint.c is a function that inserts a new node at a given position. 16 | 17 | File: 8-delete_dnodeint.c is a function that deletes the node at index index of a dlistint_t linked list. 18 | 19 | Authour: @Barissa-Imran -------------------------------------------------------------------------------- /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 | * Description: doubly linked list node structure 13 | * for Holberton project 14 | */ 15 | typedef struct dlistint_s 16 | { 17 | int n; 18 | struct dlistint_s *prev; 19 | struct dlistint_s *next; 20 | } dlistint_t; 21 | 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 | 32 | #endif 33 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/.gitignore: -------------------------------------------------------------------------------- 1 | *.c 2 | *.o 3 | len 4 | 100-tests.py 5 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/1-create_dynamic_lib.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -c -fPIC *.c 3 | gcc -shared -o liball.so *.o 4 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/100-operations.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x18-dynamic_libraries/100-operations.so -------------------------------------------------------------------------------- /0x18-dynamic_libraries/101-make_me_wish.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | wget -q -O /tmp/libtest.so https://github.com/MitaliSengupta/holbertonschool-low_level_programming/raw/master/0x17-dynamic_libraries/libtest.so 3 | export LD_PRELOAD=/tmp/libtest.so 4 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/README.md: -------------------------------------------------------------------------------- 1 | # 0x18. C - Dynamic libraries 2 | 3 | In this project, i learnt about what dynamic library is, how it works, how to create one and how it works. It also helped me understand the differences between static and shared libraries. 4 | 5 | ## Function Prototypes :floppy_disk: 6 | The following files are scripts written for the projects written in C: 7 | 8 | 9 | ## Table of contents 10 | Files | Description 11 | ----- | ----------- 12 | [libdynamic.so](./libdynamic.so) | C dynamic library containing the function definitions 13 | [main.h](./main.h) | Header files containing the function prototypes 14 | [1-create_dynamic_lib.sh](./1-create_dynamic_lib.sh) | Bash script that creates a dynamic library called liball.so from all the .c files that are in the current directory 15 | [100-operations.so](./100-operations.so) | C dynamic library that contains C functions that can be called from Python 16 | [random.so](./random.so) | C dynamic library to inject in a giga million program 17 | [101-make_me_win.sh](./101-make_me_win.sh) | Bash script to inject the libmask.so library, using LD_PRELOAD, in the giga million program 18 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/liball.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x18-dynamic_libraries/liball.so -------------------------------------------------------------------------------- /0x18-dynamic_libraries/libdynamic.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x18-dynamic_libraries/libdynamic.so -------------------------------------------------------------------------------- /0x18-dynamic_libraries/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | #define EXIT_SUCCESS 0 5 | 6 | #include 7 | 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 | 29 | #endif /* MAIN_H */ 30 | -------------------------------------------------------------------------------- /0x1A-hash_tables/0-hash_table_create.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_create - creates a hash table 5 | * @size: size of the array 6 | * 7 | * Return: pointer to 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 i; 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 (i = 0; i < size; i++) 25 | hash_table->array[i] = NULL; 26 | return (hash_table); 27 | } 28 | -------------------------------------------------------------------------------- /0x1A-hash_tables/1-djb2.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_djb2 - implementation of the djb2 algorithm 5 | * @str: string used to generate hash value 6 | * 7 | * Return: hash value 8 | */ 9 | unsigned long int hash_djb2(const unsigned char *str) 10 | { 11 | unsigned long int hash; 12 | int c; 13 | 14 | hash = 5381; 15 | while ((c = *str++)) 16 | { 17 | hash = ((hash << 5) + hash) + c; /* hash * 33 + c */ 18 | } 19 | return (hash); 20 | } 21 | -------------------------------------------------------------------------------- /0x1A-hash_tables/2-key_index.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * key_index - gives the index of a key 5 | * @key: key to get index for 6 | * @size: size of the hash table 7 | * 8 | * Return: index for the key 9 | */ 10 | unsigned long int key_index(const unsigned char *key, unsigned long int size) 11 | { 12 | return (hash_djb2(key) % size); 13 | } 14 | -------------------------------------------------------------------------------- /0x1A-hash_tables/4-hash_table_get.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_get - retrieves a value associated with a key 5 | * @ht: table to retrieve value from 6 | * @key: key to find value 7 | * 8 | * Return: value associated with key, or NULL if key cannot be found 9 | */ 10 | char *hash_table_get(const hash_table_t *ht, const char *key) 11 | { 12 | unsigned long int index; 13 | hash_node_t *tmp; 14 | 15 | if (ht == NULL || ht->array == NULL || ht->size == 0 || 16 | key == NULL || strlen(key) == 0) 17 | return (NULL); 18 | index = key_index((const unsigned char *)key, ht->size); 19 | tmp = ht->array[index]; 20 | while (tmp != NULL) 21 | { 22 | if (strcmp(tmp->key, key) == 0) 23 | return (tmp->value); 24 | tmp = tmp->next; 25 | } 26 | return (NULL); 27 | } 28 | -------------------------------------------------------------------------------- /0x1A-hash_tables/5-hash_table_print.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_print - prints a hash table 5 | * @ht: hash table to print 6 | * 7 | * Return: void 8 | */ 9 | void hash_table_print(const hash_table_t *ht) 10 | { 11 | unsigned long int i; 12 | hash_node_t *tmp; 13 | char flag = 0; /* 0 while no data has been printed yet */ 14 | 15 | if (ht == NULL || ht->array == NULL) 16 | return; 17 | printf("{"); 18 | for (i = 0; i < ht->size; i++) 19 | { 20 | tmp = ht->array[i]; 21 | while (tmp != NULL) 22 | { 23 | if (flag == 1) 24 | printf(", "); 25 | printf("'%s': '%s'", tmp->key, tmp->value); 26 | flag = 1; 27 | tmp = tmp->next; 28 | } 29 | } 30 | printf("}\n"); 31 | } 32 | -------------------------------------------------------------------------------- /0x1A-hash_tables/6-hash_table_delete.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_delete - deletes a hash table 5 | * @ht: hash table to delete 6 | * 7 | * Return: void 8 | */ 9 | void hash_table_delete(hash_table_t *ht) 10 | { 11 | unsigned long int i; 12 | hash_node_t *next; 13 | 14 | if (ht == NULL || ht->array == NULL || ht->size == 0) 15 | return; 16 | for (i = 0; i < ht->size; i++) 17 | { 18 | while (ht->array[i] != NULL) 19 | { 20 | next = ht->array[i]->next; 21 | free(ht->array[i]->key); 22 | free(ht->array[i]->value); 23 | free(ht->array[i]); 24 | ht->array[i] = next; 25 | } 26 | } 27 | free(ht->array); 28 | ht->array = NULL; 29 | ht->size = 0; 30 | free(ht); 31 | } 32 | -------------------------------------------------------------------------------- /0x1A-hash_tables/README.md: -------------------------------------------------------------------------------- 1 | 0x1A. C - Hash tables 2 | -------------------------------------------------------------------------------- /0x1C-makefiles/0-Makefile: -------------------------------------------------------------------------------- 1 | all : main.c school.c 2 | gcc main.c school.c -o school 3 | -------------------------------------------------------------------------------- /0x1C-makefiles/1-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | 4 | all: $(SRC) 5 | $(CC) $(SRC) -o school 6 | -------------------------------------------------------------------------------- /0x1C-makefiles/100-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | OBJ = $(SRC:.c=.o) 4 | NAME = school 5 | CFLAGS = -Wall -Werror -Wextra -pedantic 6 | 7 | .PHONY: all clean oclean fclean re 8 | 9 | all: m.h $(OBJ) 10 | $(CC) $(OBJ) -o $(NAME) 11 | 12 | clean: 13 | $(RM) *~ $(NAME) 14 | 15 | oclean: 16 | $(RM) $(OBJ) 17 | 18 | fclean: clean oclean 19 | 20 | re: fclean all 21 | -------------------------------------------------------------------------------- /0x1C-makefiles/2-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | OBJ = $(SRC:.c=.o) 4 | NAME = school 5 | 6 | all: $(OBJ) 7 | $(CC) $(OBJ) -o $(NAME) 8 | -------------------------------------------------------------------------------- /0x1C-makefiles/3-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | OBJ = $(SRC:.c=.o) 4 | NAME = school 5 | RM = rm -f 6 | 7 | all: $(OBJ) 8 | $(CC) $(OBJ) -o $(NAME) 9 | 10 | clean: 11 | $(RM) *~ $(NAME) 12 | 13 | oclean: 14 | $(RM) $(OBJ) 15 | 16 | fclean: clean oclean 17 | 18 | re: fclean all 19 | -------------------------------------------------------------------------------- /0x1C-makefiles/4-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | OBJ = $(SRC:.c=.o) 4 | NAME = school 5 | RM = rm -f 6 | CFLAGS = -Wall -Werror -Wextra -pedantic 7 | 8 | all: $(OBJ) 9 | $(CC) $(CLFAGS) $(OBJ) -o $(NAME) 10 | 11 | clean: 12 | $(RM) *~ $(NAME) 13 | 14 | oclean: 15 | $(RM) $(OBJ) 16 | 17 | fclean: clean oclean 18 | 19 | re: fclean all 20 | -------------------------------------------------------------------------------- /0x1C-makefiles/5-island_perimeter.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | """Defines an island perimeter measuring function.""" 3 | 4 | 5 | def island_perimeter(grid): 6 | """Return the perimiter of an island. 7 | The grid represents water by 0 and land by 1. 8 | Args: 9 | grid (list): A list of list of integers representing an island. 10 | Returns: 11 | The perimeter of the island defined in grid. 12 | """ 13 | width = len(grid[0]) 14 | height = len(grid) 15 | edges = 0 16 | size = 0 17 | 18 | for i in range(height): 19 | for j in range(width): 20 | if grid[i][j] == 1: 21 | size += 1 22 | if (j > 0 and grid[i][j - 1] == 1): 23 | edges += 1 24 | if (i > 0 and grid[i - 1][j] == 1): 25 | edges += 1 26 | return size * 4 - edges * 2 27 | -------------------------------------------------------------------------------- /0x1C-makefiles/README.md: -------------------------------------------------------------------------------- 1 | # Welcome to 0x1C-makefiles 2 | -------------------------------------------------------------------------------- /0x1C-makefiles/m.h: -------------------------------------------------------------------------------- 1 | #ifndef __M_H__ 2 | #define __M_H__ 3 | 4 | #include 5 | #include 6 | 7 | void print_school(void); 8 | 9 | #endif 10 | -------------------------------------------------------------------------------- /0x1C-makefiles/main.c: -------------------------------------------------------------------------------- 1 | #include "m.h" 2 | 3 | /** 4 | * main - Entry point 5 | * 6 | * Return: Always 7 | */ 8 | int main(void) 9 | { 10 | print_school(); 11 | return (EXIT_SUCCESS); 12 | } 13 | /* school */ 14 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/0-linear.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | #include 3 | 4 | /** 5 | * linear_search - searches for a value in an array of 6 | * integers using the Linear search algorithm 7 | * @array: array to search the value in 8 | * @size: size of the array 9 | * @value: value to look for 10 | * 11 | * Return: the index of the found value, 12 | * or -1 if not found 13 | */ 14 | int linear_search(int *array, size_t size, int value) 15 | { 16 | size_t i = 0; 17 | 18 | if (!array || size == 0) 19 | return (-1); 20 | while (i < size) 21 | { 22 | printf("Value checked array[%lu] = [%d]\n", i, array[i]); 23 | if (array[i] == value) 24 | return (i); 25 | i++; 26 | } 27 | return (-1); 28 | } 29 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/1-binary: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x1E-search_algorithms/1-binary -------------------------------------------------------------------------------- /0x1E-search_algorithms/1-binary.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * binary_search - Searches for a value in a sorted array 5 | * of intergers using binary search. 6 | * @array: a pointer to the first element of the array to search 7 | * @size: the number of elements in array 8 | * @value: the value to search for 9 | * Return: if the value is not present or the array is Null, -1, 10 | * otherwise, the index where the value is located 11 | */ 12 | int binary_search(int *array, size_t size, int value) 13 | { 14 | int i, left, right; 15 | 16 | if (array == NULL) 17 | return (-1); 18 | 19 | left = 0; 20 | right = size - 1; 21 | 22 | while (left <= right) 23 | { 24 | int mid = left + (right - left) / 2; 25 | 26 | printf("Searching in array: "); 27 | for (i = left; i <= right; i++) 28 | { 29 | if (i == left) 30 | printf("%d", array[i]); 31 | else 32 | printf(", %d", array[i]); 33 | } 34 | printf("\n"); 35 | 36 | if (array[mid] == value) 37 | return (mid); 38 | else if (array[mid] < value) 39 | left = mid + 1; 40 | else 41 | right = mid - 1; 42 | } 43 | 44 | return (-1); 45 | } 46 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/100-jump.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | /** 3 | * jump_search - searches for a value in a sorted array 4 | * of integers using the Jump search algorithm 5 | * @array: pointer to the first element of the array to search 6 | * @size: the number of elements in array 7 | * @value: the value to search for 8 | * Return: If value is not present in array or if array is NULL, 9 | * return -1 else the first index where value is located. 10 | */ 11 | int jump_search(int *array, size_t size, int value) 12 | { 13 | size_t i, jump, step; 14 | 15 | if (array == NULL || size == 0) 16 | return (-1); 17 | 18 | step = sqrt(size); 19 | for (i = jump = 0; jump < size && array[jump] < value;) 20 | { 21 | printf("Value checked array[%ld] = [%d]\n", jump, array[jump]); 22 | i = jump; 23 | jump += step; 24 | } 25 | 26 | printf("Value found between indexes [%ld] and [%ld]\n", i, jump); 27 | 28 | jump = jump < size - 1 ? jump : size - 1; 29 | for (; i < jump && array[i] < value; i++) 30 | printf("Value checked array[%ld] = [%d]\n", i, array[i]); 31 | printf("Value checked array[%ld] = [%d]\n", i, array[i]); 32 | 33 | return (array[i] == value ? (int)i : -1); 34 | } 35 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/100-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "search_algos.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always EXIT_SUCCESS 9 | */ 10 | int main(void) 11 | { 12 | int array[] = { 13 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 14 | }; 15 | size_t size = sizeof(array) / sizeof(array[0]); 16 | 17 | printf("Found %d at index: %d\n\n", 6, jump_search(array, size, 6)); 18 | printf("Found %d at index: %d\n\n", 1, jump_search(array, size, 1)); 19 | printf("Found %d at index: %d\n", 999, jump_search(array, size, 999)); 20 | return (EXIT_SUCCESS); 21 | } 22 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/101-O: -------------------------------------------------------------------------------- 1 | O(sqrt(n)) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/102-interpolation: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/0x1E-search_algorithms/102-interpolation -------------------------------------------------------------------------------- /0x1E-search_algorithms/102-interpolation.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * interpolation_search - searches for a value in a sorted array of integers 5 | * @array: pointer to the first element of the array to search 6 | * @size: the number of elements in array 7 | * @value: the value to search 8 | * Return: If value is not present in array or if array is NULL, must return -1 9 | * else, the first index where value is located. 10 | */ 11 | int interpolation_search(int *array, size_t size, int value) 12 | { 13 | size_t i, l, r; 14 | 15 | if (array == NULL) 16 | return (-1); 17 | 18 | for (l = 0, r = size - 1; r >= 1;) 19 | { 20 | i = l + (((double)(r - l) / (array[r] - array[l])) * (value - array[l])); 21 | if (i < size) 22 | printf("Value checked array[%ld] = [%d]\n", i, array[i]); 23 | else 24 | { 25 | printf("Value checked array[%ld] is out of range\n", i); 26 | break; 27 | } 28 | 29 | if (array[i] == value) 30 | return (i); 31 | if (array[i] > value) 32 | r = i - 1; 33 | else 34 | l = i + 1; 35 | } 36 | 37 | return (-1); 38 | } 39 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/102-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "search_algos.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always EXIT_SUCCESS 9 | */ 10 | int main(void) 11 | { 12 | int array[] = { 13 | 0, 0, 1, 2, 2, 2, 2, 3, 3, 4, 4, 5, 6, 6, 7, 8, 8, 8, 9, 9 14 | }; 15 | size_t size = sizeof(array) / sizeof(array[0]); 16 | 17 | printf("Found %d at index: %d\n\n", 3, interpolation_search(array, size, 3)); 18 | printf("Found %d at index: %d\n\n", 7, interpolation_search(array, size, 7)); 19 | printf("Found %d at index: %d\n", 999, interpolation_search(array, size, 999)); 20 | return (EXIT_SUCCESS); 21 | } 22 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/107-O: -------------------------------------------------------------------------------- 1 | O(n) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/108-O: -------------------------------------------------------------------------------- 1 | O(sqrt(n)) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/2-O: -------------------------------------------------------------------------------- 1 | O(n) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/3-O: -------------------------------------------------------------------------------- 1 | O(1) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/4-O: -------------------------------------------------------------------------------- 1 | O(log(n)) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/5-O: -------------------------------------------------------------------------------- 1 | O(1) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/6-O: -------------------------------------------------------------------------------- 1 | O(nm) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/README.md: -------------------------------------------------------------------------------- 1 | # Welcome to 0x1E-search_algorithms 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/listint/create_list.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "search_algos.h" 3 | 4 | void free_list(listint_t *list); 5 | 6 | /** 7 | * create_list - Creates a single linked list 8 | * 9 | * @array: Pointer to the array to use to fill the list 10 | * @size: Size of the array 11 | * 12 | * Return: A pointer to the head of the created list (NULL on failure) 13 | */ 14 | listint_t *create_list(int *array, size_t size) 15 | { 16 | listint_t *list; 17 | listint_t *node; 18 | 19 | list = NULL; 20 | while (array && size--) 21 | { 22 | node = malloc(sizeof(*node)); 23 | if (!node) 24 | { 25 | free_list(list); 26 | return (NULL); 27 | } 28 | node->n = array[size]; 29 | node->index = size; 30 | node->next = list; 31 | list = node; 32 | } 33 | return (list); 34 | } 35 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/listint/free_list.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "search_algos.h" 3 | 4 | /** 5 | * free_list - Deallocates a singly linked list 6 | * 7 | * @list: Pointer to the linked list to be freed 8 | */ 9 | void free_list(listint_t *list) 10 | { 11 | listint_t *node; 12 | 13 | if (list) 14 | { 15 | node = list->next; 16 | free(list); 17 | free_list(node); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/listint/print_list.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "search_algos.h" 3 | 4 | /** 5 | * print_list - Prints the content of a listint_t 6 | * 7 | * @list: Pointer to the head of the list 8 | */ 9 | void print_list(const listint_t *list) 10 | { 11 | printf("List :\n"); 12 | while (list) 13 | { 14 | printf("Index[%lu] = [%d]\n", list->index, list->n); 15 | list = list->next; 16 | } 17 | printf("\n"); 18 | } 19 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/skiplist/free_skiplist.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "search_algos.h" 3 | 4 | /** 5 | * free_skiplist - Deallocates a singly linked list 6 | * 7 | * @list: Pointer to the linked list to be freed 8 | */ 9 | void free_skiplist(skiplist_t *list) 10 | { 11 | skiplist_t *node; 12 | 13 | if (list) 14 | { 15 | node = list->next; 16 | free(list); 17 | free_skiplist(node); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/skiplist/print_skiplist.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "search_algos.h" 4 | 5 | /** 6 | * print_skiplist - dump the content of a skiplist_t 7 | * 8 | * @list: Pointer to the head of the list 9 | * 10 | * Return: void 11 | */ 12 | void print_skiplist(const skiplist_t *list) 13 | { 14 | const skiplist_t *node; 15 | 16 | printf("List :\n"); 17 | for (node = list; node; node = node->next) 18 | { 19 | printf("Index[%lu] = [%d]\n", node->index, node->n); 20 | } 21 | printf("\nExpress lane :\n"); 22 | for (node = list; node; node = node->express) 23 | { 24 | printf("Index[%lu] = [%d]\n", node->index, node->n); 25 | } 26 | printf("\n"); 27 | } 28 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/test/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "search_algos.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always EXIT_SUCCESS 9 | */ 10 | int main(void) 11 | { 12 | int array[] = { 13 | 10, 1, 42, 3, 4, 42, 6, 7, -1, 9 14 | }; 15 | size_t size = sizeof(array) / sizeof(array[0]); 16 | 17 | printf("Found %d at index: %d\n\n", 3, linear_search(array, size, 3)); 18 | printf("Found %d at index: %d\n\n", 42, linear_search(array, size, 42)); 19 | printf("Found %d at index: %d\n", 999, linear_search(array, size, 999)); 20 | return (EXIT_SUCCESS); 21 | } 22 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/test/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "search_algos.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always EXIT_SUCCESS 9 | */ 10 | int main(void) 11 | { 12 | int array[] = { 13 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 14 | }; 15 | size_t size = sizeof(array) / sizeof(array[0]); 16 | 17 | printf("Found %d at index: %d\n\n", 2, binary_search(array, size, 2)); 18 | printf("Found %d at index: %d\n\n", 5, binary_search(array, 5, 5)); 19 | printf("Found %d at index: %d\n", 999, binary_search(array, size, 999)); 20 | return (EXIT_SUCCESS); 21 | } 22 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/test/103-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "search_algos.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always EXIT_SUCCESS 9 | */ 10 | int main(void) 11 | { 12 | int array[] = { 13 | 0, 1, 2, 3, 4, 7, 12, 15, 18, 19, 23, 54, 61, 62, 76, 99 14 | }; 15 | size_t size = sizeof(array) / sizeof(array[0]); 16 | 17 | printf("Found %d at index: %d\n\n", 62, exponential_search(array, size, 62)); 18 | printf("Found %d at index: %d\n\n", 3, exponential_search(array, size, 3)); 19 | printf("Found %d at index: %d\n", 999, exponential_search(array, size, 999)); 20 | return (EXIT_SUCCESS); 21 | } 22 | 23 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/test/104-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "search_algos.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always EXIT_SUCCESS 9 | */ 10 | int main(void) 11 | { 12 | int array[] = { 13 | 0, 1, 2, 5, 5, 6, 6, 7, 8, 9 14 | }; 15 | size_t size = sizeof(array) / sizeof(array[0]); 16 | 17 | printf("Found %d at index: %d\n\n", 8, advanced_binary(array, size, 8)); 18 | printf("Found %d at index: %d\n\n", 5, advanced_binary(array, size, 5)); 19 | printf("Found %d at index: %d\n", 999, advanced_binary(array, size, 999)); 20 | return (EXIT_SUCCESS); 21 | } 22 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/test/105-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "search_algos.h" 4 | 5 | listint_t *create_list(int *array, size_t size); 6 | void print_list(const listint_t *list); 7 | void free_list(listint_t *list); 8 | 9 | /** 10 | * main - Entry point 11 | * 12 | * Return: Always EXIT_SUCCESS 13 | */ 14 | int main(void) 15 | { 16 | listint_t *list, *res; 17 | int array[] = { 18 | 0, 1, 2, 3, 4, 7, 12, 15, 18, 19, 23, 53, 61, 62, 76, 99 19 | }; 20 | size_t size = sizeof(array) / sizeof(array[0]); 21 | 22 | list = create_list(array, size); 23 | print_list(list); 24 | 25 | res = jump_list(list, size, 53); 26 | printf("Found %d at index: %lu\n\n", 53, res->index); 27 | res = jump_list(list, size, 2); 28 | printf("Found %d at index: %lu\n\n", 2, res->index); 29 | res = jump_list(list, size, 999); 30 | printf("Found %d at index: %p\n", 999, (void *) res); 31 | 32 | free_list(list); 33 | return (EXIT_SUCCESS); 34 | } 35 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/test/106-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "search_algos.h" 4 | 5 | skiplist_t *create_skiplist(int *array, size_t size); 6 | void print_skiplist(const skiplist_t *list); 7 | void free_skiplist(skiplist_t *list); 8 | 9 | /** 10 | * main - Entry point 11 | * 12 | * Return: Always EXIT_SUCCESS 13 | */ 14 | int main(void) 15 | { 16 | skiplist_t *list, *res; 17 | int array[] = { 18 | 0, 1, 2, 3, 4, 7, 12, 15, 18, 19, 23, 53, 61, 62, 76, 99 19 | }; 20 | size_t size = sizeof(array) / sizeof(array[0]); 21 | 22 | list = create_skiplist(array, size); 23 | print_skiplist(list); 24 | 25 | res = linear_skip(list, 53); 26 | printf("Found %d at index: %lu\n\n", 53, res->index); 27 | res = linear_skip(list, 2); 28 | printf("Found %d at index: %lu\n\n", 2, res->index); 29 | res = linear_skip(list, 999); 30 | printf("Found %d at index: %p\n", 999, (void *) res); 31 | 32 | free_skiplist(list); 33 | return (EXIT_SUCCESS); 34 | } 35 | -------------------------------------------------------------------------------- /test: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Barissa-Imran/alx-low_level_programming/b1fc989618ad516fee1c10045a3152a2be7a9b64/test -------------------------------------------------------------------------------- /test.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main (void) 4 | { 5 | int i; 6 | 7 | for (i = 48; i < 58; i++) 8 | { 9 | printf("%c", i); 10 | } 11 | } 12 | --------------------------------------------------------------------------------