├── .gitignore ├── .gitmodules ├── 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 ├── 10-print_comb2.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 ├── .gitignore ├── 0-holberton.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 ├── _putchar.c └── holberton.h ├── 0x03-more_functions_nested_loops ├── 0-isupper.c ├── 1-isdigit.c ├── 10-print_triangle.c ├── 100-prime_factor.c ├── 101-print_number.c ├── 2-mul.c ├── 3-print_numbers.c ├── 4-print_most_numbers.c ├── 5-more_numbers.c ├── 6-print_line.c ├── 7-print_diagonal.c ├── 8-print_square.c ├── 9-fizz_buzz.c ├── README.md ├── _putchar.c └── holberton.h ├── 0x04-pointers_arrays_strings ├── 0-main.c ├── 0-reset_to_98.c ├── 1-main.c ├── 1-swap.c ├── 100-atoi.c ├── 100-main.c ├── 2-main.c ├── 2-strlen.c ├── 3-main.c ├── 3-puts.c ├── 4-main.c ├── 4-print_rev.c ├── 5-main.c ├── 5-rev_string.c ├── 6-main.c ├── 6-puts2.c ├── 7-main.c ├── 7-puts_half.c ├── 8-main.c ├── 8-print_array.c ├── 9-main.c ├── 9-strcpy.c ├── README.md ├── _putchar.c └── holberton.h ├── 0x05-pointers_arrays_strings ├── 0-main.c ├── 0-strcat.c ├── 1-main.c ├── 1-strncat.c ├── 100-main.c ├── 100-print_number.c ├── 101-magic.c ├── 2-main.c ├── 2-strncpy.c ├── 3-main.c ├── 3-strcmp.c ├── 4-main.c ├── 4-rev_array.c ├── 5-main.c ├── 5-string_toupper.c ├── 6-cap_string.c ├── 6-main.c ├── 7-leet.c ├── 7-main.c ├── 8-main.c ├── 8-rot13.c ├── README.md ├── _putchar.c └── holberton.h ├── 0x06-pointers_arrays_strings ├── 0-main.c ├── 0-memset.c ├── 1-main.c ├── 1-memcpy.c ├── 101-crackme_password ├── 2-main.c ├── 2-strchr.c ├── 3-main.c ├── 3-strspn.c ├── 4-main.c ├── 4-strpbrk.c ├── 5-main.c ├── 5-strstr.c ├── 7-main.c ├── 7-print_chessboard.c ├── 8-main.c ├── 8-print_diagsums.c ├── 9-main.c ├── 9-set_string.c ├── README.md ├── _putchar.c ├── crackme2 └── holberton.h ├── 0x07-recursion ├── 0-puts_recursion.c ├── 1-print_rev_recursion.c ├── 100-wildcmp.c ├── 100-wildcmp_NONRECURSIVE.c ├── 2-strlen_recursion.c ├── 3-factorial.c ├── 4-pow_recursion.c ├── 5-sqrt_recursion.c ├── 6-is_prime_number.c ├── 7-is_palindrome.c ├── README.md ├── _putchar.c ├── holberton.h └── test_files │ ├── 0-main.c │ ├── 1-main.c │ ├── 100-main.c │ ├── 2-main.c │ ├── 3-main.c │ ├── 4-main.c │ ├── 5-main.c │ ├── 6-main.c │ └── 7-main.c ├── 0x08-static_libraries ├── .gitignore ├── README.md ├── create_static_lib.sh ├── holberton.h └── libholberton.a ├── 0x09-argc_argv ├── 0-whatsmyname.c ├── 1-args.c ├── 100-change.c ├── 2-args.c ├── 3-mul.c ├── 4-add.c ├── README.md ├── _putchar.c └── holberton.h ├── 0x0A-malloc_free ├── 0-create_array.c ├── 0-main.c ├── 1-main.c ├── 1-strdup.c ├── 2-main.c ├── 2-str_concat.c ├── 3-alloc_grid.c ├── 3-main.c ├── 4-free_grid.c ├── 4-main.c ├── 5-argstostr.c ├── 5-main.c ├── README.md ├── _putchar.c └── holberton.h ├── 0x0B-more_malloc_free ├── 0-main.c ├── 0-malloc_checked.c ├── 1-main.c ├── 1-string_nconcat.c ├── 100-main.c ├── 100-realloc.c ├── 2-calloc.c ├── 2-main.c ├── 3-array_range.c ├── 3-main.c ├── README.md └── holberton.h ├── 0x0C-preprocessor ├── 0-main.c ├── 0-object_like_macro.h ├── 1-main.c ├── 1-pi.h ├── 2-main.c ├── 3-function_like_macro.h ├── 3-main.c ├── 4-main.c ├── 4-sum.h └── README.md ├── 0x0D-structures_typedef ├── 0-main.c ├── 1-init_dog.c ├── 1-main.c ├── 2-main.c ├── 2-print_dog.c ├── 3-main.c ├── 4-main.c ├── 4-new_dog.c ├── 5-free_dog.c ├── 5-main.c ├── README.md └── dog.h ├── 0x0E-function_pointers ├── 0-main.c ├── 0-print_name.c ├── 1-array_iterator.c ├── 1-main.c ├── 2-int_index.c ├── 2-main.c ├── 3-calc.h ├── 3-get_op_func.c ├── 3-main.c ├── 3-op_functions.c ├── README.md └── function_pointers.h ├── 0x0F-variadic_functions ├── 0-main.c ├── 0-sum_them_all.c ├── 1-main.c ├── 1-print_numbers.c ├── 2-main.c ├── 2-print_strings.c ├── 3-main.c ├── 3-print_all.c ├── README.md └── variadic_functions.h ├── 0x11-singly_linked_lists ├── 0-main.c ├── 0-print_list.c ├── 1-list_len.c ├── 1-main.c ├── 100-first.c ├── 100-main.c ├── 2-add_node.c ├── 2-main.c ├── 3-add_node_end.c ├── 3-main.c ├── 4-free_list.c ├── 4-main.c ├── README.md └── lists.h ├── 0x12-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 ├── 103-find_loop.c ├── 2-add_nodeint.c ├── 3-add_nodeint_end.c ├── 4-free_listint.c ├── 5-free_listint2.c ├── 6-pop_listint.c ├── 7-get_nodeint.c ├── 8-sum_listint.c ├── 9-insert_nodeint.c ├── README.md ├── lists.h └── main_test_files │ ├── 0-main.c │ ├── 1-main.c │ ├── 10-main.c │ ├── 100-main.c │ ├── 101-main.c │ ├── 103-main.c │ ├── 2-main.c │ ├── 3-main.c │ ├── 4-main.c │ ├── 5-main.c │ ├── 6-main.c │ ├── 7-main.c │ ├── 8-main.c │ └── 9-main.c ├── 0x13-bit_manipulation ├── 0-binary_to_uint.c ├── 1-print_binary.c ├── 100-get_endianness.c ├── 2-get_bit.c ├── 3-set_bit.c ├── 4-clear_bit.c ├── 5-flip_bits.c ├── README.md ├── _putchar.c ├── holberton.h └── main_test_files │ ├── 0-main.c │ ├── 1-main.c │ ├── 100-main.c │ ├── 2-main.c │ ├── 3-main.c │ ├── 4-main.c │ └── 5-main.c ├── 0x14-file_io ├── 0-read_textfile.c ├── 1-create_file.c ├── 2-append_text_to_file.c ├── 3-cp.c ├── README.md ├── holberton.h └── main_test_files │ ├── 0-main.c │ ├── 1-main.c │ ├── 2-main.c │ └── Requiescat ├── 0x15-simple_shell_practice ├── 0-printargvs ├── 1-getpids ├── 10-linkedlist_env ├── 2-getline.c ├── 3-strtok ├── 4-fork_wait_execve ├── 5-prompt_till_CtrlD ├── 6-execve.c ├── 7-printenv ├── 8-getenv.c ├── 9-set_unsetenv ├── README.md ├── main.c └── shell.h ├── 0x16-doubly_linked_lists ├── 0-print_dlistint.c ├── 1-dlistint_len.c ├── 100-password ├── 102-palindromes.py ├── 102-result ├── 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 └── main_test_files.c │ ├── 0-main.c │ ├── 1-main.c │ ├── 2-main.c │ ├── 3-main.c │ ├── 4-main.c │ ├── 5-main.c │ ├── 6-main.c │ ├── 7-main.c │ └── 8-main.c ├── 0x17-dynamic_libraries ├── 1-create_dynamic_lib.sh ├── 100-operations.so ├── 100-tests.py ├── README.md ├── functions_folder │ ├── 0-isupper.c │ ├── 0-main.c │ ├── 0-memset.c │ ├── 0-strcat.c │ ├── 1-isdigit.c │ ├── 1-memcpy.c │ ├── 1-strncat.c │ ├── 100-atoi.c │ ├── 2-strchr.c │ ├── 2-strlen.c │ ├── 2-strncpy.c │ ├── 3-islower.c │ ├── 3-op_functions.c │ ├── 3-puts.c │ ├── 3-strcmp.c │ ├── 3-strspn.c │ ├── 4-isalpha.c │ ├── 4-strpbrk.c │ ├── 5-strstr.c │ ├── 6-abs.c │ ├── 9-strcpy.c │ └── _putchar.c ├── holberton.h ├── len ├── liball.so └── libholberton.so ├── 0x19-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 └── main_files │ ├── 0-main.c │ ├── 1-main.c │ ├── 100-main.c │ ├── 2-main.c │ ├── 3-main.c │ ├── 4-main.c │ ├── 5-main.c │ └── 6-main.c ├── 0x1A-sorting_algorithms ├── 0-O ├── 0-bubble_sort.c ├── 0-main.c ├── 1-O ├── 1-insertion_sort_list.c ├── 1-main.c ├── 100-main.c ├── 100-shell_sort.c ├── 101-O ├── 101-cocktail_sort_list.c ├── 101-main.c ├── 102-O ├── 102-counting_sort.c ├── 102-main.c ├── 103-O ├── 103-main.c ├── 103-merge_sort.c ├── 2-O ├── 2-main.c ├── 2-selection_sort.c ├── 3-O ├── 3-main.c ├── 3-quick_sort.c ├── README.md ├── print_array.c ├── print_list.c └── sort.h ├── 0x1B-makefiles ├── 0-Makefile ├── 1-Makefile ├── 100-Makefile ├── 2-Makefile ├── 3-Makefile ├── 4-Makefile ├── 5-island_perimeter.py ├── 5-main.py ├── README.md ├── holberton.c ├── m.h └── main.c ├── 0x1D-search_algorithms ├── 0-linear.c ├── 1-binary.c ├── 100-jump.c ├── 101-O ├── 102-interpolation.c ├── 2-O ├── 3-O ├── 4-O ├── 5-O ├── 6-O ├── README.md ├── main_files │ ├── 0-main.c │ ├── 1-main.c │ ├── 100-main.c │ └── 102-main.c └── search_algos.h └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "monty"] 2 | path = monty 3 | url = https://github.com/MelissaN/monty 4 | [submodule "0x18-stacks_queues_lifo_fifo/monty"] 5 | path = 0x18-stacks_queues_lifo_fifo/monty 6 | url = git@github.com:MelissaN/monty.git 7 | [submodule "0x1C-Binary-trees"] 8 | path = 0x1C-Binary-trees 9 | url = git@github.com:flourishcodes/0x1C-Binary-trees.git 10 | -------------------------------------------------------------------------------- /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 - Entry point 6 | *Return: Always 1 7 | * 8 | */ 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 - Entry point 5 | *Return: Always 0 (Success) 6 | */ 7 | 8 | int main(void) 9 | { 10 | puts("\"Programming is like building a multilingual puzzle"); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x00-hello_world/5-printf.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | *main - Entry point 5 | *Return: Always 0 (Success) 6 | */ 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 - Entry point 5 | *Return: Always 0 (Success) 6 | */ 7 | 8 | int main(void) 9 | { 10 | printf("Size of a char: %zu byte(s)\n", sizeof(char)); 11 | printf("Size of an int: %zu byte(s)\n", sizeof(int)); 12 | printf("Size of a long int: %zu byte(s)\n", sizeof(long int)); 13 | printf("Size of a long long int: %zu byte(s)\n", sizeof(long long int)); 14 | printf("Size of a float: %zu byte(s)\n", sizeof(float)); 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/0-positive_or_negative.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /** 6 | *main - Entry point 7 | *Return: Always 0 (Success) 8 | */ 9 | 10 | int main(void) 11 | { 12 | int n; 13 | 14 | srand(time(0)); 15 | n = rand() - RAND_MAX / 2; 16 | 17 | if (n > 0) 18 | { 19 | printf("%i is positive\n", n); 20 | } 21 | else if (n == 0) 22 | { 23 | printf("%i is zero\n", n); 24 | } 25 | else if (n < 0) 26 | { 27 | printf("%i is negative\n", n); 28 | } 29 | 30 | return (0); 31 | } 32 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/1-last_digit.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /** 6 | *main - Entry point 7 | *Return: Always 0 (Success) 8 | */ 9 | 10 | int main(void) 11 | { 12 | int n; 13 | 14 | srand(time(0)); 15 | n = rand() - RAND_MAX / 2; 16 | 17 | printf("Last digit of %i is %i and is ", n, (n % 10)); 18 | 19 | if ((n % 10) == 0) 20 | { 21 | printf("0\n"); 22 | } 23 | else if ((n % 10) > 5) 24 | { 25 | printf("greater than 5\n"); 26 | } 27 | else 28 | { 29 | printf("less than 6 and not 0\n"); 30 | } 31 | 32 | return (0); 33 | } 34 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/10-print_comb2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | *main - Entry point, print 00 to 99 using putchar 5 | *Return: Always 0 (Success) 6 | */ 7 | 8 | int main(void) 9 | { 10 | int tens; 11 | int ones; 12 | 13 | for (tens = '0'; tens <= '9'; tens++) /*print tens place*/ 14 | { 15 | for (ones = '0'; ones <= '9'; ones++) /*print ones place*/ 16 | { 17 | putchar(tens); 18 | putchar(ones); 19 | if (!(tens == '9' && ones == '9')) /*skip comma at end*/ 20 | { 21 | putchar(','); 22 | putchar(' '); 23 | } 24 | } 25 | } 26 | putchar('\n'); 27 | 28 | return (0); 29 | } 30 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/100-print_comb3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | *main - print 00 to 99 with no duplicate digits or combos: no 11, no 10 (01) 5 | * 6 | *Return: Always 0 (Success) 7 | */ 8 | 9 | int main(void) 10 | { 11 | int ones; 12 | int tens; 13 | 14 | for (tens = '0'; tens <= '9'; tens++) /*increment tens*/ 15 | { 16 | for (ones = (tens + 1); ones <= '9'; ones++) /*one's ten+1*/ 17 | { 18 | putchar(tens); 19 | putchar(ones); 20 | 21 | if (tens != '8' || ones != '9') /*print commas*/ 22 | { 23 | putchar(','); 24 | putchar(' '); 25 | } 26 | } 27 | } 28 | putchar('\n'); 29 | 30 | return (0); 31 | } 32 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/101-print_comb4.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | *main - print all combinations of three different digits 5 | *Return: Always 0 (Success) 6 | */ 7 | 8 | int main(void) 9 | { 10 | int one; 11 | int ten; 12 | int hundred; 13 | 14 | for (hundred = '0'; hundred <= '9'; hundred++) /*hundreds place*/ 15 | { 16 | for (ten = (hundred + 1); ten <= '9'; ten++) /*tens=100s+1*/ 17 | { 18 | for (one = (ten + 1); one <= '9'; one++) /*ones*/ 19 | { 20 | putchar(hundred); 21 | putchar(ten); 22 | putchar(one); 23 | if (hundred != '7' || ten != '8' || one != '9') 24 | { 25 | putchar(','); 26 | putchar(' '); 27 | } 28 | } 29 | } 30 | } 31 | putchar('\n'); 32 | 33 | return (0); 34 | } 35 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/102-print_comb5.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | *main - print a num pair from 00-99 but no repeats (00 01, 00 02, 00 03,...) 5 | *Return: Always 0 (Success) 6 | */ 7 | 8 | int main(void) 9 | { 10 | int tens; 11 | int ones; 12 | int t; 13 | int o; 14 | 15 | for (tens = '0'; tens <= '9'; tens++) /*print first two digit combo*/ 16 | { 17 | for (ones = '0'; ones <= '9'; ones++) 18 | { 19 | for (t = tens; t <= '9'; t++) /*print second of pair*/ 20 | { 21 | for (o = ones + 1; o <= '9'; o++) 22 | { 23 | putchar(tens); 24 | putchar(ones); 25 | putchar(' '); 26 | putchar(t); 27 | putchar(o); 28 | 29 | if (!((tens == '9' && ones == '8') && 30 | (t == '9' && o == '9'))) 31 | { 32 | putchar(','); 33 | putchar(' '); 34 | } 35 | } 36 | o = '0'; 37 | } 38 | } 39 | } 40 | putchar('\n'); 41 | 42 | return (0); 43 | } 44 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/2-print_alphabet.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | *main - print lowercase a-z 5 | *Return: Always 0 (Success) 6 | */ 7 | 8 | int main(void) 9 | { 10 | char alpha = 'a'; 11 | 12 | while (alpha <= 'z') 13 | { 14 | putchar(alpha); 15 | alpha++; 16 | } 17 | putchar('\n'); 18 | 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/3-print_alphabets.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | *main - print lowercase and uppercase a-zA-Z using putchar 5 | *Return: Always 0 (Success) 6 | */ 7 | 8 | int main(void) 9 | { 10 | char lower = 'a'; 11 | char upper = 'A'; 12 | 13 | while (lower <= 'z') /*print lowercases a-z*/ 14 | { 15 | putchar(lower); 16 | lower++; 17 | } 18 | 19 | while (upper <= 'Z') /*print uppercase A-Z*/ 20 | { 21 | putchar(upper); 22 | upper++; 23 | } 24 | 25 | putchar('\n'); 26 | 27 | return (0); 28 | } 29 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/4-print_alphabt.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | *main - print lowercase alpha a-z but remove 'q' and 'e' 5 | *Return: Always 0 (Success) 6 | */ 7 | 8 | int main(void) 9 | { 10 | char alpha = 'a'; 11 | 12 | while (alpha <= 'z') 13 | { 14 | if ((alpha != 'q') && (alpha != 'e')) 15 | { 16 | putchar(alpha); 17 | } 18 | alpha++; 19 | } 20 | putchar('\n'); 21 | 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/5-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | *main - print 0123456789 5 | *Return: Always 0 (Success) 6 | */ 7 | 8 | int main(void) 9 | { 10 | int n = 0; 11 | 12 | while (n < 10) 13 | { 14 | printf("%i", n); 15 | n++; 16 | } 17 | putchar('\n'); 18 | 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/6-print_numberz.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | *main - print 0123456789 using putchar 5 | *Return: Always 0 (Success) 6 | */ 7 | 8 | int main(void) 9 | { 10 | int n = '0'; 11 | 12 | while (n <= '9') 13 | { 14 | putchar(n); 15 | n++; 16 | } 17 | putchar('\n'); 18 | 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/7-print_tebahpla.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | *main - print alphabet reversed using putchar 5 | *Return: Always 0 (Success) 6 | */ 7 | 8 | int main(void) 9 | { 10 | char alpha = 'z'; 11 | 12 | while (alpha >= 'a') 13 | { 14 | putchar(alpha); 15 | alpha--; 16 | } 17 | putchar('\n'); 18 | 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/8-print_base16.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | *main - prints hexadecimal base 0123456789abcdef, using putchar 5 | *Return: Always 0 (Success) 6 | */ 7 | 8 | int main(void) 9 | { 10 | int n = '0'; 11 | int a_to_f = 'a'; 12 | 13 | while (n <= '9') /*print 0-9*/ 14 | { 15 | putchar(n); 16 | n++; 17 | } 18 | 19 | while (a_to_f <= 'f') /*print a-f to finish hexbase*/ 20 | { 21 | putchar(a_to_f); 22 | a_to_f++; 23 | } 24 | 25 | putchar('\n'); 26 | 27 | return (0); 28 | } 29 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/9-print_comb.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | *main - print 0-9 separated with commas, using putchar 5 | *Return: Always 0 (Success) 6 | */ 7 | 8 | int main(void) 9 | { 10 | int n = '0'; 11 | 12 | while (n <= '9') 13 | { 14 | putchar(n); 15 | if (n != '9') 16 | { 17 | putchar(','); 18 | putchar(' '); 19 | } 20 | n++; 21 | } 22 | putchar('\n'); 23 | 24 | return (0); 25 | } 26 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/0-holberton.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * main - print Holberton, followed by new line 5 | * Return: 0 6 | */ 7 | 8 | int main(void) 9 | { 10 | _putchar('H'); 11 | _putchar('o'); 12 | _putchar('l'); 13 | _putchar('b'); 14 | _putchar('e'); 15 | _putchar('r'); 16 | _putchar('t'); 17 | _putchar('o'); 18 | _putchar('n'); 19 | _putchar('\n'); 20 | 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/1-alphabet.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * print_alphabet - prints lowercase alphabet 5 | * Return: 0 6 | */ 7 | 8 | void print_alphabet(void) 9 | { 10 | char alpha; 11 | 12 | alpha = 'a'; 13 | 14 | while (alpha <= 'z') 15 | { 16 | _putchar(alpha); 17 | alpha++; 18 | } 19 | _putchar('\n'); 20 | } 21 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/10-add.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * add - add two integers 5 | * @a: two integer arguments 6 | * @b: two integer arguments 7 | * Return: sum 8 | */ 9 | 10 | int add(int a, int b) 11 | { 12 | return (a + b); 13 | } 14 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/101-natural.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - print sum of multiples of 3 or 5 below 1024 5 | * Return: 0 6 | */ 7 | 8 | int main(void) 9 | { 10 | int n; 11 | int sum; 12 | 13 | sum = 0; 14 | 15 | for (n = 0; n < 1024; n++) 16 | { 17 | if ((n % 3 == 0) || (n % 5 == 0)) 18 | { 19 | sum += n; 20 | } 21 | } 22 | printf("%d\n", sum); 23 | 24 | return (0); 25 | } 26 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/102-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - print first 50 Fibonacci numbers 5 | * Return: 0 6 | */ 7 | 8 | int main(void) 9 | { 10 | int counter; 11 | int countto = 50; 12 | long a = 1; 13 | long b = 2; 14 | 15 | for (counter = 1; counter <= (countto / 2); counter++) 16 | { 17 | printf("%li %li ", a, b); 18 | a += b; 19 | b += a; 20 | } 21 | if (countto % 2 == 1) 22 | printf("%li", a); 23 | 24 | printf("\n"); 25 | 26 | return (0); 27 | } 28 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/103-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - print sum of even Fionacci sequence up to 4,000,000 5 | * Return: 0 6 | */ 7 | 8 | int main(void) 9 | { 10 | int sum_of_evens = 0; 11 | int a; 12 | int b; 13 | int sum = 1; 14 | 15 | a = 1; 16 | b = 1; 17 | 18 | while (b < 4000000) 19 | { 20 | sum = a + b; 21 | a = b; 22 | b = sum; 23 | if ((sum <= 4000000) && (sum % 2 == 0)) 24 | sum_of_evens += sum; 25 | } 26 | printf("%d\n", sum_of_evens); 27 | 28 | return (0); 29 | } 30 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/11-print_to_98.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * print_to_98 - print numbers n to 98 6 | * @n: integer argument 7 | */ 8 | 9 | void print_to_98(int n) 10 | { 11 | while (n < 98) 12 | { 13 | printf("%d, ", n); 14 | n++; 15 | } 16 | 17 | while (n > 98) 18 | { 19 | printf("%d, ", n); 20 | n--; 21 | } 22 | 23 | if (n == 98) 24 | printf("%d", n); 25 | 26 | printf("\n"); 27 | } 28 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/2-print_alphabet_x10.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * print_alphabet_x10 - print lowercase alphabet ten times 5 | * Return: 0 6 | */ 7 | 8 | void print_alphabet_x10(void) 9 | { 10 | char alpha; 11 | int counter; 12 | 13 | for (counter = 1; counter <= 10; counter++) 14 | { 15 | for (alpha = 'a'; alpha <= 'z'; alpha++) 16 | { 17 | _putchar(alpha); 18 | } 19 | _putchar('\n'); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/3-islower.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _islower - checks for lowercase character 5 | * @c: character to check 6 | * Return: 1 if lowercase, 0 if uppercase 7 | */ 8 | 9 | int _islower(int c) 10 | { 11 | if (c >= 'a' && c <= 'z') 12 | { 13 | return (1); 14 | } 15 | else 16 | { 17 | return (0); 18 | } 19 | _putchar('\n'); 20 | } 21 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/4-isalpha.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _isalpha - checks for alphabet character 5 | * @c: character to check 6 | * Return: 1 if lower or uppercase, 0 if not alphabet character 7 | */ 8 | 9 | int _isalpha(int c) 10 | { 11 | if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) 12 | { 13 | return (1); 14 | } 15 | else 16 | { 17 | return (0); 18 | } 19 | _putchar('\n'); 20 | } 21 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/5-sign.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * print_sign - print sign of number 5 | * @n: number to check 6 | * Return: 1 if greater than 0, 0 if 0, -1 if less than 0 7 | */ 8 | 9 | int print_sign(int n) 10 | { 11 | if (n > 0) 12 | { 13 | _putchar('+'); 14 | return (1); 15 | } 16 | else if (n == 0) 17 | { 18 | _putchar('0'); 19 | return (0); 20 | } 21 | else 22 | { 23 | _putchar('-'); 24 | return (-1); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/6-abs.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _abs - compute absolute value of integer 5 | * @n: integer argument 6 | * Return: absolute value 7 | */ 8 | 9 | int _abs(int n) 10 | { 11 | if (n > 0) 12 | return (n); 13 | else if (n < 0) 14 | return (-n); 15 | 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/7-print_last_digit.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * print_last_digit - print last digit of integer 5 | * @n: integer argument 6 | * Return: last digit of number 7 | */ 8 | 9 | int print_last_digit(int n) 10 | { 11 | if (n < 0) 12 | n *= -1; 13 | 14 | _putchar('0' + (n % 10)); 15 | 16 | return (n % 10); 17 | } 18 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/8-24_hours.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * jack_bauer - print every minute of a day 5 | * Description: print minutes 00:00 to 23:59 6 | */ 7 | 8 | void jack_bauer(void) 9 | { 10 | int hours = 0; 11 | int min = 0; 12 | 13 | while (hours < 24) 14 | { 15 | while (min < 60) 16 | { 17 | _putchar('0' + (hours / 10)); 18 | _putchar('0' + (hours % 10)); 19 | _putchar(':'); 20 | _putchar('0' + (min / 10)); 21 | _putchar('0' + (min % 10)); 22 | _putchar('\n'); 23 | min++; 24 | } 25 | min = 0; 26 | hours++; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/9-times_table.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * times_table - print multiplication table 5 | */ 6 | 7 | void times_table(void) 8 | { 9 | int row; 10 | int column; 11 | int product; 12 | 13 | for (row = 0; row <= 9; row++) 14 | { 15 | for (column = 0; column <= 9; column++) 16 | { 17 | product = (row * column); 18 | 19 | if (column == 0) 20 | { 21 | _putchar('0' + product); 22 | } 23 | else if (product <= 9) 24 | { 25 | _putchar(','); 26 | _putchar(' '); 27 | _putchar(' '); 28 | _putchar('0' + product); 29 | } 30 | else if (product > 9) 31 | { 32 | _putchar(','); 33 | _putchar(' '); 34 | _putchar('0' + (product / 10)); 35 | _putchar('0' + (product % 10)); 36 | } 37 | } 38 | _putchar('\n'); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _putchar - writes the character c to stdout 5 | * @c: The character to print 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | int _putchar(char c) 11 | { 12 | return (write(1, &c, 1)); 13 | } 14 | -------------------------------------------------------------------------------- /0x03-more_functions_nested_loops/0-isupper.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _isupper - checks for uppercase character 5 | * @c: character to be checked 6 | * Return: 1 if true, 0 if false 7 | */ 8 | 9 | int _isupper(int c) 10 | { 11 | if (c >= 'A' && c <= 'Z') 12 | return (1); 13 | 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x03-more_functions_nested_loops/1-isdigit.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _isdigit - checks for a digit 0 through 9 5 | * @c: digit to be checked 6 | * Return: 1 if true, 0 if false 7 | */ 8 | 9 | int _isdigit(int c) 10 | { 11 | if (c >= '0' && c <= '9') 12 | return (1); 13 | 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x03-more_functions_nested_loops/10-print_triangle.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * print_triangle - print traingle with #'s with given size 5 | * @size: size of triangle to draw 6 | */ 7 | 8 | void print_triangle(int size) 9 | { 10 | int height; 11 | int width; 12 | int draw; 13 | 14 | if (size <= 0) 15 | _putchar('\n'); 16 | 17 | for (height = 1; height <= size; height++) 18 | { 19 | for (width = 1; width <= (size - height); width++) 20 | _putchar(' '); 21 | 22 | for (draw = 1; draw <= height; draw++) 23 | _putchar('#'); 24 | 25 | _putchar('\n'); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /0x03-more_functions_nested_loops/100-prime_factor.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - print largest prime factor of 612852475143 5 | * Return: 0 on success 6 | */ 7 | 8 | int main(void) 9 | { 10 | unsigned long num = 612852475143; 11 | unsigned long divisor = 2; 12 | 13 | while (divisor < num) 14 | { 15 | if (num % divisor == 0) 16 | { 17 | num /= divisor; 18 | divisor = 2; 19 | } 20 | else 21 | divisor++; 22 | } 23 | printf("%lu\n", num); 24 | return (0); 25 | } 26 | -------------------------------------------------------------------------------- /0x03-more_functions_nested_loops/101-print_number.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * print_number - print an integer, without using long, arrays, or pointers 5 | * @n: number to be printed 6 | */ 7 | 8 | void print_number(int n) 9 | { 10 | unsigned int tens, digit, positive = n; 11 | double t_beg = 1; 12 | 13 | if (n == 0) 14 | _putchar('0'); 15 | else 16 | { 17 | if (n < 0) 18 | { 19 | positive = n * -1; 20 | _putchar('-'); 21 | } 22 | 23 | while (t_beg <= positive) 24 | t_beg *= 10; 25 | tens = t_beg / 10; 26 | 27 | while (tens >= 1) 28 | { 29 | digit = positive / tens; 30 | _putchar(digit + '0'); 31 | positive = (positive - (tens * digit)); 32 | tens /= 10; 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /0x03-more_functions_nested_loops/2-mul.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * mul - multiplies two integers 5 | * @a: one integer 6 | * @b: second integer 7 | * Return: product of a and b 8 | */ 9 | 10 | int mul(int a, int b) 11 | { 12 | return (a * b); 13 | } 14 | -------------------------------------------------------------------------------- /0x03-more_functions_nested_loops/3-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * print_numbers - print 0 to 9 5 | */ 6 | 7 | void print_numbers(void) 8 | { 9 | char i; 10 | 11 | for (i = '0'; i <= '9'; i++) 12 | _putchar(i); 13 | 14 | _putchar('\n'); 15 | } 16 | -------------------------------------------------------------------------------- /0x03-more_functions_nested_loops/4-print_most_numbers.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * print_most_numbers - print 0 to 9, skip 2 and 4 5 | */ 6 | 7 | void print_most_numbers(void) 8 | { 9 | char i; 10 | 11 | for (i = '0'; i <= '9'; i++) 12 | { 13 | if (i != '2' && i != '4') 14 | _putchar(i); 15 | } 16 | _putchar('\n'); 17 | } 18 | -------------------------------------------------------------------------------- /0x03-more_functions_nested_loops/5-more_numbers.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * more_numbers - print 0 to 14 ten times 5 | */ 6 | 7 | void more_numbers(void) 8 | { 9 | char i; 10 | int counter; 11 | 12 | for (counter = 1; counter <= 10; counter++) 13 | { 14 | for (i = 0; i <= 14; i++) 15 | { 16 | if (i / 10 > 0) 17 | _putchar((i / 10) + '0'); 18 | _putchar((i % 10) + '0'); 19 | } 20 | _putchar('\n'); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /0x03-more_functions_nested_loops/6-print_line.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * print_line - print the character "_" n times to draw a line 5 | * @n: number of characters to draw 6 | */ 7 | 8 | void print_line(int n) 9 | { 10 | int times = n; 11 | 12 | for (times = n; times > 0; times--) 13 | _putchar('_'); 14 | 15 | _putchar('\n'); 16 | } 17 | -------------------------------------------------------------------------------- /0x03-more_functions_nested_loops/7-print_diagonal.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * print_diagonal - print character "\" n times 5 | * @n: number of characters to draw 6 | */ 7 | 8 | void print_diagonal(int n) 9 | { 10 | int draw; 11 | int space; 12 | 13 | if (n > 0) 14 | { 15 | for (draw = 1; draw <= n; draw++) 16 | { 17 | for (space = 1; space < draw; space++) 18 | _putchar(' '); 19 | _putchar('\\'); 20 | _putchar('\n'); 21 | } 22 | } 23 | else 24 | _putchar('\n'); 25 | } 26 | -------------------------------------------------------------------------------- /0x03-more_functions_nested_loops/8-print_square.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * print_square - print a square of #'s given size 5 | * @size: size to draw 6 | */ 7 | 8 | void print_square(int size) 9 | { 10 | int row, column; 11 | 12 | if (size > 0) 13 | { 14 | for (row = 1; row <= size; row++) 15 | { 16 | for (column = 1; column <= size; column++) 17 | { 18 | _putchar('#'); 19 | } 20 | _putchar('\n'); 21 | } 22 | } 23 | else 24 | _putchar('\n'); 25 | } 26 | -------------------------------------------------------------------------------- /0x03-more_functions_nested_loops/9-fizz_buzz.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - print 1 to 100, 5 | * multiples of 3 with Fizz, 6 | * multiples of 5 with Buzz 7 | * and multiple of both 3 and 5 with FizzBuzz 8 | * Return: 0 on success 9 | */ 10 | 11 | int main(void) 12 | { 13 | int i; 14 | 15 | for (i = 1; i <= 100; i++) 16 | { 17 | if (i % 3 == 0 && i % 5 == 0) 18 | printf("FizzBuzz"); 19 | else if (i % 3 == 0) 20 | printf("Fizz"); 21 | else if (i % 5 == 0) 22 | printf("Buzz"); 23 | else 24 | printf("%d", i); 25 | 26 | if (i < 100) 27 | printf(" "); 28 | } 29 | printf("\n"); 30 | return (0); 31 | } 32 | -------------------------------------------------------------------------------- /0x03-more_functions_nested_loops/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _putchar - writes the character c to stdout 5 | * @c: The character to print 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | int _putchar(char c) 11 | { 12 | return (write(1, &c, 1)); 13 | } 14 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/0-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int n; 12 | 13 | n = 402; 14 | printf("n=%d\n", n); 15 | reset_to_98(&n); 16 | printf("n=%d\n", n); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/0-reset_to_98.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * reset_to_98 - takes a pointer to an int as parameter 5 | * and updates the value it points to to 98 6 | * @n: pointer to int n 7 | */ 8 | 9 | void reset_to_98(int *n) 10 | { 11 | *n = 98; 12 | } 13 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/1-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int a; 12 | int b; 13 | 14 | a = 98; 15 | b = 42; 16 | printf("a=%d, b=%d\n", a, b); 17 | swap_int(&a, &b); 18 | printf("a=%d, b=%d\n", a, b); 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/1-swap.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * swap_int - swaps the values of two integers 5 | * @a: integer to swap 6 | * @b: integer to swap 7 | */ 8 | 9 | void swap_int(int *a, int *b) 10 | { 11 | int tmp; 12 | 13 | tmp = *a; 14 | *a = *b; 15 | *b = tmp; 16 | } 17 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/100-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int n; 12 | 13 | n = _atoi("98"); 14 | printf("%d\n", n); 15 | n = _atoi("-402"); 16 | printf("%d\n", n); 17 | n = _atoi(" ------++++++-----+++++--98"); 18 | printf("%d\n", n); 19 | n = _atoi("2147483647"); 20 | printf("%d\n", n); 21 | n = _atoi("-2147483648"); 22 | printf("%d\n", n); 23 | n = _atoi("Suite 402"); 24 | printf("%d\n", n); 25 | n = _atoi(" + + - -98 Battery Street; San Francisco, CA 94111 - USA "); 26 | printf("%d\n", n); 27 | n = _atoi("---++++ -++ Sui - te - 402 #cisfun :)"); 28 | printf("%d\n", n); 29 | return (0); 30 | } 31 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/2-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char *str; 12 | int len; 13 | 14 | str = "Holberton!"; 15 | len = _strlen(str); 16 | printf("%d\n", len); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/2-strlen.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _strlen - returns the length of a string 5 | * @s: string 6 | * Return: length 7 | */ 8 | 9 | int _strlen(char *s) 10 | { 11 | int len = 0; 12 | 13 | while (*s != '\0') 14 | { 15 | len++; 16 | s++; 17 | } 18 | 19 | return (len); 20 | } 21 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/3-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * main - check the code for Holberton School students. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | char *str; 11 | 12 | str = "I do not fear computers. I fear the lack of them - Isaac Asimov"; 13 | _puts(str); 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/3-puts.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _puts - prints a string, followed by a new line, to stdout 5 | * @str: string to print 6 | */ 7 | 8 | void _puts(char *str) 9 | { 10 | while (*str != '\0') 11 | _putchar(*str++); 12 | 13 | _putchar('\n'); 14 | } 15 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/4-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * main - check the code for Holberton School students. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | char *str; 11 | 12 | str = "I do not fear computers. I fear the lack of them - Isaac Asimov"; 13 | print_rev(str); 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/4-print_rev.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * print_rev - prints a string, in reverse, followed by a new line 5 | * @s: string to reverse 6 | */ 7 | 8 | void print_rev(char *s) 9 | { 10 | 11 | int len = 0; 12 | int i; 13 | 14 | /* iterate to find length of string and point to last character */ 15 | while (*s != '\0') 16 | { 17 | len++; 18 | ++s; 19 | } 20 | 21 | /* go back to character before null character */ 22 | s--; 23 | 24 | /* print string reversed */ 25 | for (i = len; i > 0; i--) 26 | { 27 | _putchar(*s); 28 | s--; 29 | } 30 | 31 | _putchar('\n'); 32 | } 33 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/5-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char s[10] = "Holberton"; 12 | 13 | printf("%s\n", s); 14 | rev_string(s); 15 | printf("%s\n", s); 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/5-rev_string.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _strlen - returns the length of a string 5 | * @s: string 6 | * Return: length 7 | */ 8 | 9 | int _strlen(char *s) 10 | { 11 | int len = 0; 12 | 13 | while (*s != '\0') 14 | { 15 | len++; 16 | s++; 17 | } 18 | 19 | return (len); 20 | } 21 | 22 | /** 23 | * rev_string - reverses a string 24 | * @s: string to reverse 25 | */ 26 | 27 | void rev_string(char *s) 28 | { 29 | int i = 0; 30 | int j = (_strlen(s) - 1); 31 | char tmp; 32 | 33 | while (i < j) 34 | { 35 | tmp = s[i]; 36 | s[i] = s[j]; 37 | s[j] = tmp; 38 | i++, j--; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/6-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * main - check the code for Holberton School students. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | char *str; 11 | 12 | str = "0123456789"; 13 | puts2(str); 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/6-puts2.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _strlen - returns the length of a string 5 | * @s: string 6 | * Return: length 7 | */ 8 | 9 | int _strlen(char *s) 10 | { 11 | int len = 0; 12 | 13 | while (*s != '\0') 14 | { 15 | len++; 16 | s++; 17 | } 18 | 19 | return (len); 20 | } 21 | 22 | /** 23 | * puts2 - prints every second character of a string, followed by a new line 24 | * @str: string to print 25 | */ 26 | 27 | void puts2(char *str) 28 | { 29 | int i; 30 | 31 | for (i = 0; str[i] != '\0' && i < _strlen(str); i += 2) 32 | _putchar(str[i]); 33 | 34 | _putchar('\n'); 35 | } 36 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/7-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * main - check the code for Holberton School students. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | char *str; 11 | 12 | str = "0123456789"; 13 | puts_half(str); 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/7-puts_half.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _strlen - returns the length of a string 5 | * @s: string 6 | * Return: length 7 | */ 8 | 9 | int _strlen(char *s) 10 | { 11 | int len = 0; 12 | 13 | while (*s != '\0') 14 | { 15 | len++; 16 | s++; 17 | } 18 | 19 | return (len); 20 | } 21 | 22 | /** 23 | * puts_half - prints half of a string, followed by a new line 24 | * @str: string to print 25 | */ 26 | 27 | void puts_half(char *str) 28 | { 29 | int idx; 30 | int len = _strlen(str); 31 | 32 | /* find the index to start depending on even/odd amount of strlen */ 33 | if (len % 2 != 0) 34 | idx = (len / 2) + 1; 35 | else 36 | idx = (len / 2); 37 | 38 | while (idx < len) 39 | { 40 | _putchar(*(str + idx)); 41 | idx++; 42 | } 43 | _putchar('\n'); 44 | } 45 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/8-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * main - check the code for Holberton School students. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | int array[5]; 11 | 12 | array[0] = 98; 13 | array[1] = 402; 14 | array[2] = -198; 15 | array[3] = 298; 16 | array[4] = -1024; 17 | print_array(array, 5); 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/8-print_array.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * print_array - prints n elements of an array of integers 5 | * @a: array 6 | * @n: number of elements 7 | */ 8 | 9 | void print_array(int *a, int n) 10 | { 11 | int i; 12 | 13 | for (i = 0; i < n; i++) 14 | { 15 | if (i != (n - 1)) 16 | printf("%d, ", a[i]); 17 | else 18 | printf("%d", a[i]); 19 | } 20 | printf("\n"); 21 | 22 | } 23 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/9-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char s1[98]; 12 | char *p; 13 | 14 | p = _strcpy(s1, "First, solve the problem. Then, write the code\n"); 15 | printf("%s", s1); 16 | printf("%s", p); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/9-strcpy.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _strlen - returns the length of a string 5 | * @s: string 6 | * Return: length 7 | */ 8 | 9 | int _strlen(char *s) 10 | { 11 | int len = 0; 12 | 13 | while (*s != '\0') 14 | { 15 | len++; 16 | s++; 17 | } 18 | 19 | return (len); 20 | } 21 | 22 | /** 23 | * _strcpy - copies the string pointed to by src, 24 | * including the terminating null byte (\0), 25 | * to the buffer pointed to by dest 26 | * @dest: copy source to this buffer 27 | * @src: this is the source to copy 28 | * Return: copy of original source 29 | */ 30 | 31 | char *_strcpy(char *dest, char *src) 32 | { 33 | int i; 34 | 35 | for (i = 0; i <= _strlen(src); i++) 36 | dest[i] = src[i]; 37 | 38 | return (dest); 39 | } 40 | 41 | /* another method using pointer notation 42 | 43 | char *_strcpy(char *dest, char *src) 44 | { 45 | while (*src) 46 | { 47 | *dest = *src; 48 | src++; 49 | dest++; 50 | } 51 | return (dest); 52 | } 53 | */ 54 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _putchar - writes the character c to stdout 5 | * @c: The character to print 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | int _putchar(char c) 11 | { 12 | return (write(1, &c, 1)); 13 | } 14 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/0-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char s1[98] = "Hello "; 12 | char s2[] = "World!\n"; 13 | char *p; 14 | 15 | printf("%s\n", s1); 16 | printf("%s", s2); 17 | p = _strcat(s1, s2); 18 | printf("%s", s1); 19 | printf("%s", s2); 20 | printf("%s", p); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/0-strcat.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _strcat - concatenate two strings 5 | * @dest: string to be appended to 6 | * @src: string to append 7 | * Return: concatenated string 8 | */ 9 | 10 | char *_strcat(char *dest, char *src) 11 | { 12 | int i = 0; 13 | int j = 0; 14 | 15 | while (dest[i] != '\0') 16 | i++; 17 | 18 | while (src[j] != '\0') 19 | { 20 | dest[i] = src[j]; 21 | i++; 22 | j++; 23 | } 24 | dest[i] = '\0'; 25 | 26 | return (dest); 27 | } 28 | 29 | /* another method using pointer notation 30 | 31 | char *_strcat(char *dest, char *src) 32 | { 33 | int i = 0; 34 | int j = 0; 35 | 36 | while (*(dest + i)) 37 | i++; 38 | 39 | while (*(src + j)) 40 | { 41 | *(dest+i) = *(src+j); 42 | i++; 43 | j++; 44 | } 45 | *(dest+i) = '\0'; 46 | 47 | return (dest); 48 | } 49 | */ 50 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/1-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char s1[98] = "Hello "; 12 | char s2[] = "World!\n"; 13 | char *p; 14 | 15 | printf("%s\n", s1); 16 | printf("%s", s2); 17 | p = _strncat(s1, s2, 1); 18 | printf("%s\n", s1); 19 | printf("%s", s2); 20 | printf("%s\n", p); 21 | p = _strncat(s1, s2, 1024); 22 | printf("%s", s1); 23 | printf("%s", s2); 24 | printf("%s", p); 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/100-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * main - check the code for Holberton School students. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | print_number(98); 11 | _putchar('\n'); 12 | print_number(402); 13 | _putchar('\n'); 14 | print_number(1024); 15 | _putchar('\n'); 16 | print_number(0); 17 | _putchar('\n'); 18 | print_number(-98); 19 | _putchar('\n'); 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/100-print_number.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * print_number - print an integer, without using long, arrays, or pointers 5 | * @n: number to be printed 6 | */ 7 | 8 | void print_number(int n) 9 | { 10 | unsigned int tens, digit, positive = n; 11 | double t_beg = 1; 12 | 13 | if (n == 0) 14 | _putchar('0'); 15 | else 16 | { 17 | if (n < 0) 18 | { 19 | positive = n * -1; 20 | _putchar('-'); 21 | } 22 | 23 | while (t_beg <= positive) 24 | t_beg *= 10; 25 | tens = t_beg / 10; 26 | 27 | while (tens >= 1) 28 | { 29 | digit = positive / tens; 30 | _putchar(digit + '0'); 31 | positive = (positive - (tens * digit)); 32 | tens /= 10; 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/101-magic.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | int n; 6 | int a[5]; 7 | int *p; 8 | 9 | a[2] = 1024; 10 | p = &n; 11 | /* 12 | * write your line of code here... 13 | * Remember: 14 | * - you are not allowed to use a 15 | * - you are not allowed to modify p 16 | * - only one statement 17 | * - you are not allowed to code anything else than this line of code 18 | */ 19 | *(p + 5) = 98; 20 | /* ...so that this prints 98\n */ 21 | printf("a[2] = %d\n", a[2]); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/2-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char s1[98]; 12 | char *p; 13 | int i; 14 | 15 | for (i = 0; i < 98 - 1; i++) 16 | { 17 | s1[i] = '*'; 18 | } 19 | s1[i] = '\0'; 20 | printf("%s\n", s1); 21 | p = _strncpy(s1, "First, solve the problem. Then, write the code\n", 5); 22 | printf("%s\n", s1); 23 | printf("%s\n", p); 24 | p = _strncpy(s1, "First, solve the problem. Then, write the code\n", 90); 25 | printf("%s", s1); 26 | printf("%s", p); 27 | for (i = 0; i < 98; i++) 28 | { 29 | if (i % 10) 30 | { 31 | printf(" "); 32 | } 33 | if (!(i % 10) && i) 34 | { 35 | printf("\n"); 36 | } 37 | printf("0x%02x", s1[i]); 38 | } 39 | printf("\n"); 40 | return (0); 41 | } 42 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/2-strncpy.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _strncpy - copies n bytes of a source into buffer of a destination string, 5 | * @dest: copy source to this buffer 6 | * @src: this is the source to copy 7 | * @n: n bytes to be copied 8 | * Return: copied string 9 | */ 10 | 11 | char *_strncpy(char *dest, char *src, int n) 12 | { 13 | int i; 14 | 15 | for (i = 0; i < n && src[i] != '\0'; i++) 16 | dest[i] = src[i]; 17 | while (i < n) 18 | { 19 | dest[i] = '\0'; 20 | i++; 21 | } 22 | 23 | return (dest); 24 | } 25 | 26 | /* this method in pointer arithmetic 27 | 28 | char *_strncpy(char *dest, char *src, int n) 29 | { 30 | int i; 31 | 32 | for (i = 0; i < n && *(src + i); i++) 33 | *(dest + i) = *(src + i); 34 | while (i < n) 35 | { 36 | *(dest + i) = '\0'; 37 | i++; 38 | } 39 | return (dest); 40 | } 41 | */ 42 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/3-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char s1[] = "aHello"; 12 | char s2[] = "aWorld!"; 13 | 14 | printf("%d\n", _strcmp(s1, s2)); 15 | printf("%d\n", _strcmp(s2, s1)); 16 | printf("%d\n", _strcmp(s1, s1)); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/3-strcmp.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _strcmp - compare two strings 5 | * @s1: one string 6 | * @s2: one string 7 | * Return: int that tells num spaces in between 8 | */ 9 | 10 | int _strcmp(char *s1, char *s2) 11 | { 12 | int i = 0; 13 | 14 | while (*(s1 + i) == *(s2 + i) && *(s1 + i)) 15 | i++; 16 | 17 | if (*(s2 + i)) 18 | return (*(s1 + i) - *(s2 + i)); 19 | else 20 | return (0); 21 | } 22 | 23 | /* another method 24 | 25 | int _strcmp(char *s1, char *s2) 26 | { 27 | // while neither strings have ended 28 | while (*s1 != '\0' || *s2 != '\0') 29 | { 30 | // if element in arr1 in arr2 are diff, return difference 31 | if (*s1 != *s2) 32 | return (*s1 - *s2); 33 | // iterate index 34 | s1++; 35 | s2++; 36 | } 37 | return (0); 38 | } 39 | */ 40 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/4-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * @a: an array of integers 7 | * @n: the number of elements to swap 8 | * 9 | * Return: nothing. 10 | */ 11 | void print_array(int *a, int n) 12 | { 13 | int i; 14 | 15 | i = 0; 16 | while (i < n) 17 | { 18 | if (i != 0) 19 | { 20 | printf(", "); 21 | } 22 | printf("%d", a[i]); 23 | i++; 24 | } 25 | printf("\n"); 26 | } 27 | 28 | /** 29 | * main - check the code for Holberton School students. 30 | * 31 | * Return: Always 0. 32 | */ 33 | int main(void) 34 | { 35 | int a[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 98, 1024, 1337}; 36 | 37 | print_array(a, sizeof(a) / sizeof(int)); 38 | reverse_array(a, sizeof(a) / sizeof(int)); 39 | print_array(a, sizeof(a) / sizeof(int)); 40 | return (0); 41 | } 42 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/4-rev_array.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * reverse_array - reverse array elements 5 | * @a: array 6 | * @n: number of elements in array 7 | */ 8 | 9 | void reverse_array(int *a, int n) 10 | { 11 | 12 | int tmp, beg = 0; 13 | int end = n - 1; /* omit null terminator in length */ 14 | 15 | while (beg < end) 16 | { 17 | tmp = *(a + beg); 18 | *(a + beg) = *(a + end); 19 | *(a + end) = tmp; 20 | beg++, end--; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/5-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char s[] = "Holberton School!\n"; 12 | char *p; 13 | 14 | p = string_toupper(s); 15 | printf("%s", p); 16 | printf("%s", s); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/5-string_toupper.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * string_toupper - capitalize all letters in string 5 | * @s: string to manipulate 6 | * Return: string with all letters capitalized 7 | */ 8 | 9 | char *string_toupper(char *s) 10 | { 11 | 12 | int i = 0; 13 | 14 | for (i = 0; s[i] != '\0'; i++) 15 | { 16 | if (s[i] >= 'a' && s[i] <= 'z') 17 | s[i] = s[i] - 'a' + 'A'; 18 | } 19 | return (s); 20 | } 21 | 22 | /* another method with pointer notation 23 | 24 | char *string_toupper(char *s) 25 | { 26 | while (*s) //if s exists will omit null terminator 27 | if (*s >= 'a' && *s <= 'z') 28 | *s = *s -'a' + 'A'; 29 | s++; 30 | 31 | } 32 | */ 33 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/6-cap_string.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * cap_string - capitalize first letter of each word 5 | * @s: string to manipulate 6 | * Return: string 7 | */ 8 | 9 | char *cap_string(char *s) 10 | { 11 | int i = 0; 12 | 13 | /* check first index for capital */ 14 | if (s[i] >= 'a' && s[i] <= 'z') 15 | s[i] = s[i] - 'a' + 'A'; 16 | i++; 17 | 18 | while (s[i] != '\0') /* iterate through string */ 19 | { 20 | 21 | /* if lowercase and prior char is separator, capitalize*/ 22 | if ((s[i] >= 'a' && s[i] <= 'z') 23 | && (s[i - 1] == ',' || s[i - 1] == ';' || s[i - 1] == '.' || 24 | s[i - 1] == '!' || s[i - 1] == '?' || s[i - 1] == '"' || 25 | s[i - 1] == '(' || s[i - 1] == ')' || s[i - 1] == '{' || 26 | s[i - 1] == '}' || s[i - 1] == ' ' || s[i - 1] == '\t' 27 | || s[i - 1] == '\n')) 28 | s[i] = s[i] - 'a' + 'A'; 29 | i++; 30 | } 31 | 32 | return (s); 33 | } 34 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/6-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char s[] = "Expect the best. Prepare for the worst. Capitalize on what comes.\nhello world! hello-world 0123456hello world\thello world.hello world\n"; 12 | char *p; 13 | 14 | p = cap_string(s); 15 | printf("%s", p); 16 | printf("%s", s); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/7-leet.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * leet - encode string into 1337 leet 5 | * @s: string to manipulate 6 | * Return: string 7 | */ 8 | 9 | char *leet(char *s) 10 | { 11 | 12 | int a[11] = {'a', 'A', 'e', 'E', 'o', 'O', 't', 'T', 'l', 'L'}; 13 | int b[11] = {'4', '4', '3', '3', '0', '0', '7', '7', '1', '1'}; 14 | 15 | int i, j; 16 | 17 | for (j = 0; s[j] != '\0'; j++) 18 | { 19 | for (i = 0; a[i] != '\0'; i++) 20 | { 21 | if (s[j] == a[i]) 22 | s[j] = b[i]; 23 | } 24 | } 25 | 26 | return (s); 27 | } 28 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/7-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char s[] = "Expect the best. Prepare for the worst. Capitalize on what comes.\n"; 12 | char *p; 13 | 14 | p = leet(s); 15 | printf("%s", p); 16 | printf("%s", s); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/8-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char s[] = "ROT13 (\"rotate by 13 places\", sometimes hyphenated ROT-13) is a simple letter substitution cipher.\n"; 12 | char *p; 13 | 14 | p = rot13(s); 15 | printf("%s", p); 16 | printf("------------------------------------\n"); 17 | printf("%s", s); 18 | printf("------------------------------------\n"); 19 | p = rot13(s); 20 | printf("%s", p); 21 | printf("------------------------------------\n"); 22 | printf("%s", s); 23 | printf("------------------------------------\n"); 24 | p = rot13(s); 25 | printf("%s", p); 26 | printf("------------------------------------\n"); 27 | printf("%s", s); 28 | return (0); 29 | } 30 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _putchar - writes the character c to stdout 5 | * @c: The character to print 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | int _putchar(char c) 11 | { 12 | return (write(1, &c, 1)); 13 | } 14 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/0-memset.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _memset - fill n bytes of memory with a constant value 5 | * @s: pointer to memory area 6 | * @b: constant value 7 | * @n: number of bytes to fill 8 | * Return: memory area 9 | */ 10 | 11 | char *_memset(char *s, char b, unsigned int n) 12 | { 13 | 14 | int size = n; /* only accept positive sizes */ 15 | 16 | if (size > 0) 17 | { 18 | int i; 19 | 20 | for (i = 0; i < size; i++) 21 | s[i] = b; 22 | } 23 | 24 | return (s); 25 | } 26 | 27 | /* pointer arithmetic version 28 | 29 | char *_memset(char *s, char b, unsigned int n) 30 | { 31 | 32 | int size = n; 33 | 34 | if (size > 0) 35 | { 36 | int i; 37 | 38 | for (i = 0; i < size; i++) 39 | *(s + i) = b; 40 | } 41 | 42 | return (s); 43 | } 44 | */ 45 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/1-memcpy.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _memcpy - copy n bytes of memory from source to destination 5 | * @dest: destination 6 | * @src: source 7 | * @n: number of bytes 8 | * Return: pointer to dest 9 | */ 10 | 11 | char *_memcpy(char *dest, char *src, unsigned int n) 12 | { 13 | 14 | int size = n; /* only accept positive sizes */ 15 | 16 | if (size > 0) 17 | { 18 | int i; 19 | 20 | for (i = 0; i < size; i++) 21 | dest[i] = src[i]; 22 | } 23 | 24 | return (dest); 25 | } 26 | 27 | /* pointer arithmetic version 28 | 29 | char *_memcpy(char *dest, char *src, unsigned int n) 30 | { 31 | 32 | int size = n; 33 | 34 | if (size > 0) 35 | { 36 | int i; 37 | 38 | for (i = 0; i < size; i++) 39 | *(dest + i) = *(src + i); 40 | } 41 | 42 | return (dest); 43 | } 44 | */ 45 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/101-crackme_password: -------------------------------------------------------------------------------- 1 | abc123 -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/2-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char *s = "hello"; 12 | char *f; 13 | 14 | f = _strchr(s, 'l'); 15 | printf("%s\n", f); 16 | 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/2-strchr.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #define NULL 0 3 | 4 | /** 5 | * _strchr - locate 1st occurrence of char in string and returns pointer there 6 | * @s: string to search 7 | * @c: target characer 8 | * Return: pointer to that character in string 9 | */ 10 | 11 | char *_strchr(char *s, char c) 12 | { 13 | int i = 0; 14 | 15 | while (s[i] != '\0' && s[i] != c) /* find match */ 16 | i++; 17 | 18 | if (s[i] == c) /* if match, assign to address */ 19 | return (&s[i]); 20 | else 21 | return (NULL); 22 | } 23 | 24 | /* pointer arithmetic version 25 | 26 | char *_strchr(char *s, char c) 27 | { 28 | int i = 0; 29 | 30 | while (*(s + i) && *(s + i) != c) 31 | i++; 32 | 33 | if (*(s + i) == c) 34 | return (s + i); 35 | else 36 | return (NULL); 37 | } 38 | */ 39 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/3-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char *s = "hello, world"; 12 | char *f = "oleh"; 13 | unsigned int n; 14 | 15 | n = _strspn(s, f); 16 | printf("%u\n", n); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/4-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char *s = "hello, world"; 12 | char *f = "world"; 13 | char *t; 14 | 15 | t = _strpbrk(s, f); 16 | printf("%s\n", t); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/4-strpbrk.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #define NULL 0 3 | 4 | /** 5 | * _strpbrk - return pointer to byte in s that matches a byte in accept 6 | * @s: string to search 7 | * @accept: target matches 8 | * Return: pointer to index of string at first occurence 9 | */ 10 | 11 | char *_strpbrk(char *s, char *accept) 12 | { 13 | int i = 0, j; 14 | 15 | while (s[i] != '\0') /*iterate through string*/ 16 | { 17 | 18 | for (j = 0; accept[j] != '\0'; j++) /* iterate through target */ 19 | { 20 | if (s[i] == accept[j]) /* stop at first match */ 21 | { 22 | s = &s[i]; /* set pointer to first occurence */ 23 | return (s); 24 | } 25 | } 26 | i++; 27 | } 28 | return (NULL); /* return NULL if no matches */ 29 | 30 | } 31 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/5-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char *s = "hello, world"; 12 | char *f = "lo"; 13 | char *t; 14 | 15 | t = _strstr(s, f); 16 | printf("%s\n", t); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/7-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char board[8][8] = { 12 | {'r', 'k', 'b', 'q', 'k', 'b', 'k', 'r'}, 13 | {'p', 'p', 'p', 'p', 'p', 'p', 'p', 'p'}, 14 | {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}, 15 | {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}, 16 | {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}, 17 | {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}, 18 | {'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P'}, 19 | {'R', 'K', 'B', 'Q', 'K', 'B', 'K', 'R'}, 20 | }; 21 | print_chessboard(board); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/7-print_chessboard.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * print_chessboard - print chessboard given set 2D array 5 | * @a: 2D array 6 | */ 7 | 8 | void print_chessboard(char (*a)[8]) 9 | { 10 | 11 | int row; 12 | int column; 13 | 14 | for (row = 0; row < 8; row++) 15 | { 16 | for (column = 0; column < 8; column++) 17 | _putchar(a[row][column]); 18 | _putchar('\n'); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/8-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int c3[3][3] = { 12 | {0, 1, 5}, 13 | {10, 11, 12}, 14 | {1000, 101, 102}, 15 | }; 16 | int c5[5][5] = { 17 | {0, 1, 5, 12124, 1234}, 18 | {10, 11, 12, 123521, 12512}, 19 | {1000, 101, 102, 12545, 214543435}, 20 | {100, 1012451, 11102, 12545, 214543435}, 21 | {10, 12401, 10452, 11542545, 1214543435}, 22 | }; 23 | print_diagsums((int *)c3, 3); 24 | print_diagsums((int *)c5, 5); 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/8-print_diagsums.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * print_diagsums - print sums of diagonals in matrix 6 | * @a: matrix 7 | * @size: size of matrix 8 | */ 9 | 10 | void print_diagsums(int *a, int size) 11 | { 12 | 13 | int diagonal_sum_1 = 0; 14 | int diagonal_sum_2 = 0; 15 | int row, i; 16 | 17 | for (row = 0; row < size; row++) 18 | { 19 | i = (row * size) + row; 20 | diagonal_sum_1 += a[i]; 21 | } 22 | 23 | for (row = 1; row <= size; row++) 24 | { 25 | i = (row * size) - row; 26 | diagonal_sum_2 += a[i]; 27 | } 28 | 29 | printf("%d, %d\n", diagonal_sum_1, diagonal_sum_2); 30 | 31 | } 32 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/9-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char *s0 = "Anne Cognet"; 12 | char *s1 = "Asaia Palacios"; 13 | 14 | printf("%s, %s\n", s0, s1); 15 | set_string(&s1, s0); 16 | printf("%s, %s\n", s0, s1); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/9-set_string.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * set_string - set value of pointer to a char 5 | * @s: variable of type pointer 6 | * @to: char 7 | */ 8 | 9 | void set_string(char **s, char *to) 10 | { 11 | *s = to; 12 | } 13 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _putchar - writes the character c to stdout 5 | * @c: The character to print 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | int _putchar(char c) 11 | { 12 | return (write(1, &c, 1)); 13 | } 14 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/crackme2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MelissaN/holbertonschool-low_level_programming/b45ee1afb3ce2bf5a61d65623fcecbe36bad26d2/0x06-pointers_arrays_strings/crackme2 -------------------------------------------------------------------------------- /0x07-recursion/0-puts_recursion.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _puts_recursion - print string with new line 5 | * @s: string to print 6 | */ 7 | 8 | void _puts_recursion(char *s) 9 | { 10 | if (*s == '\0') 11 | { 12 | _putchar('\n'); 13 | return; 14 | } 15 | _putchar(*s); 16 | s++; 17 | _puts_recursion(s); 18 | } 19 | -------------------------------------------------------------------------------- /0x07-recursion/1-print_rev_recursion.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _print_rev_recursion - print string in reverse 5 | * @s: string to print 6 | */ 7 | 8 | void _print_rev_recursion(char *s) 9 | { 10 | if (*s == '\0') 11 | return; 12 | 13 | _print_rev_recursion(s + 1); 14 | _putchar(*s); 15 | } 16 | -------------------------------------------------------------------------------- /0x07-recursion/100-wildcmp_NONRECURSIVE.c: -------------------------------------------------------------------------------- 1 | /** 2 | * wildcmp - compare if string with wildcard mattches 3 | * @s1: one string 4 | * @s2: one string 5 | * Return: 1 if matched, 0 if not 6 | */ 7 | 8 | int wildcmp(char *s1, char *s2) 9 | { 10 | int i = 0, j = 0; 11 | while (s1[i] != '\0' && s2[j] != '\0') 12 | { 13 | if (s1[i] == s2[j]) 14 | i++; j++; 15 | if (s2[j] == "*") 16 | { 17 | j++; 18 | while ((s1[i] != s2[j]) && s1[i] != '\0') 19 | i++; 20 | } 21 | if (s1[i] != s2[j]) 22 | return (0); 23 | } 24 | return (1); 25 | } 26 | -------------------------------------------------------------------------------- /0x07-recursion/2-strlen_recursion.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _strlen_recursion - return string length 5 | * @s: string 6 | * Return: string length 7 | */ 8 | 9 | int _strlen_recursion(char *s) 10 | { 11 | if (*s == '\0') 12 | return (0); 13 | 14 | s++; 15 | return (1 + _strlen_recursion(s)); 16 | } 17 | -------------------------------------------------------------------------------- /0x07-recursion/3-factorial.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * factorial - find factorial of n 5 | * @n: number 6 | * Return: factorial of number, -1 if negative n 7 | */ 8 | 9 | int factorial(int n) 10 | { 11 | if (n < 0) 12 | return (-1); 13 | else if (n == 0 || n == 1) 14 | return (n); 15 | else 16 | return (n * factorial(n - 1)); 17 | } 18 | -------------------------------------------------------------------------------- /0x07-recursion/4-pow_recursion.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _pow_recursion - raise x to power y 5 | * @x: number 6 | * @y: power to raise to 7 | * Return: power of a number, -1 if power less than 0 8 | */ 9 | 10 | int _pow_recursion(int x, int y) 11 | { 12 | if (y < 0) 13 | return (-1); 14 | 15 | if (x == 0) 16 | return (0); 17 | 18 | if (x == 1 || y == 0) 19 | return (1); 20 | 21 | return (x * _pow_recursion(x, y - 1)); 22 | } 23 | -------------------------------------------------------------------------------- /0x07-recursion/5-sqrt_recursion.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * find_root - find square root of n, starting from the smallest possible, 0 5 | * @n: n 6 | * @root: test this root 7 | * Return: natural square root, or -1 if not natural root 8 | */ 9 | 10 | int find_root(int n, int root) 11 | { 12 | if (root * root > n) 13 | return (-1); 14 | 15 | if (root * root == n) 16 | return (root); 17 | 18 | return (find_root(n, root + 1)); 19 | } 20 | 21 | /** 22 | * _sqrt_recursion - find natural square root of n 23 | * @n: n 24 | * Return: natural square root, or -1 if not natural root 25 | */ 26 | 27 | int _sqrt_recursion(int n) 28 | { 29 | if (n < 0) 30 | return (-1); 31 | 32 | return (find_root(n, 0)); 33 | } 34 | -------------------------------------------------------------------------------- /0x07-recursion/6-is_prime_number.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * is_prime - recursively divide by higher divisor, skip even nums 5 | * @n: number to check if prime 6 | * @divisor: divisor 7 | * Return: 1 if prime, 0 if not, or recursive function call 8 | */ 9 | 10 | int is_prime(int n, int divisor) 11 | { 12 | if (n == divisor) 13 | return (1); 14 | if (n % divisor == 0) 15 | return (0); 16 | return (is_prime(n, divisor + 1)); 17 | 18 | } 19 | 20 | /** 21 | * is_prime_number - check if prime 22 | * @n: number to check 23 | * Return: 1 if prime, 0 if not 24 | */ 25 | 26 | int is_prime_number(int n) 27 | { 28 | int divisor = 3; 29 | 30 | if (n % 2 == 0 || n < 2) 31 | return (0); 32 | if (n == 2) 33 | return (1); 34 | 35 | return (is_prime(n, divisor)); 36 | } 37 | -------------------------------------------------------------------------------- /0x07-recursion/7-is_palindrome.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * compare - compare head and tail indices for match 5 | * @head: index starting from left of string 6 | * @tail: index starting from right of string, moving backwards 7 | * Return: 1 if palindrome, 0 if not 8 | */ 9 | 10 | int compare(char *head, char *tail) 11 | { 12 | 13 | if (head >= tail) 14 | return (1); 15 | if (*head == *tail) 16 | return (compare(head + 1, tail - 1)); 17 | 18 | return (0); 19 | } 20 | 21 | /** 22 | * _strlen - find length of string to access last index 23 | * @s: string 24 | * Return: length 25 | */ 26 | 27 | int _strlen(char *s) 28 | { 29 | 30 | if (*s == '\0') 31 | return (0); 32 | s++; 33 | return (1 + (_strlen(s))); 34 | } 35 | 36 | /** 37 | * is_palindrome - check if palindrome 38 | * @s: string to check 39 | * Return: 1 if palindrome, 0 if not 40 | */ 41 | 42 | int is_palindrome(char *s) 43 | { 44 | int len = _strlen(s); 45 | 46 | return (compare(s, (s + len - 1))); 47 | } 48 | -------------------------------------------------------------------------------- /0x07-recursion/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _putchar - writes the character c to stdout 5 | * @c: The character to print 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | int _putchar(char c) 11 | { 12 | return (write(1, &c, 1)); 13 | } 14 | -------------------------------------------------------------------------------- /0x07-recursion/test_files/0-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * main - check the code for Holberton School students. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | _puts_recursion(""); 11 | _puts_recursion("\0"); 12 | _puts_recursion("B"); 13 | _puts_recursion("Betty Holberton"); 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x07-recursion/test_files/1-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * main - check the code for Holberton School students. 5 | * 6 | * Return: Always 0. 7 | */ 8 | 9 | int main(void) 10 | { 11 | _print_rev_recursion("\nColton Walker"); 12 | _print_rev_recursion("\nCo"); 13 | _print_rev_recursion("\n\0"); 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x07-recursion/test_files/100-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int r; 12 | 13 | r = wildcmp("main.c", "*.c"); 14 | printf("%d\n", r); 15 | r = wildcmp("main.c", "m*a*i*n*.*c*"); 16 | printf("%d\n", r); 17 | r = wildcmp("main.c", "main.c"); 18 | printf("%d\n", r); 19 | r = wildcmp("main.c", "m*c"); 20 | printf("%d\n", r); 21 | r = wildcmp("main.c", "ma********************************c"); 22 | printf("%d\n", r); 23 | r = wildcmp("main.c", "*"); 24 | printf("%d\n", r); 25 | r = wildcmp("main.c", "***"); 26 | printf("%d\n", r); 27 | r = wildcmp("main.c", "m.*c"); 28 | printf("%d\n", r); 29 | r = wildcmp("main.c", "**.*c"); 30 | printf("%d\n", r); 31 | r = wildcmp("main-main.c", "ma*in.c"); 32 | printf("%d\n", r); 33 | r = wildcmp("main", "main*d"); 34 | printf("%d\n", r); 35 | r = wildcmp("abc", "*b"); 36 | printf("%d\n", r); 37 | return (0); 38 | } 39 | -------------------------------------------------------------------------------- /0x07-recursion/test_files/2-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | 10 | int main(void) 11 | { 12 | int n; 13 | 14 | n = _strlen_recursion("Corbin Coleman"); 15 | printf("%d\n", n); 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x07-recursion/test_files/3-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int r; 12 | 13 | r = factorial(1); 14 | printf("%d\n", r); 15 | r = factorial(5); 16 | printf("%d\n", r); 17 | r = factorial(10); 18 | printf("%d\n", r); 19 | r = factorial(-1024); 20 | printf("%d\n", r); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x07-recursion/test_files/4-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int r; 12 | 13 | r = _pow_recursion(1, 10); 14 | printf("%d\n", r); 15 | r = _pow_recursion(1024, 0); 16 | printf("%d\n", r); 17 | r = _pow_recursion(2, 16); 18 | printf("%d\n", r); 19 | r = _pow_recursion(5, 2); 20 | printf("%d\n", r); 21 | r = _pow_recursion(5, -2); 22 | printf("%d\n", r); 23 | r = _pow_recursion(-5, 3); 24 | printf("%d\n", r); 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /0x07-recursion/test_files/5-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int r; 12 | 13 | r = _sqrt_recursion(0); 14 | printf("%d\n", r); 15 | r = _sqrt_recursion(1); 16 | printf("%d\n", r); 17 | r = _sqrt_recursion(1024); 18 | printf("%d\n", r); 19 | r = _sqrt_recursion(16); 20 | printf("%d\n", r); 21 | r = _sqrt_recursion(17); 22 | printf("%d\n", r); 23 | r = _sqrt_recursion(2147483647); 24 | printf("%d\n", r); 25 | r = _sqrt_recursion(-5); 26 | printf("%d\n", r); 27 | return (0); 28 | } 29 | -------------------------------------------------------------------------------- /0x07-recursion/test_files/6-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int r; 12 | 13 | r = is_prime_number(1); 14 | printf("%d\n", r); 15 | r = is_prime_number(1024); 16 | printf("%d\n", r); 17 | r = is_prime_number(16); 18 | printf("%d\n", r); 19 | r = is_prime_number(17); 20 | printf("%d\n", r); 21 | r = is_prime_number(25); 22 | printf("%d\n", r); 23 | r = is_prime_number(-1); 24 | printf("%d\n", r); 25 | r = is_prime_number(113); 26 | printf("%d\n", r); 27 | r = is_prime_number(7919); 28 | printf("%d\n", r); 29 | return (0); 30 | } 31 | -------------------------------------------------------------------------------- /0x07-recursion/test_files/7-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int r; 12 | 13 | r = is_palindrome("level"); 14 | printf("%d\n", r); 15 | r = is_palindrome("abcdecba"); 16 | printf("%d\n", r); 17 | r = is_palindrome("holberton"); 18 | printf("%d\n", r); 19 | r = is_palindrome("step on no pets"); 20 | printf("%d\n", r); 21 | r = is_palindrome(" "); 22 | printf("%d\n", r); 23 | r = is_palindrome("0 0"); 24 | printf("%d\n", r); 25 | 26 | 27 | return (0); 28 | } 29 | -------------------------------------------------------------------------------- /0x08-static_libraries/.gitignore: -------------------------------------------------------------------------------- 1 | /* don't show these files */ 2 | 3 | *~ 4 | *.o 5 | *.c -------------------------------------------------------------------------------- /0x08-static_libraries/README.md: -------------------------------------------------------------------------------- 1 | # C STATIC LIBRARIES 2 | 3 | ## Requirements 4 | * Language: C 5 | * OS: Ubuntu 14.04 LTS 6 | * Compiler: gcc 4.8.4 7 | * Style guidelines: [Betty style] (https://github.com/holbertonschool/Betty/wiki) 8 | 9 | ## Synopsis 10 | This repository holds some projects I worked on for Holberton School. The goal is to learn coding and concepts through the C programming language. 11 | 12 | ## Concepts to be learned in this repository: 13 | * what a static library is 14 | * how it works 15 | * how to create one `ar -cr` 16 | * how to use it 17 | 18 | ## Description of what each file shows: 19 | 1. libholberton.a - created this static library containing some functions 20 | 2. holberton.h - included function prototypes of functions in library 21 | 3. create_static_lib.sh - shell script to compile pre linking and create static library 22 | 23 | ## Authors 24 | * Melissa Ng 25 | 26 | ## License 27 | * The Holberton School 28 | -------------------------------------------------------------------------------- /0x08-static_libraries/create_static_lib.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -Wall -pedantic -Werror -Wextra -c *.c 3 | ar -cr liball.a *.o 4 | -------------------------------------------------------------------------------- /0x08-static_libraries/holberton.h: -------------------------------------------------------------------------------- 1 | #ifndef HOLBERTON_H 2 | #define HOLBERTON_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 | -------------------------------------------------------------------------------- /0x08-static_libraries/libholberton.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MelissaN/holbertonschool-low_level_programming/b45ee1afb3ce2bf5a61d65623fcecbe36bad26d2/0x08-static_libraries/libholberton.a -------------------------------------------------------------------------------- /0x09-argc_argv/0-whatsmyname.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "holberton.h" 3 | 4 | /** 5 | * main - print name of program 6 | * @argc: argument counter 7 | * @argv: pointer to array of arguments 8 | * Return: 0 on success 9 | */ 10 | 11 | int main(int argc, char *argv[]) 12 | { 13 | if (argc > 0) 14 | printf("%s\n", argv[0]); 15 | 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x09-argc_argv/1-args.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "holberton.h" 3 | 4 | /** 5 | * main - print number of arguments passed to program 6 | * @argc: argument counter 7 | * @argv: pointer to array of arguments 8 | * Return: 0 on success 9 | */ 10 | 11 | int main(int argc, char *argv[] __attribute__((unused))) 12 | { 13 | if (argc > 0) 14 | printf("%d\n", argc - 1); 15 | 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x09-argc_argv/100-change.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "holberton.h" 4 | 5 | /** 6 | * main - prints the minimum number of coins for an amount of money 7 | * @argc: should count two arguments 8 | * @argv: arguments given should be program name and amount of money 9 | * Return: least number of coins, 0 if negative amount, 1 if amount not given 10 | */ 11 | 12 | int main(int argc, char *argv[]) 13 | { 14 | int n, coins = 0; 15 | 16 | /* validate input */ 17 | if (argc != 2) 18 | { 19 | printf("Error\n"); 20 | return (1); 21 | } 22 | 23 | if (argv[1][0] == '-') 24 | { 25 | printf("0\n"); 26 | return (0); 27 | } 28 | 29 | /* convert string to int and calculate coins */ 30 | n = atoi(argv[1]); 31 | 32 | coins += n / 25; 33 | n = n % 25; 34 | coins += n / 10; 35 | n = n % 10; 36 | coins += n / 5; 37 | n = n % 5; 38 | coins += n / 2; 39 | n = n % 2; 40 | coins += n / 1; 41 | 42 | printf("%d\n", coins); 43 | return (0); 44 | } 45 | -------------------------------------------------------------------------------- /0x09-argc_argv/2-args.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "holberton.h" 3 | 4 | /** 5 | * main - print each argument passed to program on a line 6 | * @argc: argument counter 7 | * @argv: pointer to array of arguments 8 | * Return: 0 on success 9 | */ 10 | 11 | int main(int argc, char *argv[]) 12 | { 13 | int i = 0; 14 | 15 | while (i < argc) 16 | printf("%s\n", argv[i++]); 17 | 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /0x09-argc_argv/3-mul.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "holberton.h" 4 | 5 | /** 6 | * main - print product of argument numbers 7 | * @argc: argument counter 8 | * @argv: numbers to multiply 9 | * Return: 0 on success, 1 if two arguments not given 10 | */ 11 | 12 | int main(int argc, char *argv[]) 13 | { 14 | 15 | /* validate input */ 16 | if (argc != 3) 17 | { 18 | printf("Error\n"); 19 | return (1); 20 | } 21 | 22 | /* mulitply two arguments passed via cmd line */ 23 | printf("%d\n", atoi(argv[1]) * atoi(argv[2])); 24 | return (0); 25 | } 26 | -------------------------------------------------------------------------------- /0x09-argc_argv/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _putchar - writes the character c to stdout 5 | * @c: The character to print 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | int _putchar(char c) 11 | { 12 | return (write(1, &c, 1)); 13 | } 14 | -------------------------------------------------------------------------------- /0x09-argc_argv/holberton.h: -------------------------------------------------------------------------------- 1 | #ifndef HOLBERTON_H 2 | #define HOLBERTON_H 3 | 4 | /** 5 | * _putchar - writes the character c to stdout 6 | * @c: The character to print 7 | * 8 | * Return: On success 1. 9 | * On error, -1 is returned, and errno is set appropriately. 10 | */ 11 | 12 | int _putchar(char c); 13 | 14 | #endif 15 | -------------------------------------------------------------------------------- /0x0A-malloc_free/0-create_array.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "holberton.h" 4 | 5 | /** 6 | * create_array - create array of chars and initialize with a specific char 7 | * @size: size of array 8 | * @c: fill array values with this char 9 | * Return: pointer to array 10 | */ 11 | 12 | char *create_array(unsigned int size, char c) 13 | { 14 | char *arr; 15 | int i = 0; 16 | 17 | if (size <= 0) /* validate size input */ 18 | return (NULL); 19 | arr = malloc(sizeof(char) * size); /* allocate memory */ 20 | 21 | if (arr == NULL) /* validate memory */ 22 | return (NULL); 23 | 24 | while (i < (int)size) /* set array values to char c */ 25 | { 26 | *(arr + i) = c; 27 | i++; 28 | } 29 | *(arr + i) = '\0'; 30 | 31 | return (arr); 32 | } 33 | -------------------------------------------------------------------------------- /0x0A-malloc_free/0-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * simple_print_buffer - prints buffer in hexa 7 | * @buffer: the address of memory to print 8 | * @size: the size of the memory to print 9 | * 10 | * Return: Nothing. 11 | */ 12 | void simple_print_buffer(char *buffer, unsigned int size) 13 | { 14 | unsigned int i; 15 | 16 | i = 0; 17 | while (i < size) 18 | { 19 | if (i % 10) 20 | { 21 | printf(" "); 22 | } 23 | if (!(i % 10) && i) 24 | { 25 | printf("\n"); 26 | } 27 | printf("0x%02x", buffer[i]); 28 | i++; 29 | } 30 | printf("\n"); 31 | } 32 | 33 | /** 34 | * main - check the code for Holberton School students. 35 | * 36 | * Return: Always 0. 37 | */ 38 | int main(void) 39 | { 40 | char *buffer; 41 | 42 | buffer = create_array(98, 'H'); 43 | if (buffer == NULL) 44 | { 45 | printf("failed to allocate memory\n"); 46 | return (1); 47 | } 48 | simple_print_buffer(buffer, 98); 49 | free(buffer); 50 | return (0); 51 | } 52 | -------------------------------------------------------------------------------- /0x0A-malloc_free/1-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * main - check the code for Holberton School students. 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(void) 11 | { 12 | char *s; 13 | 14 | s = _strdup("teststring"); 15 | if (s == NULL) 16 | { 17 | printf("failed to allocate memory\n"); 18 | return (1); 19 | } 20 | printf("%s\n", s); 21 | free(s); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x0A-malloc_free/1-strdup.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "holberton.h" 4 | 5 | /** 6 | * _strdup - returns a pointer to a newly allocated space in memory, 7 | * which contains a copy of the string given as a parameter 8 | * @str: string to duplicate 9 | * Return: pointer to duplicated string in allocated memory 10 | */ 11 | 12 | char *_strdup(char *str) 13 | { 14 | char *duplicate_str; 15 | int i = 0, len = 0; 16 | 17 | if (str == NULL) /* validate str input */ 18 | return (NULL); 19 | 20 | while (*(str + i)) 21 | len++, i++; 22 | len++; /* add null terminator to length */ 23 | 24 | duplicate_str = malloc(sizeof(char) * len); /* allocate memory */ 25 | 26 | if (duplicate_str == NULL) /* validate memory */ 27 | return (NULL); 28 | 29 | i = 0; 30 | while (i < len) 31 | { 32 | *(duplicate_str + i) = *(str + i); 33 | i++; 34 | } 35 | 36 | return (duplicate_str); 37 | } 38 | -------------------------------------------------------------------------------- /0x0A-malloc_free/2-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * main - check the code for Holberton School students. 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(void) 11 | { 12 | char *s; 13 | 14 | s = str_concat("string2isEmpty", ""); 15 | if (s == NULL) 16 | { 17 | printf("failed\n"); 18 | return (1); 19 | } 20 | printf("%s\n", s); 21 | free(s); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x0A-malloc_free/2-str_concat.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "holberton.h" 4 | 5 | /** 6 | * str_concat - concatenates two strings 7 | * @s1: string 1 8 | * @s2: string 2 9 | * Return: pointer to concatenated string 10 | */ 11 | 12 | char *str_concat(char *s1, char *s2) 13 | { 14 | char *concat; 15 | int len1 = 0, len2 = 0, i = 0, j = 0; 16 | 17 | if (s1 == NULL) 18 | s1 = ""; 19 | if (s2 == NULL) 20 | s2 = ""; 21 | while (*(s1 + i)) 22 | len1++, i++; 23 | while (*(s2 + j)) 24 | len2++, j++; 25 | len2++; /* add null terminator to length */ 26 | 27 | concat = malloc(sizeof(char) * (len1 + len2)); /*alloc memory*/ 28 | 29 | if (concat == NULL) /* validate memory */ 30 | return (NULL); 31 | 32 | i = 0, j = 0; 33 | while (i < len1) /* concatenate */ 34 | { 35 | *(concat + i) = *(s1 + i); 36 | i++; 37 | } 38 | while (j < len2) 39 | { 40 | *(concat + i) = *(s2 + j); 41 | i++, j++; 42 | } 43 | 44 | return (concat); 45 | } 46 | -------------------------------------------------------------------------------- /0x0A-malloc_free/3-alloc_grid.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "holberton.h" 4 | 5 | /** 6 | * alloc_grid - returns a pointer to a 2 dimensional array of integers 7 | * @width: columns 8 | * @height: rows 9 | * Return: pointer to 2d array 10 | */ 11 | 12 | int **alloc_grid(int width, int height) 13 | { 14 | int **grid; 15 | int i, j; 16 | 17 | if (width <= 0 || height <= 0) /* validate input */ 18 | return (NULL); 19 | 20 | grid = malloc(height * sizeof(int *)); /*allocate memory for rows*/ 21 | 22 | if (grid == NULL) /* validate memory */ 23 | return (NULL); 24 | 25 | for (i = 0; i < height; i++) /*allocate memory for columns of each row*/ 26 | { 27 | grid[i] = malloc(width * sizeof(int)); 28 | if (grid[i] == NULL) /* validate memory */ 29 | { 30 | for (i = 0; i < height; i++) 31 | free(grid[i]); 32 | free(grid); 33 | return (NULL); 34 | } 35 | for (j = 0; j < width; j++) /* set array values to 0 */ 36 | grid[i][j] = 0; 37 | } 38 | 39 | return (grid); 40 | } 41 | -------------------------------------------------------------------------------- /0x0A-malloc_free/3-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * print_grid - prints a grid of integers 7 | * @grid: the address of the two dimensional grid 8 | * @width: width of the grid 9 | * @height: height of the grid 10 | * 11 | * Return: Nothing. 12 | */ 13 | void print_grid(int **grid, int width, int height) 14 | { 15 | int w; 16 | int h; 17 | 18 | h = 0; 19 | while (h < height) 20 | { 21 | w = 0; 22 | while (w < width) 23 | { 24 | printf("%d ", grid[h][w]); 25 | w++; 26 | } 27 | printf("\n"); 28 | h++; 29 | } 30 | } 31 | 32 | /** 33 | * main - check the code for Holberton School students. 34 | * 35 | * Return: Always 0. 36 | */ 37 | int main(void) 38 | { 39 | int **grid; 40 | 41 | grid = alloc_grid(6, 4); 42 | if (grid == NULL) 43 | { 44 | return (1); 45 | } 46 | print_grid(grid, 6, 4); 47 | printf("\n"); 48 | grid[0][3] = 98; 49 | grid[3][4] = 402; 50 | print_grid(grid, 6, 4); 51 | return (0); 52 | } 53 | -------------------------------------------------------------------------------- /0x0A-malloc_free/4-free_grid.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "holberton.h" 4 | 5 | /** 6 | * free_grid - frees a 2 dimensional grid 7 | * previously created by your alloc_grid function 8 | * @grid: pointer to 2D array 9 | * @height: height of array 10 | */ 11 | 12 | void free_grid(int **grid, int height) 13 | { 14 | int i; 15 | 16 | if (grid == NULL) 17 | free(grid); 18 | 19 | for (i = 0; i < height; i++) 20 | free(grid[i]); 21 | 22 | free(grid); 23 | } 24 | -------------------------------------------------------------------------------- /0x0A-malloc_free/5-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * main - check the code for Holberton School students. 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(int ac, char *av[]) 11 | { 12 | char *s; 13 | 14 | s = argstostr(ac, av); 15 | if (s == NULL) 16 | { 17 | return (1); 18 | } 19 | printf("%s", s); 20 | free(s); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x0A-malloc_free/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _putchar - writes the character c to stdout 5 | * @c: The character to print 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | int _putchar(char c) 11 | { 12 | return (write(1, &c, 1)); 13 | } 14 | -------------------------------------------------------------------------------- /0x0A-malloc_free/holberton.h: -------------------------------------------------------------------------------- 1 | #ifndef HOLBERTON_H 2 | #define HOLBERTON_H 3 | 4 | int _putchar(char c); 5 | char *create_array(unsigned int size, char c); 6 | char *_strdup(char *str); 7 | char *str_concat(char *s1, char *s2); 8 | int **alloc_grid(int width, int height); 9 | void free_grid(int **grid, int height); 10 | char *argstostr(int ac, char **av); 11 | char **strtow(char *str); 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /0x0B-more_malloc_free/0-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | #include 4 | #include 5 | 6 | /** 7 | * main - check the code for Holberton School students. 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | char *c; 14 | int *i; 15 | float *f; 16 | double *d; 17 | 18 | c = malloc_checked(sizeof(char) * 1024); 19 | printf("%p\n", (void *)c); 20 | i = malloc_checked(sizeof(int) * 402); 21 | printf("%p\n", (void *)i); 22 | f = malloc_checked(sizeof(float) * 100000000); 23 | printf("%p\n", (void *)f); 24 | d = malloc_checked(INT_MAX); 25 | printf("%p\n", (void *)d); 26 | free(c); 27 | free(i); 28 | free(f); 29 | free(d); 30 | return (0); 31 | } 32 | -------------------------------------------------------------------------------- /0x0B-more_malloc_free/0-malloc_checked.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "holberton.h" 3 | 4 | /** 5 | * malloc_checked - allocates memory using malloc 6 | * @b: size to malloc 7 | * Return: pointer to allocated memory 8 | */ 9 | 10 | void *malloc_checked(unsigned int b) 11 | { 12 | void *ptr; 13 | 14 | ptr = malloc(b); 15 | 16 | if (ptr == NULL) 17 | exit(98); 18 | 19 | return (ptr); 20 | } 21 | -------------------------------------------------------------------------------- /0x0B-more_malloc_free/1-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * main - check the code for Holberton School students. 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(void) 11 | { 12 | char *concat; 13 | 14 | concat = string_nconcat("Holberton ", "School !!!", 6); 15 | printf("%s\n", concat); 16 | free(concat); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x0B-more_malloc_free/2-calloc.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "holberton.h" 3 | 4 | /** 5 | * _calloc - allocate memory and set all values to 0 6 | * @nmemb: size 7 | * @size: sizeof(datatype) 8 | * Return: pointer to calloc'd string 9 | */ 10 | 11 | void *_calloc(unsigned int nmemb, unsigned int size) 12 | { 13 | void *ptr; 14 | unsigned int i; /* match unsigned arguments */ 15 | 16 | if (nmemb <= 0 || size <= 0) /* validate input */ 17 | return (NULL); 18 | 19 | /* allocate memory and check if error */ 20 | ptr = malloc(nmemb * size); 21 | if (ptr == NULL) 22 | return (NULL); 23 | 24 | /* set allocated memory values to 0 */ 25 | for (i = 0; i < nmemb * size; i++) 26 | *((char *)ptr + i) = 0; /* type cast assigning values*/ 27 | 28 | return (ptr); 29 | } 30 | -------------------------------------------------------------------------------- /0x0B-more_malloc_free/2-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | #include 4 | #include 5 | 6 | /** 7 | * simple_print_buffer - prints buffer in hexa 8 | * @buffer: the address of memory to print 9 | * @size: the size of the memory to print 10 | * 11 | * Return: Nothing. 12 | */ 13 | void simple_print_buffer(char *buffer, unsigned int size) 14 | { 15 | unsigned int i; 16 | 17 | i = 0; 18 | while (i < size) 19 | { 20 | if (i % 10) 21 | { 22 | printf(" "); 23 | } 24 | if (!(i % 10) && i) 25 | { 26 | printf("\n"); 27 | } 28 | printf("0x%02x", buffer[i]); 29 | i++; 30 | } 31 | printf("\n"); 32 | } 33 | 34 | /** 35 | * main - check the code for Holberton School students. 36 | * 37 | * Return: Always 0. 38 | */ 39 | int main(void) 40 | { 41 | char *a; 42 | 43 | a = _calloc(98, sizeof(char)); 44 | strcpy(a, "testing"); 45 | strcpy(a + 9, " test"); 46 | a[97] = '!'; 47 | simple_print_buffer(a, 98); 48 | free(a); 49 | return (0); 50 | } 51 | -------------------------------------------------------------------------------- /0x0B-more_malloc_free/3-array_range.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "holberton.h" 3 | 4 | /** 5 | * array_range - create array of integers 6 | * @min: start range from 7 | * @max: end range at 8 | * Return: pointer to array 9 | */ 10 | 11 | int *array_range(int min, int max) 12 | { 13 | int *ptr; 14 | int i; 15 | int n = (max - min + 1); 16 | 17 | if (min > max) /* validate input */ 18 | return (NULL); 19 | 20 | /* malloc and check for error */ 21 | ptr = malloc(sizeof(int) * n); 22 | if (ptr == NULL) 23 | return (NULL); 24 | 25 | /* set values */ 26 | for (i = 0; i < n; i++) 27 | ptr[i] = min++; 28 | 29 | return (ptr); 30 | } 31 | -------------------------------------------------------------------------------- /0x0B-more_malloc_free/3-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | #include 4 | #include 5 | 6 | /** 7 | * simple_print_buffer - prints buffer in hexa 8 | * @buffer: the address of memory to print 9 | * @size: the size of the memory to print 10 | * 11 | * Return: Nothing. 12 | */ 13 | void simple_print_buffer(int *buffer, unsigned int size) 14 | { 15 | unsigned int i; 16 | 17 | i = 0; 18 | while (i < size) 19 | { 20 | if (i % 10) 21 | { 22 | printf(" "); 23 | } 24 | if (!(i % 10) && i) 25 | { 26 | printf("\n"); 27 | } 28 | printf("0x%02x", buffer[i]); 29 | i++; 30 | } 31 | printf("\n"); 32 | } 33 | 34 | /** 35 | * main - check the code for Holberton School students. 36 | * 37 | * Return: Always 0. 38 | */ 39 | int main(void) 40 | { 41 | int *a; 42 | 43 | a = array_range(0, 10); 44 | simple_print_buffer(a, 11); 45 | free(a); 46 | return (0); 47 | } 48 | -------------------------------------------------------------------------------- /0x0B-more_malloc_free/holberton.h: -------------------------------------------------------------------------------- 1 | #ifndef HOLBERTON_H 2 | #define HOLBERTON_H 3 | 4 | void *malloc_checked(unsigned int b); 5 | char *string_nconcat(char *s1, char *s2, unsigned int n); 6 | void *_calloc(unsigned int nmemb, unsigned int size); 7 | int *array_range(int min, int max); 8 | void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size); 9 | 10 | #endif 11 | -------------------------------------------------------------------------------- /0x0C-preprocessor/0-main.c: -------------------------------------------------------------------------------- 1 | #include "0-object_like_macro.h" 2 | #include "0-object_like_macro.h" 3 | #include 4 | 5 | /** 6 | * main - check the code for Holberton School students. 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(void) 11 | { 12 | int s; 13 | 14 | s = 98 + SIZE; 15 | printf("%d\n", s); 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x0C-preprocessor/0-object_like_macro.h: -------------------------------------------------------------------------------- 1 | #ifndef OBJECT_LIKE_MACRO 2 | #define OBJECT_LIKE_MACRO 3 | 4 | #define SIZE 1024 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /0x0C-preprocessor/1-main.c: -------------------------------------------------------------------------------- 1 | #include "1-pi.h" 2 | #include "1-pi.h" 3 | #include 4 | 5 | /** 6 | * main - check the code for Holberton School students. 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(void) 11 | { 12 | float a; 13 | float r; 14 | 15 | r = 98; 16 | a = PI * r * r; 17 | printf("%.3f\n", a); 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /0x0C-preprocessor/1-pi.h: -------------------------------------------------------------------------------- 1 | #ifndef PI_H 2 | #define PI_H 3 | 4 | #define PI 3.14159265359 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /0x0C-preprocessor/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints current filename to std output 5 | * Return: 0 on success 6 | */ 7 | 8 | int main(void) 9 | { 10 | printf("%s\n", __FILE__); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x0C-preprocessor/3-function_like_macro.h: -------------------------------------------------------------------------------- 1 | #ifndef FUNCTION_LIKE_MACRO 2 | #define FUNCTION_LIKE_MACRO 3 | 4 | #define ABS(x) (((x) < 0) ? (-x) : (x)) 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /0x0C-preprocessor/3-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "3-function_like_macro.h" 3 | #include "3-function_like_macro.h" 4 | 5 | /** 6 | * main - check the code for Holberton School students. 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(void) 11 | { 12 | int i; 13 | int j; 14 | 15 | i = ABS(-98) * 10; 16 | j = ABS(98) * 10; 17 | printf("%d, %d\n", i, j); 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /0x0C-preprocessor/4-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "4-sum.h" 3 | #include "4-sum.h" 4 | 5 | /** 6 | * main - check the code for Holberton School students. 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(void) 11 | { 12 | int s; 13 | 14 | s = SUM(98, 1024); 15 | printf("%d\n", s); 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x0C-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-structures_typedef/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | 10 | int main(void) 11 | { 12 | struct dog my_dog; 13 | 14 | my_dog.name = "Django"; 15 | my_dog.age = 3.5; 16 | my_dog.owner = "Jay"; 17 | printf("My name is %s, and I am %.1f :) - Woof!\n", my_dog.name, my_dog.age); 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /0x0D-structures_typedef/1-init_dog.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | 4 | /** 5 | * init_dog - initialize a variable of type struct dog 6 | * @d: a variable of type struct dog 7 | * @name: member 8 | * @age: member 9 | * @owner: member 10 | */ 11 | 12 | void init_dog(struct dog *d, char *name, float age, char *owner) 13 | { 14 | 15 | if (d == NULL) /* validate if d initiated correctly */ 16 | return; 17 | 18 | d->name = name; 19 | d->age = age; 20 | d->owner = owner; 21 | } 22 | -------------------------------------------------------------------------------- /0x0D-structures_typedef/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | struct dog my_dog; 12 | 13 | init_dog(&my_dog, "Django", 3.5, "Jay"); 14 | printf("My name is %s, and I am %.1f :) - Woof!\n", my_dog.name, my_dog.age); 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x0D-structures_typedef/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | struct dog my_dog; 12 | 13 | my_dog.name = "Django"; 14 | my_dog.age = 3.5; 15 | /*my_dog.owner = "Jay";*/ 16 | print_dog(&my_dog); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x0D-structures_typedef/2-print_dog.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | 4 | /** 5 | * print_dog - prints all member info of a struct dog 6 | * @d: a variable of type struct dog 7 | */ 8 | 9 | void print_dog(struct dog *d) 10 | { 11 | 12 | if (d == NULL) /* validate if d initiated correctly */ 13 | return; 14 | 15 | (d->name == NULL) ? printf("Name: (nil)\n") 16 | : printf("Name: %s\n", d->name); 17 | 18 | printf("Age: %f\n", d->age); 19 | 20 | (d->owner == NULL) ? printf("Owner: (nil)\n") 21 | : printf("Owner: %s\n", d->owner); 22 | 23 | } 24 | -------------------------------------------------------------------------------- /0x0D-structures_typedef/3-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | dog_t my_dog; 12 | 13 | my_dog.name = "Django"; 14 | my_dog.age = 3.5; 15 | my_dog.owner = "Jay"; 16 | printf("My name is %s, and I am %.1f :) - Woof!\n", my_dog.name, my_dog.age); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x0D-structures_typedef/4-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | dog_t *my_dog; 12 | 13 | my_dog = new_dog(NULL, 3.75, "Jon Snow"); 14 | printf("My name is %s, my owner is %s, and I am %.1f :) - Woof!\n", my_dog->name, my_dog->owner, my_dog->age); 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x0D-structures_typedef/5-free_dog.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "dog.h" 4 | 5 | /** 6 | * free_dog - free instance of struct dog 7 | * @d: instance to be freed 8 | */ 9 | 10 | void free_dog(dog_t *d) 11 | { 12 | if (d == NULL) 13 | return; 14 | 15 | if (d->name) /* free members of struct that exist */ 16 | free(d->name); 17 | if (d->owner) 18 | free(d->owner); 19 | free(d); 20 | } 21 | -------------------------------------------------------------------------------- /0x0D-structures_typedef/5-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | dog_t *my_dog; 12 | 13 | my_dog = new_dog("Django", 3.5, "Jay"); 14 | printf("My name is %s, and I am %.1f :) - Woof!\n", my_dog->name, my_dog->age); 15 | free_dog(my_dog); 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x0D-structures_typedef/dog.h: -------------------------------------------------------------------------------- 1 | #ifndef DOG_H 2 | #define DOG_H 3 | 4 | /** 5 | * dog_t - Typedef for struct dog 6 | */ 7 | typedef struct dog dog_t; 8 | 9 | /** 10 | * struct dog - structure for dogs 11 | * @name: first member 12 | * @age: second member 13 | * @owner: third member 14 | */ 15 | struct dog 16 | { 17 | char *name; 18 | float age; 19 | char *owner; 20 | }; 21 | 22 | void init_dog(struct dog *d, char *name, float age, char *owner); 23 | void print_dog(struct dog *d); 24 | dog_t *new_dog(char *name, float age, char *owner); 25 | void free_dog(dog_t *d); 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /0x0E-function_pointers/0-print_name.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * print_name - execute a function given as a parameter 5 | * @name: parameter that function ptr needs 6 | * @f:function to execute 7 | * 8 | */ 9 | 10 | void print_name(char *name, void (*f)(char *)) 11 | { 12 | if (name != NULL && f != NULL) 13 | f(name); 14 | } 15 | -------------------------------------------------------------------------------- /0x0E-function_pointers/1-array_iterator.c: -------------------------------------------------------------------------------- 1 | #include 2 | /* defines size_t */ 3 | 4 | /** 5 | * array_iterator - execute function on an array via a function ptr 6 | * @array: array 7 | * @size: size of array 8 | * @action: pointer to function that we need to execute 9 | */ 10 | 11 | void array_iterator(int *array, size_t size, void (*action)(int)) 12 | { 13 | size_t i; /* match data type */ 14 | 15 | if (array != NULL && action != NULL) 16 | for (i = 0; i < size; i++) 17 | action(*(array + i)); 18 | } 19 | -------------------------------------------------------------------------------- /0x0E-function_pointers/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "function_pointers.h" 3 | 4 | /** 5 | * print_elem - prints an integer 6 | * @elem: the integer to print 7 | * 8 | * Return: Nothing. 9 | */ 10 | void print_elem(int elem) 11 | { 12 | printf("%d\n", elem); 13 | } 14 | 15 | /** 16 | * print_elem_hex - prints an integer, in hexadecimal 17 | * @elem: the integer to print 18 | * 19 | * Return: Nothing. 20 | */ 21 | void print_elem_hex(int elem) 22 | { 23 | printf("0x%x\n", elem); 24 | } 25 | 26 | /** 27 | * main - check the code for Holberton School students. 28 | * 29 | * Return: Always 0. 30 | */ 31 | int main(void) 32 | { 33 | int array[5] = {0, 98, 402, 1024, 4096}; 34 | 35 | array_iterator(array, 5, &print_elem); 36 | array_iterator(array, 5, &print_elem_hex); 37 | return (0); 38 | } 39 | -------------------------------------------------------------------------------- /0x0E-function_pointers/2-int_index.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * int_index - search for integer in array via function pointer 5 | * @array: array 6 | * @size: size of array 7 | * @cmp: pointer to searching/comparing function that we need to execute 8 | * Return: index where integer's found, -1 if not found or array not present 9 | */ 10 | 11 | int int_index(int *array, int size, int (*cmp)(int)) 12 | { 13 | int i; 14 | 15 | if (array == NULL || cmp == NULL || size <= 0) 16 | return (-1); 17 | 18 | /* iterate through array and call searching function */ 19 | for (i = 0; i < size; i++) 20 | if (cmp(*(array + i))) /* if returned true */ 21 | return (i); 22 | 23 | return (-1); 24 | 25 | } 26 | -------------------------------------------------------------------------------- /0x0E-function_pointers/3-calc.h: -------------------------------------------------------------------------------- 1 | #ifndef PROTOTYPES 2 | #define PROTOTYPES 3 | 4 | /** 5 | * struct op - Struct op 6 | * 7 | * @op: The operator 8 | * @f: The function associated 9 | */ 10 | 11 | typedef struct op 12 | { 13 | char *op; 14 | int (*f)(int a, int b); 15 | } op_t; 16 | 17 | /* functions to choose from for 5 different operations */ 18 | int op_add(int a, int b); 19 | int op_sub(int a, int b); 20 | int op_mul(int a, int b); 21 | int op_div(int a, int b); 22 | int op_mod(int a, int b); 23 | 24 | /* function to select correct operation function to perform */ 25 | int (*get_op_func(char *s))(int, int); 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /0x0E-function_pointers/3-get_op_func.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "3-calc.h" 3 | 4 | /** 5 | * get_op_func - function to select correct operation function 6 | * @s: operation given (+, *, -, /, %) 7 | * Return: pointer to correct operation function (0-4) 8 | */ 9 | 10 | int (*get_op_func(char *s))(int a, int b) 11 | { 12 | op_t ops[] = { 13 | {"+", op_add}, 14 | {"-", op_sub}, 15 | {"*", op_mul}, 16 | {"/", op_div}, 17 | {"%", op_mod}, 18 | {NULL, NULL} 19 | }; 20 | int i = 0; 21 | 22 | while (ops[i].op != NULL) 23 | { 24 | if (*s == *ops[i].op) 25 | return (ops[i].f); 26 | i++; 27 | } 28 | return (NULL); 29 | } 30 | -------------------------------------------------------------------------------- /0x0E-function_pointers/3-op_functions.c: -------------------------------------------------------------------------------- 1 | /** 2 | * op_add - add 3 | * @a: integer 4 | * @b: integer 5 | * Return: result 6 | */ 7 | int op_add(int a, int b) 8 | { 9 | return (a + b); 10 | } 11 | 12 | /** 13 | * op_sub - subtract 14 | * @a: integer 15 | * @b: integer 16 | * Return: result 17 | */ 18 | int op_sub(int a, int b) 19 | { 20 | return (a - b); 21 | } 22 | 23 | /** 24 | * op_mul - multiply 25 | * @a: integer 26 | * @b: integer 27 | * Return: result 28 | */ 29 | int op_mul(int a, int b) 30 | { 31 | return (a * b); 32 | } 33 | 34 | /** 35 | * op_div - divide 36 | * @a: integer 37 | * @b: integer 38 | * Return: result 39 | */ 40 | int op_div(int a, int b) 41 | { 42 | return (a / b); 43 | } 44 | 45 | /** 46 | * op_mod - modulus for remainder 47 | * @a: integer 48 | * @b: integer 49 | * Return: result 50 | */ 51 | int op_mod(int a, int b) 52 | { 53 | return (a % b); 54 | } 55 | -------------------------------------------------------------------------------- /0x0E-function_pointers/function_pointers.h: -------------------------------------------------------------------------------- 1 | #ifndef FUNCTION_POINTERS 2 | #define FUNCTION_POINTERS 3 | 4 | void print_name(char *name, void (*f)(char *)); 5 | void array_iterator(int *array, size_t size, void (*action)(int)); 6 | int int_index(int *array, int size, int (*cmp)(int)); 7 | 8 | #endif 9 | -------------------------------------------------------------------------------- /0x0F-variadic_functions/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "variadic_functions.h" 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int sum; 12 | 13 | sum = sum_them_all(2, 98, -1024); 14 | printf("%d\n", sum); 15 | sum = sum_them_all(4, 98, 1024, 402, -1024); 16 | printf("%d\n", sum); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x0F-variadic_functions/0-sum_them_all.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "variadic_functions.h" 3 | 4 | /** 5 | * sum_them_all - return sum when given unknown amount of arguments 6 | * @n: integers to add 7 | * Return: sum 8 | */ 9 | 10 | int sum_them_all(const unsigned int n, ...) 11 | { 12 | va_list valist; 13 | unsigned int sum = 0, i; 14 | 15 | /* validate valist and initialize */ 16 | if (n == 0) 17 | return (0); 18 | va_start(valist, n); 19 | 20 | /* iterate through list, update sum, free list */ 21 | for (i = 0; i < n; i++) 22 | sum += va_arg(valist, int); 23 | va_end(valist); 24 | 25 | return (sum); 26 | } 27 | -------------------------------------------------------------------------------- /0x0F-variadic_functions/1-main.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | 3 | /** 4 | * main - check the code for Holberton School students. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | print_numbers(", ", 4, 0, 98, -1024, 402); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x0F-variadic_functions/1-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include /* va_* */ 2 | #include /* printf */ 3 | #include "variadic_functions.h" 4 | 5 | /** 6 | * print_numbers - print varying amount of numbers 7 | * @separator: delimiter 8 | * @n: amount of arguments in list 9 | */ 10 | 11 | void print_numbers(const char *separator, const unsigned int n, ...) 12 | { 13 | va_list valist; 14 | unsigned int i; 15 | 16 | if (n > 0) 17 | { 18 | va_start(valist, n); 19 | for (i = 1; i <= n; i++) 20 | { 21 | printf("%d", va_arg(valist, int)); 22 | if (i != n && separator != NULL) 23 | printf("%s", separator); 24 | } 25 | va_end(valist); 26 | } 27 | printf("\n"); 28 | } 29 | -------------------------------------------------------------------------------- /0x0F-variadic_functions/2-main.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | 3 | /** 4 | * main - check the code for Holberton School students. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | print_strings(", ", 3, "Jay", "", "Django"); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x0F-variadic_functions/2-print_strings.c: -------------------------------------------------------------------------------- 1 | #include /* va_* */ 2 | #include /* printf */ 3 | #include "variadic_functions.h" 4 | 5 | /** 6 | * print_strings - print varying amount of strings 7 | * @separator: delimiter 8 | * @n: amount of arguments in list 9 | */ 10 | 11 | void print_strings(const char *separator, const unsigned int n, ...) 12 | { 13 | va_list valist; 14 | unsigned int i; 15 | char *s; 16 | 17 | if (n > 0) 18 | { 19 | va_start(valist, n); 20 | for (i = 1; i <= n; i++) 21 | { 22 | s = va_arg(valist, char *); 23 | if (s == NULL) 24 | printf("(nil)"); 25 | else 26 | printf("%s", s); 27 | 28 | if (i != n && separator != NULL) 29 | printf("%s", separator); 30 | } 31 | va_end(valist); 32 | } 33 | printf("\n"); 34 | } 35 | -------------------------------------------------------------------------------- /0x0F-variadic_functions/3-main.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | 3 | /** 4 | * main - check the code for Holberton School students. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | print_all("ceiss", 'H', 0, "", "lberton"); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x0F-variadic_functions/variadic_functions.h: -------------------------------------------------------------------------------- 1 | #ifndef VARIADIC_FUNCTIONS 2 | #define VARIADIC_FUNCTIONS 3 | 4 | #include 5 | 6 | int _putchar(char c); 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 | /** 13 | * struct dt - struct 14 | * @letter: letter signifying data type 15 | * @func: function ptr 16 | */ 17 | typedef struct dt 18 | { 19 | char letter; 20 | void (*func)(va_list); 21 | } datatype; 22 | 23 | #endif 24 | -------------------------------------------------------------------------------- /0x11-singly_linked_lists/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code for Holberton School students. 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | list_t *head; 14 | list_t *new; 15 | list_t hello = {"World", 5, NULL}; 16 | size_t n; 17 | 18 | head = &hello; 19 | new = malloc(sizeof(list_t)); 20 | if (new == NULL) 21 | { 22 | printf("Error\n"); 23 | return (1); 24 | } 25 | new->str = strdup("Hello"); 26 | new->len = 5; 27 | new->next = head; 28 | head = new; 29 | n = print_list(head); 30 | printf("-> %lu elements\n", n); 31 | 32 | printf("\n"); 33 | free(new->str); 34 | new->str = NULL; 35 | n = print_list(head); 36 | printf("-> %lu elements\n", n); 37 | 38 | free(new); 39 | return (0); 40 | } 41 | -------------------------------------------------------------------------------- /0x11-singly_linked_lists/0-print_list.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * print_list - prints all elements of list_t list 5 | * @h: linked list 6 | * Return: number of nodes in linked list 7 | */ 8 | 9 | size_t print_list(const list_t *h) 10 | { 11 | size_t count = 0; 12 | 13 | while (h != NULL) 14 | { 15 | if (h->str == NULL) 16 | printf("[0] (nil)\n"); 17 | else 18 | printf("[%d] %s\n", h->len, h->str); 19 | 20 | count++; 21 | h = h->next; 22 | } 23 | 24 | return (count); 25 | } 26 | -------------------------------------------------------------------------------- /0x11-singly_linked_lists/1-list_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * list_len - prints length of list_t linked list 5 | * @h: linked list 6 | * Return: number of nodes/elements (length) in linked list 7 | */ 8 | 9 | size_t list_len(const list_t *h) 10 | { 11 | size_t count = 0; 12 | 13 | while (h != NULL) 14 | { 15 | count++; 16 | h = h->next; 17 | } 18 | 19 | return (count); 20 | } 21 | -------------------------------------------------------------------------------- /0x11-singly_linked_lists/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code for Holberton School students. 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | list_t *head; 14 | list_t *new; 15 | list_t hello = {"World", 5, NULL}; 16 | size_t n; 17 | 18 | head = &hello; 19 | new = malloc(sizeof(list_t)); 20 | if (new == NULL) 21 | { 22 | printf("Error\n"); 23 | return (1); 24 | } 25 | new->str = strdup("Hello"); 26 | new->len = 5; 27 | new->next = head; 28 | head = new; 29 | n = list_len(head); 30 | printf("-> %lu elements\n", n); 31 | free(new->str); 32 | free(new); 33 | return (0); 34 | } 35 | -------------------------------------------------------------------------------- /0x11-singly_linked_lists/100-first.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * execute_before_main - executes this function before main function 5 | * Note: made possible by prior declaration of "__attribute__ ((constructor))" 6 | */ 7 | 8 | void __attribute__ ((constructor)) execute_before_main() 9 | { 10 | printf("You're beat! and yet, you must allow,\n"); 11 | printf("I bore my house upon my back!\n"); 12 | } 13 | -------------------------------------------------------------------------------- /0x11-singly_linked_lists/100-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - check the code for Holberton School students. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | printf("(A tortoise, having pretty good sense of a hare's nature, challenges one to a race.)\n"); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x11-singly_linked_lists/4-free_list.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_list - free linked list 5 | * @head: linked list 6 | */ 7 | 8 | void free_list(list_t *head) 9 | { 10 | while (head != NULL) 11 | { 12 | free(head->str); 13 | free(head); 14 | head = head->next; 15 | } 16 | } 17 | 18 | /** 19 | * another way using temp pointer 20 | * 21 | * list_t *ptr; 22 | * 23 | * if (head == NULL) // account for no linked list 24 | * return; 25 | * 26 | * while (head != NULL) // have ptr keep track of head node and free 27 | * { 28 | * ptr = head; 29 | * head = head->next; // move to next node while ptr frees prior 30 | * free(ptr->str); // free malloced strings 31 | * free(ptr); 32 | * } 33 | */ 34 | -------------------------------------------------------------------------------- /0x11-singly_linked_lists/4-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code for Holberton School students. 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | list_t *head; 14 | 15 | head = NULL; 16 | add_node_end(&head, "Jennie"); 17 | add_node_end(&head, "&"); 18 | add_node_end(&head, "Jay"); 19 | add_node_end(&head, "love"); 20 | add_node_end(&head, "asm"); 21 | print_list(head); 22 | free_list(head); 23 | head = NULL; 24 | return (0); 25 | } 26 | -------------------------------------------------------------------------------- /0x11-singly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef LISTS_H 2 | #define LISTS_H 3 | 4 | #include /* size_t, printf */ 5 | #include /* malloc */ 6 | #include /* strdup */ 7 | 8 | /** 9 | * struct list_s - singly linked list 10 | * @str: string - (malloc'ed string) 11 | * @len: length of the string 12 | * @next: points to the next node 13 | * 14 | * Description: singly linked list node structure 15 | * for Holberton project 16 | */ 17 | typedef struct list_s 18 | { 19 | char *str; 20 | unsigned int len; 21 | struct list_s *next; 22 | } list_t; 23 | 24 | /* function prototypes for all files */ 25 | size_t print_list(const list_t *h); 26 | size_t list_len(const list_t *h); 27 | list_t *add_node(list_t **head, const char *str); 28 | list_t *add_node_end(list_t **head, const char *str); 29 | void free_list(list_t *head); 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /0x12-more_singly_linked_lists/0-print_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * print_listint - prints all elements of a list 5 | * @h: list 6 | * Return: number of nodes 7 | */ 8 | 9 | size_t print_listint(const listint_t *h) 10 | { 11 | size_t num_nodes = 0; 12 | 13 | while (h != NULL) 14 | { 15 | printf("%d\n", h->n); /* autofills 0 if no int value given */ 16 | 17 | num_nodes += 1; 18 | h = h->next; 19 | } 20 | 21 | return (num_nodes); 22 | } 23 | -------------------------------------------------------------------------------- /0x12-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: list 6 | * Return: number of nodes in list 7 | */ 8 | 9 | size_t listint_len(const listint_t *h) 10 | { 11 | size_t num_nodes = 0; 12 | 13 | while (h != NULL) 14 | { 15 | num_nodes += 1; 16 | h = h->next; 17 | } 18 | 19 | return (num_nodes); 20 | } 21 | -------------------------------------------------------------------------------- /0x12-more_singly_linked_lists/100-reverse_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * reverse_listint - reverse linked list 5 | * @head: pointer to head pointer of linked list 6 | * Return: pointer to first node of reversed list 7 | */ 8 | 9 | listint_t *reverse_listint(listint_t **head) 10 | { 11 | listint_t *prev, *next; 12 | 13 | /* account for no ptr and empty list */ 14 | if (head == NULL || *head == NULL) 15 | return (NULL); 16 | 17 | /* iterate thorugh list to reverse linking */ 18 | prev = NULL; 19 | while (*head != NULL) 20 | { 21 | next = (*head)->next; /* keep track of next node to move to */ 22 | (*head)->next = prev; /* link current node's ptr to prev node */ 23 | prev = *head; /* update previous node to be current node */ 24 | *head = next; /* move current node to next node */ 25 | } 26 | (*head) = prev; /* unlink last element from null and point backwards */ 27 | 28 | return (*head); 29 | } 30 | -------------------------------------------------------------------------------- /0x12-more_singly_linked_lists/103-find_loop.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * find_listint_loop - find loop in linked list 5 | * @head: pointer to head pointer of linked list 6 | * Return: address of node where loop starts 7 | */ 8 | 9 | listint_t *find_listint_loop(listint_t *head) 10 | { 11 | listint_t *turtle, *hare; 12 | 13 | /* determine if loop exists by seeing if turtle and hare meets */ 14 | /* start them at head, move turtle one node and hare two nodes */ 15 | turtle = hare = head; 16 | 17 | while (turtle != NULL && hare != NULL) 18 | { 19 | turtle = turtle->next; 20 | hare = hare->next->next; 21 | 22 | if (turtle == hare) 23 | { 24 | /* start turtle at head and hare at address they met */ 25 | /* move turtle and hare one node to find loop origin */ 26 | turtle = head; 27 | while (turtle != hare) 28 | { 29 | turtle = turtle->next; 30 | hare = hare->next; 31 | } 32 | return (turtle); 33 | } 34 | } 35 | 36 | return (NULL); 37 | } 38 | -------------------------------------------------------------------------------- /0x12-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 head pointer of linked list 6 | * @n: data to add to new node 7 | * Return: address of new element, or NULL if failed 8 | */ 9 | 10 | listint_t *add_nodeint(listint_t **head, const int n) 11 | { 12 | 13 | listint_t *new_node; 14 | 15 | new_node = malloc(sizeof(listint_t)); /* create new node */ 16 | if (new_node == NULL) 17 | return (NULL); 18 | 19 | new_node->n = n; /* set values */ 20 | 21 | new_node->next = *head; /* first link new node to point to first node */ 22 | *head = new_node; /* then switch list pointer to point to new node */ 23 | 24 | return (new_node); 25 | } 26 | -------------------------------------------------------------------------------- /0x12-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 head pointer of linked list 6 | * @n: data to add to new node 7 | * Return: address of new element, or NULL if failed 8 | */ 9 | 10 | listint_t *add_nodeint_end(listint_t **head, const int n) 11 | { 12 | 13 | listint_t *new_node, *tmp; 14 | 15 | new_node = malloc(sizeof(listint_t)); /* make new node and set values */ 16 | if (new_node == NULL) 17 | return (NULL); 18 | 19 | new_node->n = n; 20 | new_node->next = NULL; 21 | 22 | if (*head == NULL) /* account for empty list by pointing head to node */ 23 | *head = new_node; 24 | else 25 | { 26 | tmp = *head; /* set tmp ptr to iterate w/o moving head ptr */ 27 | 28 | while (tmp->next != NULL) /* iterate till last node */ 29 | tmp = tmp->next; 30 | tmp->next = new_node; /* link last node to new node */ 31 | } 32 | 33 | return (new_node); 34 | } 35 | -------------------------------------------------------------------------------- /0x12-more_singly_linked_lists/4-free_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_listint - frees a linked list 5 | * @head: linked list 6 | */ 7 | 8 | void free_listint(listint_t *head) 9 | { 10 | listint_t *ptr; 11 | 12 | if (head == NULL) /* account for no linked list */ 13 | return; 14 | 15 | while (head != NULL) /* have ptr keep track of head node and free */ 16 | { 17 | ptr = head; 18 | head = head->next; /* move to next node while ptr frees prior */ 19 | free(ptr); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /0x12-more_singly_linked_lists/5-free_listint2.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_listint2 - frees a linked list, and sets head to NULL 5 | * @head: linked list 6 | */ 7 | 8 | void free_listint2(listint_t **head) 9 | { 10 | listint_t *ptr; 11 | 12 | if (head == NULL) /* account for no ptr */ 13 | return; 14 | 15 | while (*head != NULL) /* have ptr keep track of head node and free */ 16 | { 17 | ptr = *head; 18 | *head = ptr->next; /* move to next node while ptr frees prior */ 19 | free(ptr); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /0x12-more_singly_linked_lists/6-pop_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * pop_listint - deletes the head node, and returns the head node’s data 5 | * @head: linked list 6 | * Return: deleted head node's data 7 | */ 8 | 9 | int pop_listint(listint_t **head) 10 | { 11 | int data; 12 | listint_t *tmp; 13 | 14 | if (*head == NULL) /* account for no linked list */ 15 | return (0); 16 | 17 | tmp = *head; 18 | 19 | data = tmp->n; /* save data to return later */ 20 | 21 | *head = tmp->next; /* link head to next node */ 22 | free(tmp); 23 | 24 | return (data); 25 | 26 | } 27 | -------------------------------------------------------------------------------- /0x12-more_singly_linked_lists/7-get_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * get_nodeint_at_index - returns nth node of listint_t linked list 5 | * @head: pointer to head pointer of linked list 6 | * @index: nth node 7 | * Return: nth node, or NULL if failed 8 | */ 9 | 10 | listint_t *get_nodeint_at_index(listint_t *head, unsigned int index) 11 | { 12 | 13 | unsigned int i = 0; 14 | 15 | if (head == NULL) 16 | return (NULL); 17 | 18 | while ((i < index) && head != NULL) /* iterate until list ends */ 19 | { 20 | head = head->next; 21 | i++; 22 | } 23 | 24 | if (i == index) /* check that index was within list size */ 25 | return (head); 26 | 27 | return (NULL); 28 | } 29 | -------------------------------------------------------------------------------- /0x12-more_singly_linked_lists/8-sum_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * sum_listint - return sum of all data in linked list 5 | * @head: pointer to head pointer of linked list 6 | * Return: sum, or 0 if list is empty 7 | */ 8 | 9 | int sum_listint(listint_t *head) 10 | { 11 | 12 | int sum = 0; 13 | 14 | while (head != NULL) 15 | { 16 | sum += head->n; 17 | head = head->next; 18 | } 19 | 20 | return (sum); 21 | } 22 | -------------------------------------------------------------------------------- /0x12-more_singly_linked_lists/main_test_files/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code for Holberton School students. 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | listint_t *head; 14 | listint_t *new; 15 | listint_t hello = {8, NULL}; 16 | size_t n; 17 | 18 | head = &hello; 19 | new = malloc(sizeof(listint_t)); 20 | if (new == NULL) 21 | { 22 | printf("Error\n"); 23 | return (1); 24 | } 25 | new->n = 9; 26 | new->next = head; 27 | head = new; 28 | n = print_listint(head); 29 | printf("-> %lu elements\n", n); 30 | free(new); 31 | return (0); 32 | } 33 | -------------------------------------------------------------------------------- /0x12-more_singly_linked_lists/main_test_files/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code for Holberton School students. 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | listint_t *head; 14 | listint_t *new; 15 | listint_t hello = {8, NULL}; 16 | size_t n; 17 | 18 | head = &hello; 19 | new = malloc(sizeof(listint_t)); 20 | if (new == NULL) 21 | { 22 | printf("Error\n"); 23 | return (1); 24 | } 25 | new->n = 9; 26 | new->next = head; 27 | head = new; 28 | n = listint_len(head); 29 | printf("-> %lu elements\n", n); 30 | free(new); 31 | return (0); 32 | } 33 | -------------------------------------------------------------------------------- /0x12-more_singly_linked_lists/main_test_files/100-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code for Holberton School students. 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | listint_t *head; 14 | 15 | head = NULL; 16 | add_nodeint_end(&head, 0); 17 | add_nodeint_end(&head, 1); 18 | add_nodeint_end(&head, 2); 19 | add_nodeint_end(&head, 3); 20 | add_nodeint_end(&head, 4); 21 | add_nodeint_end(&head, 98); 22 | add_nodeint_end(&head, 402); 23 | add_nodeint_end(&head, 1024); 24 | print_listint(head); 25 | reverse_listint(&head); 26 | print_listint(head); 27 | free_listint2(&head); 28 | return (0); 29 | } 30 | -------------------------------------------------------------------------------- /0x12-more_singly_linked_lists/main_test_files/101-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code for Holberton School students. 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | // listint_t *head; 14 | listint_t *head2; 15 | // listint_t *node; 16 | 17 | head2 = NULL; 18 | add_nodeint(&head2, 0); 19 | add_nodeint(&head2, 1); 20 | add_nodeint(&head2, 2); 21 | add_nodeint(&head2, 3); 22 | add_nodeint(&head2, 4); 23 | add_nodeint(&head2, 98); 24 | add_nodeint(&head2, 402); 25 | add_nodeint(&head2, 1024); 26 | print_listint_safe(head2); 27 | // head = NULL; 28 | // node = add_nodeint(&head, 0); 29 | // add_nodeint(&head, 1); 30 | // add_nodeint(&head, 2); 31 | // add_nodeint(&head, 3); 32 | // add_nodeint(&head, 4); 33 | // node->next = add_nodeint(&head, 98); 34 | // add_nodeint(&head, 402); 35 | // add_nodeint(&head, 1024); 36 | // print_listint_safe(head); 37 | return (0); 38 | } 39 | -------------------------------------------------------------------------------- /0x12-more_singly_linked_lists/main_test_files/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code for Holberton School students. 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | listint_t *head; 14 | 15 | head = NULL; 16 | add_nodeint(&head, 0); 17 | add_nodeint(&head, 1); 18 | add_nodeint(&head, 2); 19 | add_nodeint(&head, 3); 20 | add_nodeint(&head, 4); 21 | add_nodeint(&head, 98); 22 | add_nodeint(&head, 402); 23 | add_nodeint(&head, 1024); 24 | print_listint(head); 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /0x12-more_singly_linked_lists/main_test_files/3-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code for Holberton School students. 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | listint_t *head; 14 | 15 | head = NULL; 16 | add_nodeint_end(&head, 0); 17 | add_nodeint_end(&head, 1); 18 | add_nodeint_end(&head, 2); 19 | add_nodeint_end(&head, 3); 20 | add_nodeint_end(&head, 4); 21 | add_nodeint_end(&head, 98); 22 | add_nodeint_end(&head, 402); 23 | add_nodeint_end(&head, 1024); 24 | print_listint(head); 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /0x12-more_singly_linked_lists/main_test_files/4-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code for Holberton School students. 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | listint_t *head; 14 | 15 | head = NULL; 16 | add_nodeint_end(&head, 0); 17 | add_nodeint_end(&head, 1); 18 | add_nodeint_end(&head, 2); 19 | add_nodeint_end(&head, 3); 20 | add_nodeint_end(&head, 4); 21 | add_nodeint_end(&head, 98); 22 | add_nodeint_end(&head, 402); 23 | add_nodeint_end(&head, 1024); 24 | print_listint(head); 25 | free_listint(head); 26 | head = NULL; 27 | return (0); 28 | } 29 | -------------------------------------------------------------------------------- /0x12-more_singly_linked_lists/main_test_files/5-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code for Holberton School students. 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | listint_t *head; 14 | 15 | head = NULL; 16 | add_nodeint_end(&head, 0); 17 | add_nodeint_end(&head, 1); 18 | add_nodeint_end(&head, 2); 19 | add_nodeint_end(&head, 3); 20 | add_nodeint_end(&head, 4); 21 | add_nodeint_end(&head, 98); 22 | add_nodeint_end(&head, 402); 23 | add_nodeint_end(&head, 1024); 24 | print_listint(head); 25 | free_listint2(&head); 26 | printf("%p\n", (void *)head); 27 | return (0); 28 | } 29 | -------------------------------------------------------------------------------- /0x12-more_singly_linked_lists/main_test_files/6-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code for Holberton School students. 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | listint_t *head; 14 | int n; 15 | 16 | head = NULL; 17 | add_nodeint_end(&head, 0); 18 | add_nodeint_end(&head, 1); 19 | add_nodeint_end(&head, 2); 20 | add_nodeint_end(&head, 3); 21 | add_nodeint_end(&head, 4); 22 | add_nodeint_end(&head, 98); 23 | add_nodeint_end(&head, 402); 24 | add_nodeint_end(&head, 1024); 25 | print_listint(head); 26 | n = pop_listint(&head); 27 | printf("- %d\n", n); 28 | print_listint(head); 29 | n = pop_listint(&head); 30 | printf("- %d\n", n); 31 | print_listint(head); 32 | free_listint2(&head); 33 | printf("%p\n", (void *)head); 34 | return (0); 35 | } 36 | -------------------------------------------------------------------------------- /0x12-more_singly_linked_lists/main_test_files/7-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code for Holberton School students. 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | listint_t *head; 14 | listint_t *node; 15 | 16 | head = NULL; 17 | add_nodeint_end(&head, 0); 18 | add_nodeint_end(&head, 1); 19 | add_nodeint_end(&head, 2); 20 | add_nodeint_end(&head, 3); 21 | add_nodeint_end(&head, 4); 22 | add_nodeint_end(&head, 98); 23 | add_nodeint_end(&head, 402); 24 | add_nodeint_end(&head, 1024); 25 | print_listint(head); 26 | node = get_nodeint_at_index(head, 5); 27 | printf("%d\n", node->n); 28 | print_listint(head); 29 | free_listint2(&head); 30 | return (0); 31 | } 32 | -------------------------------------------------------------------------------- /0x12-more_singly_linked_lists/main_test_files/8-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code for Holberton School students. 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | listint_t *head; 14 | int sum; 15 | 16 | head = NULL; 17 | add_nodeint_end(&head, 0); 18 | add_nodeint_end(&head, 1); 19 | add_nodeint_end(&head, 2); 20 | add_nodeint_end(&head, 3); 21 | add_nodeint_end(&head, 4); 22 | add_nodeint_end(&head, 98); 23 | add_nodeint_end(&head, 402); 24 | add_nodeint_end(&head, 1024); 25 | sum = sum_listint(head); 26 | printf("sum = %d\n", sum); 27 | free_listint2(&head); 28 | return (0); 29 | } 30 | -------------------------------------------------------------------------------- /0x12-more_singly_linked_lists/main_test_files/9-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code for Holberton School students. 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | listint_t *head; 14 | 15 | head = NULL; 16 | add_nodeint_end(&head, 0); 17 | add_nodeint_end(&head, 1); 18 | add_nodeint_end(&head, 2); 19 | add_nodeint_end(&head, 3); 20 | add_nodeint_end(&head, 4); 21 | add_nodeint_end(&head, 98); 22 | add_nodeint_end(&head, 402); 23 | add_nodeint_end(&head, 1024); 24 | print_listint(head); 25 | printf("-----------------\n"); 26 | insert_nodeint_at_index(&head, 8, 4096); 27 | print_listint(head); 28 | free_listint2(&head); 29 | return (0); 30 | } 31 | -------------------------------------------------------------------------------- /0x13-bit_manipulation/100-get_endianness.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * get_endianness - checks if multibyte data is stored as little or big endian 5 | * Return: first value stored, 0 if big endian, 1 if little 6 | */ 7 | 8 | int get_endianness(void) 9 | { 10 | /* have a 4 byte int hold some hex number 0x00000001 */ 11 | /* test if first byte stored is 00 or 01 */ 12 | /* big endian would store 00, little would store 01 */ 13 | 14 | int n = 0x00000001; 15 | char *c = (char *)&n; 16 | 17 | return (c[0]); 18 | } 19 | -------------------------------------------------------------------------------- /0x13-bit_manipulation/2-get_bit.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * get_bit - get bit at index 5 | * @n: number 6 | * @index: index within binary number 7 | * Return: bit 0 or 1, or -1 if error 8 | */ 9 | int get_bit(unsigned long int n, unsigned int index) 10 | { 11 | int bit; 12 | unsigned int max_bits; 13 | 14 | /* validate index is not out of range */ 15 | max_bits = (sizeof(unsigned long int) * 8); 16 | if (index > max_bits) 17 | return (-1); 18 | 19 | /* shift number index places right to find bit */ 20 | bit = ((n >> index) & 1); 21 | 22 | return (bit); 23 | } 24 | -------------------------------------------------------------------------------- /0x13-bit_manipulation/3-set_bit.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * set_bit - set bit to 1 at given index 5 | * @n: number 6 | * @index: index within binary number 7 | * Return: 1 if success, or -1 if error 8 | */ 9 | int set_bit(unsigned long int *n, unsigned int index) 10 | { 11 | unsigned int max_bits; 12 | unsigned long int mask = 1; 13 | 14 | /* validate index is not out of range */ 15 | max_bits = (sizeof(unsigned long int) * 8); 16 | if (index > max_bits) 17 | return (-1); 18 | 19 | /* create mask with 1 at index (...00100...) to work on that index */ 20 | mask <<= index; 21 | *n = (*n | mask); 22 | 23 | return (1); 24 | } 25 | -------------------------------------------------------------------------------- /0x13-bit_manipulation/4-clear_bit.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * clear_bit - clear bit to 0 at given index 5 | * @n: number 6 | * @index: index within binary number 7 | * Return: 1 if success, or -1 if error 8 | */ 9 | int clear_bit(unsigned long int *n, unsigned int index) 10 | { 11 | unsigned long int max_bits; 12 | unsigned long int mask = 1; 13 | 14 | /* validate index is not out of range */ 15 | max_bits = (sizeof(unsigned long int) * 8); 16 | if (index > max_bits) 17 | return (-1); 18 | 19 | /* create mask with 0 at index (...11011...) to work on that index */ 20 | mask = ~(mask << index); 21 | *n = (*n & mask); 22 | 23 | return (1); 24 | } 25 | -------------------------------------------------------------------------------- /0x13-bit_manipulation/5-flip_bits.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * flip_bits - determine how many bits to flip to get from one num to another 5 | * @n: number 6 | * @m: number2 7 | * Return: how many bits differ 8 | */ 9 | unsigned int flip_bits(unsigned long int n, unsigned long int m) 10 | { 11 | unsigned int diff_bits = 0; 12 | unsigned long int difference; 13 | 14 | /* Xor both nums to show bit 1 if different bits */ 15 | difference = n ^ m; 16 | 17 | /* keep shifting difference to right and tallying the ones up */ 18 | do { 19 | diff_bits += (difference & 1); 20 | difference >>= 1; 21 | } while 22 | (difference > 0); 23 | 24 | return (diff_bits); 25 | } 26 | -------------------------------------------------------------------------------- /0x13-bit_manipulation/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _putchar - writes the character c to stdout 5 | * @c: The character to print 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | int _putchar(char c) 11 | { 12 | return (write(1, &c, 1)); 13 | } 14 | -------------------------------------------------------------------------------- /0x13-bit_manipulation/holberton.h: -------------------------------------------------------------------------------- 1 | #ifndef HEADER_H 2 | #define HEADER_H 3 | 4 | #include /* NULL */ 5 | 6 | /* helper function */ 7 | int _putchar(char c); 8 | 9 | /* function prototypes for all files */ 10 | unsigned int binary_to_uint(const char *b); 11 | void print_binary(unsigned long int n); 12 | int get_bit(unsigned long int n, unsigned int index); 13 | int set_bit(unsigned long int *n, unsigned int index); 14 | int clear_bit(unsigned long int *n, unsigned int index); 15 | unsigned int flip_bits(unsigned long int n, unsigned long int m); 16 | int get_endianness(void); 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /0x13-bit_manipulation/main_test_files/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "holberton.h" 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | unsigned int n; 12 | 13 | n = binary_to_uint("1"); 14 | printf("%u\n", n); 15 | n = binary_to_uint("101"); 16 | printf("%u\n", n); 17 | n = binary_to_uint("1e01"); 18 | printf("%u\n", n); 19 | n = binary_to_uint("1100010"); 20 | printf("%u\n", n); 21 | n = binary_to_uint("0000000000000000000110010010"); 22 | printf("%u\n", n); 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x13-bit_manipulation/main_test_files/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "holberton.h" 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | print_binary(0); 12 | printf("\n"); 13 | print_binary(1); 14 | printf("\n"); 15 | print_binary(98); 16 | printf("\n"); 17 | print_binary(1024); 18 | printf("\n"); 19 | print_binary((1 << 10) + 1); 20 | printf("\n"); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x13-bit_manipulation/main_test_files/100-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "holberton.h" 3 | 4 | int main(void) 5 | { 6 | int n; 7 | 8 | n = get_endianness(); 9 | if (n != 0) 10 | { 11 | printf("Little Endian\n"); 12 | } 13 | else 14 | { 15 | printf("Big Endian\n"); 16 | } 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x13-bit_manipulation/main_test_files/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "holberton.h" 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int n; 12 | 13 | n = get_bit(1024, 10); 14 | printf("%d\n", n); 15 | n = get_bit(98, 1); 16 | printf("%d\n", n); 17 | n = get_bit(1024, 0); 18 | printf("%d\n", n); 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /0x13-bit_manipulation/main_test_files/3-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "holberton.h" 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | unsigned long int n; 12 | 13 | n = 1024; 14 | set_bit(&n, 5); 15 | printf("%lu\n", n); 16 | n = 0; 17 | set_bit(&n, 10); 18 | printf("%lu\n", n); 19 | n = 98; 20 | set_bit(&n, 0); 21 | printf("%lu\n", n); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x13-bit_manipulation/main_test_files/4-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "holberton.h" 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | unsigned long int n; 12 | 13 | n = 1024; 14 | clear_bit(&n, 10); 15 | printf("%lu\n", n); 16 | n = 0; 17 | clear_bit(&n, 10); 18 | printf("%lu\n", n); 19 | n = 98; 20 | clear_bit(&n, 1); 21 | printf("%lu\n", n); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x13-bit_manipulation/main_test_files/5-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "holberton.h" 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | unsigned int n; 12 | 13 | n = flip_bits(1024, 1); 14 | printf("%u\n", n); 15 | n = flip_bits(402, 98); 16 | printf("%u\n", n); 17 | n = flip_bits(1024, 3); 18 | printf("%u\n", n); 19 | n = flip_bits(1024, 1025); 20 | printf("%u\n", n); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x14-file_io/holberton.h: -------------------------------------------------------------------------------- 1 | #ifndef HEADER_H 2 | #define HEADER_H 3 | 4 | /* open */ 5 | #include 6 | #include 7 | #include 8 | 9 | /* close */ 10 | #include 11 | 12 | /* dprintf */ 13 | #include 14 | 15 | /* malloc, free */ 16 | #include 17 | 18 | ssize_t read_textfile(const char *filename, size_t letters); 19 | int create_file(const char *filename, char *text_content); 20 | int append_text_to_file(const char *filename, char *text_content); 21 | 22 | #endif 23 | -------------------------------------------------------------------------------- /0x14-file_io/main_test_files/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "holberton.h" 4 | 5 | /** 6 | * main - check the code for Holberton School students. 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(int ac, char **av) 11 | { 12 | ssize_t n; 13 | 14 | if (ac != 2) 15 | { 16 | dprintf(2, "Usage: %s filename\n", av[0]); 17 | exit(1); 18 | } 19 | n = read_textfile(av[1], 114); 20 | printf("\n(printed chars: %li)\n", n); 21 | n = read_textfile(av[1], 1024); 22 | printf("\n(printed chars: %li)\n", n); 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x14-file_io/main_test_files/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "holberton.h" 4 | 5 | /** 6 | * main - check the code for Holberton School students. 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(int ac, char **av) 11 | { 12 | int res; 13 | 14 | if (ac != 3) 15 | { 16 | dprintf(2, "Usage: %s filename text\n", av[0]); 17 | exit(1); 18 | } 19 | res = create_file(av[1], av[2]); 20 | printf("-> %i)\n", res); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x14-file_io/main_test_files/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "holberton.h" 4 | 5 | /** 6 | * main - check the code for Holberton School students. 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(int ac, char **av) 11 | { 12 | int res; 13 | 14 | if (ac != 3) 15 | { 16 | dprintf(2, "Usage: %s filename text\n", av[0]); 17 | exit(1); 18 | } 19 | res = append_text_to_file(av[1], av[2]); 20 | printf("-> %i)\n", res); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x14-file_io/main_test_files/Requiescat: -------------------------------------------------------------------------------- 1 | Requiescat 2 | by Oscar Wilde 3 | 4 | Tread lightly, she is near 5 | Under the snow, 6 | Speak gently, she can hear 7 | The daisies grow. 8 | 9 | All her bright golden hair 10 | Tarnished with rust, 11 | She that was young and fair 12 | Fallen to dust. 13 | 14 | Lily-like, white as snow, 15 | She hardly knew 16 | She was a woman, so 17 | Sweetly she grew. 18 | 19 | Coffin-board, heavy stone, 20 | Lie on her breast, 21 | I vex my heart alone, 22 | She is at rest. 23 | 24 | Peace, Peace, she cannot hear 25 | Lyre or sonnet, 26 | All my life's buried here, 27 | Heap earth upon it. -------------------------------------------------------------------------------- /0x15-simple_shell_practice/0-printargvs: -------------------------------------------------------------------------------- 1 | #include "shell.h" 2 | 3 | /** 4 | * print_argvs - print all argvs given to program without using argc 5 | * Return: 0 on success 6 | */ 7 | void print_argvs(int ac, char **av) 8 | { 9 | int i; 10 | (void)ac; 11 | 12 | i = 0; 13 | while (av[i] != NULL) 14 | printf("%s\n", av[i++]); 15 | 16 | } 17 | -------------------------------------------------------------------------------- /0x15-simple_shell_practice/1-getpids: -------------------------------------------------------------------------------- 1 | #include "shell.h" 2 | 3 | /** 4 | * getpids - print parent and process id 5 | */ 6 | void getpids(void) 7 | { 8 | pid_t pid, fpid; 9 | 10 | pid = getpid(); /* parent process */ 11 | fpid = fork(); /* child process */ 12 | 13 | if (fpid == 0) 14 | { 15 | printf("I'm child! Parent pid: %u, child id: %u\n", pid, fpid); 16 | } 17 | else 18 | { 19 | printf("I'm father! Parent pid: %u, child id: %u\n", pid, fpid); 20 | } 21 | 22 | } 23 | 24 | /* See max process id: cat /proc/sys/kernel/pid_max */ 25 | -------------------------------------------------------------------------------- /0x15-simple_shell_practice/2-getline.c: -------------------------------------------------------------------------------- 1 | #include "shell.h" 2 | 3 | /** 4 | * _getline - prompt; after user types, get their input 5 | * Return: characters read 6 | */ 7 | int _getline(char **buffer) 8 | { 9 | size_t size = 0; 10 | 11 | printf("$ "); 12 | 13 | if (getline(buffer, &size, stdin) == -1) 14 | { 15 | return (-1); 16 | } 17 | else 18 | return (size); 19 | /* free buffer that getline mallocs in main program */ 20 | 21 | } 22 | 23 | /* ssize_t getline(char **lineptr, size_t *n, FILE *stream); */ 24 | 25 | /* alternative way is to use write, and not have size because getline */ 26 | /* dynamically mallocs. Make sure to free */ 27 | -------------------------------------------------------------------------------- /0x15-simple_shell_practice/5-prompt_till_CtrlD: -------------------------------------------------------------------------------- 1 | #include "shell.h" 2 | 3 | /** 4 | * _execve - executes the command 5 | */ 6 | void _execve(char *cmd[]) 7 | { 8 | if ((execve(cmd[0], cmd, NULL)) == -1) 9 | { 10 | perror("Execution Error"); 11 | } 12 | } 13 | 14 | /** 15 | * main - executes a command in different children. Each child 16 | * should be created by the same process (the father). Wait for 17 | * a child to exit before creating a new child. 18 | * Return: 0 on sucess 19 | */ 20 | int main(void) 21 | { 22 | pid_t child_pid; 23 | char *cmd[] = {"/bin/ls", "-l", "/usr", NULL}; 24 | char *buffer; 25 | long unsigned int size; 26 | 27 | while (getline(&buffer, &size, stdin) > 0) 28 | { 29 | child_pid = fork(); /* creates children */ 30 | if (child_pid == -1) 31 | { 32 | perror("Error:"); 33 | return (1); 34 | } 35 | if (child_pid == 0) /* if child process, execute */ 36 | { 37 | sleep(1); 38 | _execve(cmd); 39 | } 40 | } 41 | 42 | return (0); 43 | } 44 | -------------------------------------------------------------------------------- /0x15-simple_shell_practice/9-set_unsetenv: -------------------------------------------------------------------------------- 1 | #include "shell.h" 2 | #include "errno.h" 3 | 4 | /** 5 | * main - set and unset environment 6 | * Return: 0 on success 7 | */ 8 | int main(void) 9 | { 10 | char *var; 11 | extern char** environ; 12 | 13 | // while (environ[i]) 14 | // { 15 | // printf("%s\n", environ[i]); 16 | // i++; 17 | // } 18 | // 19 | /* create new environmental variable */ 20 | setenv("TEST_VAR", NULL, 0); 21 | var = getenv("TEST_VAR"); 22 | printf("%s\n", var); 23 | 24 | /* delete environmental variable */ 25 | unsetenv("TEST_VAR"); 26 | var = getenv("TEST_VAR"); 27 | 28 | /* no error - will print 0: success */ 29 | printf("%d: %s\n", errno, strerror(errno)); 30 | 31 | return (0); 32 | } 33 | 34 | /* int setenv(const char *name, const char *value, int overwrite); */ 35 | /* int unsetenv(const char *name); */ 36 | -------------------------------------------------------------------------------- /0x15-simple_shell_practice/main.c: -------------------------------------------------------------------------------- 1 | #include "shell.h" 2 | 3 | /* 4 | * main - simulates mini shell environment that accepts user commands 5 | * Return: 0 upon success 6 | */ 7 | 8 | int main(void) 9 | { 10 | char *cmd = NULL; 11 | int i, rd, status; 12 | 13 | do { 14 | /* prompt user till they hit Ctrl-D */ 15 | printf("Kev Mel Shell$ "); 16 | 17 | rd = _getline(&cmd); 18 | if (rd == 0) 19 | { 20 | printf("Hmm... Couldn't read what you wrote...\n"); 21 | return (-1); 22 | } 23 | 24 | i = 0; 25 | if (cmd[i] == 0) /* if Ctrl-D, print newline and exit */ 26 | { 27 | printf("\n"); 28 | exit(99); 29 | } 30 | while (cmd[i] != '\n') 31 | i++; 32 | cmd[i] = '\0'; 33 | 34 | /* execute child process/cmd before reprompting */ 35 | if (fork() == 0) 36 | { 37 | _execve(cmd); 38 | } 39 | else 40 | wait(&status); 41 | free(cmd); 42 | } while (rd != EOF); 43 | free(cmd); 44 | return (0); 45 | } 46 | -------------------------------------------------------------------------------- /0x15-simple_shell_practice/shell.h: -------------------------------------------------------------------------------- 1 | #ifndef SHELL 2 | #define SHELL 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | /* global variable for environmental variables */ 17 | extern char **environ; 18 | 19 | /* some function prototypes */ 20 | int _getline(char **cmd); 21 | char **_strtok(char *cmd); 22 | void _execve(char cmd[]); 23 | char *_strdup(char *str); 24 | char *c_strdup(char *str, int cs); 25 | char * _getenv(); 26 | char *_which(char *cmd); 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /0x16-doubly_linked_lists/0-print_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * print_dlistint - prints all data of doubly linked list 5 | * @h: pointer to head of list 6 | * Return: number of elements 7 | */ 8 | size_t print_dlistint(const dlistint_t *h) 9 | { 10 | size_t nodes = 0; 11 | 12 | while (h) 13 | { 14 | printf("%d\n", h->n); 15 | h = h->next; 16 | nodes += 1; 17 | } 18 | 19 | return (nodes); 20 | } 21 | -------------------------------------------------------------------------------- /0x16-doubly_linked_lists/1-dlistint_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * dlistint_len - counts and returns num elements in doubly linked list 5 | * @h: pointer to head of list 6 | * Return: number of elements 7 | */ 8 | size_t dlistint_len(const dlistint_t *h) 9 | { 10 | size_t nodes = 0; 11 | 12 | while (h) 13 | { 14 | h = h->next; 15 | nodes += 1; 16 | } 17 | 18 | return (nodes); 19 | } 20 | -------------------------------------------------------------------------------- /0x16-doubly_linked_lists/100-password: -------------------------------------------------------------------------------- 1 | en C Pyfo neZ -------------------------------------------------------------------------------- /0x16-doubly_linked_lists/102-palindromes.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | 3 | def isPalindrome(mul): 4 | return(str(mul) == str(mul)[::-1]) 5 | 6 | def find_biggest_palindrome(): 7 | biggestPal = 0 8 | for i in range(999, 100, -1): 9 | for j in range (999, 100, -1): 10 | mul = i * j 11 | if (isPalindrome(mul)): 12 | if mul > biggestPal: 13 | biggestPal = mul 14 | return(biggestPal) 15 | 16 | print(find_biggest_palindrome()) 17 | -------------------------------------------------------------------------------- /0x16-doubly_linked_lists/102-result: -------------------------------------------------------------------------------- 1 | 906609 -------------------------------------------------------------------------------- /0x16-doubly_linked_lists/2-add_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /** 5 | * add_dnodeint - add node to front of doubly linked list 6 | * @head: pointer to head of list 7 | * @n: node data 8 | * Return: address of new node, or Null if failed 9 | */ 10 | dlistint_t *add_dnodeint(dlistint_t **head, const int n) 11 | { 12 | dlistint_t *new; 13 | 14 | if (head == NULL) 15 | return (NULL); 16 | 17 | /* malloc and set new node data */ 18 | new = malloc(sizeof(struct dlistint_s)); 19 | if (!new) 20 | return (NULL); 21 | new->n = n; 22 | 23 | /* account for empty linked list */ 24 | if (*head == NULL) 25 | { 26 | *head = new; 27 | new->next = NULL; 28 | new->prev = NULL; 29 | return (new); 30 | } 31 | 32 | /* account for regular linked list */ 33 | new->next = *head; 34 | new->prev = NULL; 35 | (*head)->prev = new; 36 | *head = new; 37 | return (new); 38 | } 39 | -------------------------------------------------------------------------------- /0x16-doubly_linked_lists/3-add_dnodeint_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /** 5 | * add_dnodeint_end - add node to end of doubly linked list 6 | * @head: pointer to head of list 7 | * @n: node data 8 | * Return: address of new node, or Null if failed 9 | */ 10 | dlistint_t *add_dnodeint_end(dlistint_t **head, const int n) 11 | { 12 | dlistint_t *new, *last; 13 | 14 | if (!head) 15 | return (NULL); 16 | 17 | /* malloc and set new node data */ 18 | new = malloc(sizeof(struct dlistint_s)); 19 | if (!new) 20 | return (NULL); 21 | new->n = n; 22 | 23 | /* account for empty linked list */ 24 | if (*head == NULL) 25 | { 26 | *head = new; 27 | new->next = NULL; 28 | new->prev = NULL; 29 | return (new); 30 | } 31 | 32 | /* traverse to last node and insert */ 33 | last = *head; 34 | while (last->next != NULL) 35 | last = last->next; 36 | new->next = NULL; 37 | new->prev = last; 38 | last->next = new; 39 | 40 | return (new); 41 | } 42 | -------------------------------------------------------------------------------- /0x16-doubly_linked_lists/4-free_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /** 5 | * free_dlistint - frees a doubly linked list with only int data, no strings 6 | * @head: pointer to head of list 7 | */ 8 | void free_dlistint(dlistint_t *head) 9 | { 10 | /* return if empty list */ 11 | if (!head) 12 | return; 13 | 14 | while (head && head->next) 15 | { 16 | head = head->next; 17 | free(head->prev); 18 | } 19 | free(head); 20 | } 21 | -------------------------------------------------------------------------------- /0x16-doubly_linked_lists/5-get_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * get_dnodeint_at_index - get node at index 5 | * @head: pointer to head of doubly linked list 6 | * @index: given index 7 | * Return: pointer to node 8 | */ 9 | dlistint_t *get_dnodeint_at_index(dlistint_t *head, unsigned int index) 10 | { 11 | if (!head) 12 | return (NULL); 13 | 14 | /* countdown till index matches or end of list */ 15 | while (index && head->next) 16 | { 17 | head = head->next; 18 | index -= 1; 19 | } 20 | if (index == 0) 21 | return (head); 22 | 23 | /* index out of range */ 24 | return (NULL); 25 | } 26 | -------------------------------------------------------------------------------- /0x16-doubly_linked_lists/6-sum_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * sum_dlistint - get sum of data in doubly linked list 5 | * @head: pointer to head of doubly linked list 6 | * Return: sum 7 | */ 8 | int sum_dlistint(dlistint_t *head) 9 | { 10 | int sum = 0; 11 | 12 | if (!head) 13 | return (sum); 14 | 15 | while (head) 16 | { 17 | sum += head->n; 18 | head = head->next; 19 | } 20 | return (sum); 21 | } 22 | -------------------------------------------------------------------------------- /0x16-doubly_linked_lists/main_test_files.c/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code for Holberton School students. 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | dlistint_t *head; 14 | dlistint_t *new; 15 | dlistint_t hello = {8, NULL, NULL}; 16 | size_t n; 17 | 18 | head = &hello; 19 | new = malloc(sizeof(dlistint_t)); 20 | if (new == NULL) 21 | { 22 | dprintf(2, "Error: Can't malloc\n"); 23 | return (EXIT_FAILURE); 24 | } 25 | new->n = 9; 26 | head->prev = new; 27 | new->next = head; 28 | new->prev = NULL; 29 | head = new; 30 | n = print_dlistint(head); 31 | printf("-> %lu elements\n", n); 32 | free(new); 33 | return (EXIT_SUCCESS); 34 | } 35 | -------------------------------------------------------------------------------- /0x16-doubly_linked_lists/main_test_files.c/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code for Holberton School students. 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | dlistint_t *head; 14 | dlistint_t *new; 15 | dlistint_t hello = {8, NULL, NULL}; 16 | size_t n; 17 | 18 | head = &hello; 19 | new = malloc(sizeof(dlistint_t)); 20 | if (new == NULL) 21 | { 22 | dprintf(2, "Error: Can't malloc\n"); 23 | return (EXIT_FAILURE); 24 | } 25 | new->n = 9; 26 | head->prev = new; 27 | new->next = head; 28 | new->prev = NULL; 29 | head = new; 30 | n = dlistint_len(head); 31 | printf("-> %lu elements\n", n); 32 | free(new); 33 | return (EXIT_SUCCESS); 34 | } 35 | -------------------------------------------------------------------------------- /0x16-doubly_linked_lists/main_test_files.c/3-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code for Holberton School students. 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | dlistint_t *head; 14 | 15 | head = NULL; 16 | add_dnodeint_end(&head, 55); 17 | add_dnodeint_end(&head, 1); 18 | add_dnodeint_end(&head, 2); 19 | add_dnodeint_end(&head, 3); 20 | add_dnodeint_end(&head, 4); 21 | add_dnodeint_end(&head, 98); 22 | add_dnodeint_end(&head, 402); 23 | add_dnodeint_end(&head, 1024); 24 | print_dlistint(head); 25 | return (EXIT_SUCCESS); 26 | } 27 | -------------------------------------------------------------------------------- /0x16-doubly_linked_lists/main_test_files.c/4-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code for Holberton School students. 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | dlistint_t *head; 14 | 15 | head = NULL; 16 | add_dnodeint_end(&head, 0); 17 | add_dnodeint_end(&head, 1); 18 | add_dnodeint_end(&head, 2); 19 | add_dnodeint_end(&head, 3); 20 | add_dnodeint_end(&head, 4); 21 | add_dnodeint_end(&head, 98); 22 | add_dnodeint_end(&head, 402); 23 | add_dnodeint_end(&head, 1024); 24 | print_dlistint(head); 25 | free_dlistint(head); 26 | head = NULL; 27 | return (EXIT_SUCCESS); 28 | } 29 | -------------------------------------------------------------------------------- /0x17-dynamic_libraries/1-create_dynamic_lib.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -c -fPIC *.c 3 | gcc -shared -o liball.so *.o 4 | -------------------------------------------------------------------------------- /0x17-dynamic_libraries/100-operations.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MelissaN/holbertonschool-low_level_programming/b45ee1afb3ce2bf5a61d65623fcecbe36bad26d2/0x17-dynamic_libraries/100-operations.so -------------------------------------------------------------------------------- /0x17-dynamic_libraries/100-tests.py: -------------------------------------------------------------------------------- 1 | import random 2 | import ctypes 3 | 4 | cops = ctypes.CDLL('./100-operations.so') 5 | a = random.randint(-111, 111) 6 | b = random.randint(-111, 111) 7 | print("{} + {} = {}".format(a, b, cops.add(a, b))) 8 | print("{} - {} = {}".format(a, b, cops.sub(a, b))) 9 | print("{} x {} = {}".format(a, b, cops.mul(a, b))) 10 | print("{} / {} = {}".format(a, b, cops.div(a, b))) 11 | print("{} % {} = {}".format(a, b, cops.mod(a, b))) 12 | -------------------------------------------------------------------------------- /0x17-dynamic_libraries/README.md: -------------------------------------------------------------------------------- 1 | # C DYNAMIC/SHARED LIBRARIES 2 | 3 | ## Requirements 4 | * Language: C 5 | * OS: Ubuntu 14.04 LTS 6 | * Compiler: gcc 4.8.4 7 | * Style guidelines: [Betty style] (https://github.com/holbertonschool/Betty/wiki) 8 | 9 | ## Synopsis 10 | This repository illustrates how to create a dynamic library in Linux. To read how to do so, read my blog here 11 | 12 | ## Description of important files: 13 | 0. create_dynamic_lib.sh - shell script to create dynamic library named liball.so 14 | 1. libholberton.so - created this dynamic library containing some functions as liball.so 15 | 2. functions_folder - all .c files hold functions; all .o files were used to create dynamic library 16 | 3. holberton.h - included function prototypes of functions in library 17 | 4. len - executable program to run 0-main.c 18 | 5. 100-operations.so - created this C dynamic library to be imported into python program 100-tests.py 19 | 20 | ## Authors 21 | * Melissa Ng 22 | -------------------------------------------------------------------------------- /0x17-dynamic_libraries/functions_folder/0-isupper.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _isupper - checks for uppercase character 5 | * @c: character to be checked 6 | * Return: 1 if true, 0 if false 7 | */ 8 | 9 | int _isupper(int c) 10 | { 11 | if (c >= 'A' && c <= 'Z') 12 | return (1); 13 | 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x17-dynamic_libraries/functions_folder/0-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * main - check the code for Holberton School students. 7 | * 8 | * Return: Always EXIT_SUCCESS. 9 | */ 10 | int main(void) 11 | { 12 | printf("%d\n", _strlen("Holberton")); 13 | return (EXIT_SUCCESS); 14 | } 15 | -------------------------------------------------------------------------------- /0x17-dynamic_libraries/functions_folder/0-memset.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _memset - fill n bytes of memory with a constant value 5 | * @s: pointer to memory area 6 | * @b: constant value 7 | * @n: number of bytes to fill 8 | * Return: memory area 9 | */ 10 | 11 | char *_memset(char *s, char b, unsigned int n) 12 | { 13 | 14 | int size = n; /* only accept positive sizes */ 15 | 16 | if (size > 0) 17 | { 18 | int i; 19 | 20 | for (i = 0; i < size; i++) 21 | s[i] = b; 22 | } 23 | 24 | return (s); 25 | } 26 | -------------------------------------------------------------------------------- /0x17-dynamic_libraries/functions_folder/0-strcat.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _strcat - concatenate two strings 5 | * @dest: string to be appended to 6 | * @src: string to append 7 | * Return: concatenated string 8 | */ 9 | 10 | char *_strcat(char *dest, char *src) 11 | { 12 | int i = 0; 13 | int j = 0; 14 | 15 | while (dest[i] != '\0') 16 | i++; 17 | 18 | while (src[j] != '\0') 19 | { 20 | dest[i] = src[j]; 21 | i++; 22 | j++; 23 | } 24 | dest[i] = '\0'; 25 | 26 | return (dest); 27 | } 28 | -------------------------------------------------------------------------------- /0x17-dynamic_libraries/functions_folder/1-isdigit.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _isdigit - checks for a digit 0 through 9 5 | * @c: digit to be checked 6 | * Return: 1 if true, 0 if false 7 | */ 8 | 9 | int _isdigit(int c) 10 | { 11 | if (c >= '0' && c <= '9') 12 | return (1); 13 | 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x17-dynamic_libraries/functions_folder/1-memcpy.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _memcpy - copy n bytes of memory from source to destination 5 | * @dest: destination 6 | * @src: source 7 | * @n: number of bytes 8 | * Return: pointer to dest 9 | */ 10 | 11 | char *_memcpy(char *dest, char *src, unsigned int n) 12 | { 13 | 14 | int size = n; /* only accept positive sizes */ 15 | 16 | if (size > 0) 17 | { 18 | int i; 19 | 20 | for (i = 0; i < size; i++) 21 | dest[i] = src[i]; 22 | } 23 | 24 | return (dest); 25 | } 26 | -------------------------------------------------------------------------------- /0x17-dynamic_libraries/functions_folder/1-strncat.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _strncat - concatenate n bytes to destination string 5 | * @dest: string to be appended to 6 | * @src: string to append 7 | * @n: append n number of bytes(chars) 8 | * Return: concatenated string 9 | */ 10 | 11 | char *_strncat(char *dest, char *src, int n) 12 | { 13 | int i = 0; 14 | int j = 0; 15 | 16 | while (dest[i] != '\0') /*i is index of null terminator*/ 17 | i++; 18 | 19 | while (src[j] != src[n]) /*append replacing null terminator*/ 20 | { 21 | dest[i] = src[j]; 22 | i++; 23 | j++; 24 | } 25 | dest[i] = '\0'; 26 | 27 | return (dest); 28 | } 29 | -------------------------------------------------------------------------------- /0x17-dynamic_libraries/functions_folder/2-strchr.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #define NULL 0 3 | 4 | /** 5 | * _strchr - locate 1st occurrence of char in string and returns pointer there 6 | * @s: string to search 7 | * @c: target characer 8 | * Return: pointer to that character in string 9 | */ 10 | 11 | char *_strchr(char *s, char c) 12 | { 13 | int i = 0; 14 | 15 | while (s[i] != '\0' && s[i] != c) /* find match */ 16 | i++; 17 | 18 | if (s[i] == c) /* if match, assign to address */ 19 | return (&s[i]); 20 | else 21 | return (NULL); 22 | } 23 | -------------------------------------------------------------------------------- /0x17-dynamic_libraries/functions_folder/2-strlen.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _strlen - returns the length of a string 5 | * @s: string 6 | * Return: length 7 | */ 8 | 9 | int _strlen(char *s) 10 | { 11 | int len = 0; 12 | 13 | while (*s != '\0') 14 | { 15 | len++; 16 | s++; 17 | } 18 | 19 | return (len); 20 | } 21 | -------------------------------------------------------------------------------- /0x17-dynamic_libraries/functions_folder/2-strncpy.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _strncpy - copies n bytes of a source into buffer of a destination string, 5 | * @dest: copy source to this buffer 6 | * @src: this is the source to copy 7 | * @n: n bytes to be copied 8 | * Return: copied string 9 | */ 10 | 11 | char *_strncpy(char *dest, char *src, int n) 12 | { 13 | int i; 14 | 15 | for (i = 0; i < n && src[i] != '\0'; i++) 16 | dest[i] = src[i]; 17 | while (i < n) 18 | { 19 | dest[i] = '\0'; 20 | i++; 21 | } 22 | 23 | return (dest); 24 | } 25 | -------------------------------------------------------------------------------- /0x17-dynamic_libraries/functions_folder/3-islower.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _islower - checks for lowercase character 5 | * @c: character to check 6 | * Return: 1 if lowercase, 0 if uppercase 7 | */ 8 | 9 | int _islower(int c) 10 | { 11 | if (c >= 'a' && c <= 'z') 12 | { 13 | return (1); 14 | } 15 | else 16 | { 17 | return (0); 18 | } 19 | _putchar('\n'); 20 | } 21 | -------------------------------------------------------------------------------- /0x17-dynamic_libraries/functions_folder/3-op_functions.c: -------------------------------------------------------------------------------- 1 | /** 2 | * add - add 3 | * @a: integer 4 | * @b: integer 5 | * Return: result 6 | */ 7 | int add(int a, int b) 8 | { 9 | return (a + b); 10 | } 11 | 12 | /** 13 | * sub - subtract 14 | * @a: integer 15 | * @b: integer 16 | * Return: result 17 | */ 18 | int sub(int a, int b) 19 | { 20 | return (a - b); 21 | } 22 | 23 | /** 24 | * mul - multiply 25 | * @a: integer 26 | * @b: integer 27 | * Return: result 28 | */ 29 | int mul(int a, int b) 30 | { 31 | return (a * b); 32 | } 33 | 34 | /** 35 | * div - divide 36 | * @a: integer 37 | * @b: integer 38 | * Return: result 39 | */ 40 | int div(int a, int b) 41 | { 42 | return (a / b); 43 | } 44 | 45 | /** 46 | * mod - modulus for remainder 47 | * @a: integer 48 | * @b: integer 49 | * Return: result 50 | */ 51 | int mod(int a, int b) 52 | { 53 | return (a % b); 54 | } 55 | -------------------------------------------------------------------------------- /0x17-dynamic_libraries/functions_folder/3-puts.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _puts - prints a string, followed by a new line, to stdout 5 | * @str: string to print 6 | */ 7 | 8 | void _puts(char *str) 9 | { 10 | while (*str != '\0') 11 | _putchar(*str++); 12 | 13 | _putchar('\n'); 14 | } 15 | -------------------------------------------------------------------------------- /0x17-dynamic_libraries/functions_folder/3-strcmp.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _strcmp - compare two strings 5 | * @s1: one string 6 | * @s2: one string 7 | * Return: int that tells num spaces in between 8 | */ 9 | 10 | int _strcmp(char *s1, char *s2) 11 | { 12 | int i = 0; 13 | 14 | while (*(s1 + i) == *(s2 + i) && *(s1 + i)) 15 | i++; 16 | 17 | if (*(s2 + i)) 18 | return (*(s1 + i) - *(s2 + i)); 19 | else 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /0x17-dynamic_libraries/functions_folder/3-strspn.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _strspn - return length of string that matches values consistently 5 | * @s: string to search 6 | * @accept: target matches 7 | * Return: number of bytes consecutively matched 8 | */ 9 | 10 | unsigned int _strspn(char *s, char *accept) 11 | { 12 | int i = 0, j; 13 | int matches = 0; 14 | 15 | while (s[i] != '\0') /*iterate through string*/ 16 | { 17 | 18 | for (j = 0; accept[j] != '\0'; j++) /*iterate through target*/ 19 | { 20 | if (s[i] == accept[j]) /*record & break at first match*/ 21 | { 22 | matches++; 23 | break; 24 | } 25 | if (accept[j + 1] == '\0' && s[i] != accept[j]) 26 | return (matches);/*return if idx doesn't match*/ 27 | } 28 | i++; 29 | } 30 | return (matches); /* return num if all match till end */ 31 | 32 | } 33 | -------------------------------------------------------------------------------- /0x17-dynamic_libraries/functions_folder/4-isalpha.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _isalpha - checks for alphabet character 5 | * @c: character to check 6 | * Return: 1 if lower or uppercase, 0 if not alphabet character 7 | */ 8 | 9 | int _isalpha(int c) 10 | { 11 | if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) 12 | { 13 | return (1); 14 | } 15 | else 16 | { 17 | return (0); 18 | } 19 | _putchar('\n'); 20 | } 21 | -------------------------------------------------------------------------------- /0x17-dynamic_libraries/functions_folder/4-strpbrk.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #define NULL 0 3 | 4 | /** 5 | * _strpbrk - return pointer to byte in s that matches a byte in accept 6 | * @s: string to search 7 | * @accept: target matches 8 | * Return: pointer to index of string at first occurence 9 | */ 10 | 11 | char *_strpbrk(char *s, char *accept) 12 | { 13 | int i = 0, j; 14 | 15 | while (s[i] != '\0') /*iterate through string*/ 16 | { 17 | 18 | for (j = 0; accept[j] != '\0'; j++) /* iterate through target */ 19 | { 20 | if (s[i] == accept[j]) /* stop at first match */ 21 | { 22 | s = &s[i]; /* set pointer to first occurence */ 23 | return (s); 24 | } 25 | } 26 | i++; 27 | } 28 | return (NULL); /* return NULL if no matches */ 29 | 30 | } 31 | -------------------------------------------------------------------------------- /0x17-dynamic_libraries/functions_folder/6-abs.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _abs - compute absolute value of integer 5 | * @n: integer argument 6 | * Return: absolute value 7 | */ 8 | 9 | int _abs(int n) 10 | { 11 | if (n > 0) 12 | return (n); 13 | else if (n < 0) 14 | return (-n); 15 | 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x17-dynamic_libraries/functions_folder/9-strcpy.c: -------------------------------------------------------------------------------- 1 | #include "holberton.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: copy source to this buffer 8 | * @src: this is the source to copy 9 | * Return: copy of original source 10 | */ 11 | 12 | char *_strcpy(char *dest, char *src) 13 | { 14 | int i, len; 15 | 16 | while (src[len] != '\0') 17 | len++; 18 | 19 | 20 | for (i = 0; i <= len; i++) 21 | dest[i] = src[i]; 22 | 23 | return (dest); 24 | } 25 | -------------------------------------------------------------------------------- /0x17-dynamic_libraries/functions_folder/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _putchar - writes the character c to stdout 5 | * @c: The character to print 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | int _putchar(char c) 11 | { 12 | return (write(1, &c, 1)); 13 | } 14 | -------------------------------------------------------------------------------- /0x17-dynamic_libraries/holberton.h: -------------------------------------------------------------------------------- 1 | #ifndef HOLBERTON_H 2 | #define HOLBERTON_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 | -------------------------------------------------------------------------------- /0x17-dynamic_libraries/len: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MelissaN/holbertonschool-low_level_programming/b45ee1afb3ce2bf5a61d65623fcecbe36bad26d2/0x17-dynamic_libraries/len -------------------------------------------------------------------------------- /0x17-dynamic_libraries/liball.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MelissaN/holbertonschool-low_level_programming/b45ee1afb3ce2bf5a61d65623fcecbe36bad26d2/0x17-dynamic_libraries/liball.so -------------------------------------------------------------------------------- /0x17-dynamic_libraries/libholberton.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MelissaN/holbertonschool-low_level_programming/b45ee1afb3ce2bf5a61d65623fcecbe36bad26d2/0x17-dynamic_libraries/libholberton.so -------------------------------------------------------------------------------- /0x19-hash_tables/0-hash_table_create.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_create - create hash table given size 5 | * @size: size 6 | * Return: pointer to table; NULL if error 7 | */ 8 | hash_table_t *hash_table_create(unsigned long int size) 9 | { 10 | hash_table_t *ht; 11 | hash_node_t **array; 12 | unsigned long int i = 0; 13 | 14 | if (size == 0) 15 | return (NULL); 16 | 17 | ht = malloc(sizeof(hash_table_t)); 18 | if (!ht) 19 | return (NULL); 20 | array = malloc(sizeof(*array) * size); 21 | if (!array) 22 | { 23 | free(ht); 24 | return (NULL); 25 | } 26 | while (i < size) 27 | { 28 | array[i] = NULL; 29 | i++; 30 | } 31 | 32 | ht->size = size; 33 | ht->array = array; 34 | 35 | return (ht); 36 | } 37 | -------------------------------------------------------------------------------- /0x19-hash_tables/1-djb2.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_djb2 - implement djb2 algorithm (one of best string hash functions) 5 | * source code copied from http://www.cse.yorku.ca/~oz/hash.html 6 | * 7 | * @str: value 8 | * Return: key 9 | */ 10 | unsigned long int hash_djb2(const unsigned char *str) 11 | { 12 | unsigned long hash = 5381; 13 | int c; 14 | 15 | while ((c = *str++)) 16 | hash = ((hash << 5) + hash) + c; /* hash * 33 + c */ 17 | 18 | return (hash); 19 | } 20 | -------------------------------------------------------------------------------- /0x19-hash_tables/2-key_index.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * key_index - gives index of a key 5 | * @key: key 6 | * @size: size of hash table array 7 | * Return: index where key/value pair is stored in hash table array 8 | */ 9 | unsigned long int key_index(const unsigned char *key, unsigned long int size) 10 | { 11 | unsigned long int idx; 12 | 13 | if (size == 0) 14 | return (0); 15 | 16 | idx = hash_djb2(key); 17 | return (idx % size); 18 | } 19 | -------------------------------------------------------------------------------- /0x19-hash_tables/4-hash_table_get.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_get - given key, get value 5 | * @ht: hash table 6 | * @key: key 7 | * Return: value; or NULL if not found 8 | */ 9 | char *hash_table_get(const hash_table_t *ht, const char *key) 10 | { 11 | unsigned long int idx; 12 | hash_node_t *tmp; 13 | 14 | /* find index in hash table where key is */ 15 | /* look through linked list to find matching key for value */ 16 | 17 | if (!ht || !key) 18 | return (NULL); 19 | idx = key_index((const unsigned char *)key, ht->size); 20 | 21 | tmp = (ht->array)[idx]; 22 | while (tmp != NULL && strcmp(tmp->key, key) != 0) 23 | tmp = tmp->next; 24 | if (!tmp) 25 | return (NULL); 26 | else 27 | return (tmp->value); 28 | } 29 | -------------------------------------------------------------------------------- /0x19-hash_tables/5-hash_table_print.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_print - print key/values of hash table in order 5 | * @ht: hash table 6 | */ 7 | void hash_table_print(const hash_table_t *ht) 8 | { 9 | unsigned long int idx = 0; 10 | hash_node_t *node; 11 | char *comma = ""; 12 | 13 | if (!ht || !ht->array) 14 | return; 15 | 16 | putchar('{'); 17 | while (idx < ht->size) 18 | { 19 | node = ((ht->array)[idx]); 20 | while (node) 21 | { 22 | printf("%s'%s': '%s'", comma, node->key, node->value); 23 | comma = ", "; 24 | node = node->next; 25 | } 26 | idx++; 27 | } 28 | puts("}"); 29 | } 30 | -------------------------------------------------------------------------------- /0x19-hash_tables/6-hash_table_delete.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_delete - free and delete hash table 5 | * @ht: hash table 6 | */ 7 | void hash_table_delete(hash_table_t *ht) 8 | { 9 | unsigned long int idx = 0; 10 | hash_node_t *node, *next; 11 | 12 | if (!ht) 13 | return; 14 | 15 | if (!(ht->array)) 16 | { 17 | free(ht); 18 | return; 19 | } 20 | 21 | while (idx < ht->size) 22 | { 23 | node = (ht->array)[idx]; 24 | while (node) 25 | { 26 | next = node->next; 27 | if (node->key) 28 | free(node->key); 29 | if (node->value) 30 | free(node->value); 31 | node->key = NULL; 32 | node->value = NULL; 33 | free(node); 34 | node = next; 35 | } 36 | idx++; 37 | } 38 | free(ht->array); 39 | free(ht); 40 | } 41 | -------------------------------------------------------------------------------- /0x19-hash_tables/main_files/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "hash_tables.h" 5 | 6 | /** 7 | * main - check the code for Holberton School students. 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | hash_table_t *ht; 14 | 15 | ht = hash_table_create(1024); 16 | printf("%p\n", (void *)ht); 17 | return (EXIT_SUCCESS); 18 | } -------------------------------------------------------------------------------- /0x19-hash_tables/main_files/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "hash_tables.h" 5 | 6 | /** 7 | * main - check the code for Holberton School students. 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | char *s; 14 | 15 | s = "cisfun"; 16 | printf("%lu\n", hash_djb2((unsigned char *)s)); 17 | s = "Don't forget to tweet today"; 18 | printf("%lu\n", hash_djb2((unsigned char *)s)); 19 | s = "98"; 20 | printf("%lu\n", hash_djb2((unsigned char *)s)); 21 | return (EXIT_SUCCESS); 22 | } 23 | -------------------------------------------------------------------------------- /0x19-hash_tables/main_files/100-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "hash_tables.h" 5 | 6 | /** 7 | * main - check the code for Holberton School students. 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | shash_table_t *ht; 14 | 15 | ht = shash_table_create(1024); 16 | shash_table_set(ht, "y", "0"); 17 | shash_table_print(ht); 18 | shash_table_set(ht, "j", "1"); 19 | shash_table_print(ht); 20 | shash_table_set(ht, "c", "2"); 21 | shash_table_print(ht); 22 | shash_table_set(ht, "b", "3"); 23 | shash_table_print(ht); 24 | shash_table_set(ht, "z", "4"); 25 | shash_table_print(ht); 26 | shash_table_set(ht, "n", "5"); 27 | shash_table_print(ht); 28 | shash_table_set(ht, "a", "6"); 29 | shash_table_print(ht); 30 | shash_table_set(ht, "m", "7"); 31 | shash_table_print(ht); 32 | shash_table_print_rev(ht); 33 | shash_table_delete(ht); 34 | return (EXIT_SUCCESS); 35 | } 36 | -------------------------------------------------------------------------------- /0x19-hash_tables/main_files/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "hash_tables.h" 5 | 6 | /** 7 | * main - check the code for Holberton School students. 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | char *s; 14 | unsigned long int hash_table_array_size; 15 | 16 | hash_table_array_size = 1024; 17 | s = "cisfun"; 18 | printf("%lu\n", hash_djb2((unsigned char *)s)); 19 | printf("%lu\n", key_index((unsigned char *)s, hash_table_array_size)); 20 | s = "Don't forget to tweet today"; 21 | printf("%lu\n", hash_djb2((unsigned char *)s)); 22 | printf("%lu\n", key_index((unsigned char *)s, hash_table_array_size)); 23 | s = "98"; 24 | printf("%lu\n", hash_djb2((unsigned char *)s)); 25 | printf("%lu\n", key_index((unsigned char *)s, hash_table_array_size)); 26 | 27 | /* test size 0 and size 1 */ 28 | printf("%lu\n", key_index((unsigned char *)s, 0)); 29 | printf("%lu\n", key_index((unsigned char *)s, 1)); 30 | return (EXIT_SUCCESS); 31 | } 32 | -------------------------------------------------------------------------------- /0x19-hash_tables/main_files/5-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "hash_tables.h" 5 | 6 | /** 7 | * main - check the code for Holberton School students. 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | hash_table_t *ht; 14 | 15 | ht = hash_table_create(1024); 16 | hash_table_print(ht); 17 | hash_table_set(ht, "c", "fun"); 18 | hash_table_set(ht, "python", "awesome"); 19 | hash_table_set(ht, "Jennie", "and Jay love asm"); 20 | hash_table_set(ht, "N", "queens"); 21 | hash_table_set(ht, "Asterix", "Obelix"); 22 | hash_table_set(ht, "Betty", "Holberton"); 23 | hash_table_set(ht, "98", "Battery Street"); 24 | /* set collision to see if linked list prints in right order */ 25 | hash_table_set(ht, "heliotropes", "firstInSameIdx"); 26 | hash_table_set(ht, "neurospora", "secondInSameIdx"); 27 | hash_table_print(ht); 28 | return (EXIT_SUCCESS); 29 | } 30 | -------------------------------------------------------------------------------- /0x1A-sorting_algorithms/0-O: -------------------------------------------------------------------------------- 1 | O(n) 2 | O(n^2) 3 | O(n^2) 4 | -------------------------------------------------------------------------------- /0x1A-sorting_algorithms/0-bubble_sort.c: -------------------------------------------------------------------------------- 1 | #include "sort.h" 2 | /** 3 | * bubble_sort - sorts bubbly 4 | * @array: array to sort 5 | * @size: size 6 | * 7 | */ 8 | void bubble_sort(int *array, size_t size) 9 | { 10 | size_t i, newsize = size; 11 | int tmp, swap; 12 | 13 | if (array == NULL || size < 2) 14 | return; 15 | 16 | while (newsize) 17 | { 18 | swap = 0; 19 | for (i = 0; i < newsize - 1; i++) 20 | { 21 | if (array[i] > array[i + 1]) 22 | { 23 | tmp = array[i + 1]; 24 | array[i + 1] = array[i]; 25 | array[i] = tmp; 26 | print_array(array, size); 27 | swap = 1; 28 | } 29 | } 30 | newsize--; 31 | if (!swap) 32 | break; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /0x1A-sorting_algorithms/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "sort.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always 0 9 | */ 10 | int main(void) 11 | { 12 | int array[] = {19, 48, 99, 71, 13, 52, 96, 73, 86, 7}; 13 | size_t n = sizeof(array) / sizeof(array[0]); 14 | 15 | print_array(array, n); 16 | printf("\n"); 17 | bubble_sort(array, n); 18 | printf("\n"); 19 | print_array(array, n); 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /0x1A-sorting_algorithms/1-O: -------------------------------------------------------------------------------- 1 | O(n) 2 | O(n^2) 3 | O(n^2) 4 | -------------------------------------------------------------------------------- /0x1A-sorting_algorithms/100-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "sort.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always 0 9 | */ 10 | int main(void) 11 | { 12 | int array[] = {19, 48, 99, 71, 13, 52, 96, 73, 86, 7}; 13 | size_t n = sizeof(array) / sizeof(array[0]); 14 | 15 | print_array(array, n); 16 | printf("\n"); 17 | shell_sort(array, n); 18 | printf("\n"); 19 | print_array(array, n); 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /0x1A-sorting_algorithms/100-shell_sort.c: -------------------------------------------------------------------------------- 1 | #include "sort.h" 2 | /** 3 | * shell_sort - sorts shelly like 4 | * @array: array to take in 5 | * @size: size of array 6 | * 7 | */ 8 | void shell_sort(int *array, size_t size) 9 | { 10 | size_t gap = 1, i, j; 11 | int tmp; 12 | 13 | if (array == NULL || size < 2) 14 | return; 15 | 16 | while (gap < size / 3) 17 | gap = (gap * 3) + 1; 18 | 19 | while (gap) 20 | { 21 | for (i = gap; i < size; i++) 22 | { 23 | tmp = array[i]; 24 | for (j = i; j >= gap && array[j - gap] > tmp; j -= gap) 25 | { 26 | array[j] = array[j - gap]; 27 | } 28 | array[j] = tmp; 29 | } 30 | print_array(array, size); 31 | gap /= 3; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /0x1A-sorting_algorithms/101-O: -------------------------------------------------------------------------------- 1 | O(n) 2 | O(n^2) 3 | O(n^2) 4 | -------------------------------------------------------------------------------- /0x1A-sorting_algorithms/102-O: -------------------------------------------------------------------------------- 1 | O(n+k) 2 | O(n+k) 3 | O(n+k) 4 | -------------------------------------------------------------------------------- /0x1A-sorting_algorithms/102-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "sort.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always 0 9 | */ 10 | int main(void) 11 | { 12 | int array[] = {-19, 0, 19, 48, 0, 99, -71, 13, 52, 96, 73, 86, 7}; 13 | size_t n = sizeof(array) / sizeof(array[0]); 14 | 15 | print_array(array, n); 16 | printf("\n"); 17 | counting_sort(array, n); 18 | printf("\n"); 19 | print_array(array, n); 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /0x1A-sorting_algorithms/103-O: -------------------------------------------------------------------------------- 1 | O(nlog(n)) 2 | O(nlog(n)) 3 | O(nlog(n)) 4 | -------------------------------------------------------------------------------- /0x1A-sorting_algorithms/103-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "sort.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always 0 9 | */ 10 | int main(void) 11 | { 12 | int array[] = {19, 48, 99, 71, 13, 52, 96, 73, 86, 7}; 13 | size_t n = sizeof(array) / sizeof(array[0]); 14 | 15 | print_array(array, n); 16 | printf("\n"); 17 | merge_sort(array, n); 18 | printf("\n"); 19 | print_array(array, n); 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /0x1A-sorting_algorithms/2-O: -------------------------------------------------------------------------------- 1 | O(n^2) 2 | O(n^2) 3 | O(n^2) 4 | -------------------------------------------------------------------------------- /0x1A-sorting_algorithms/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "sort.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always 0 9 | */ 10 | int main(void) 11 | { 12 | int array[] = {19, 48, 99, 71, 13, 52, 96, 73, 86, 7}; 13 | size_t n = sizeof(array) / sizeof(array[0]); 14 | 15 | print_array(array, n); 16 | printf("\n"); 17 | selection_sort(array, n); 18 | printf("\n"); 19 | print_array(array, n); 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /0x1A-sorting_algorithms/2-selection_sort.c: -------------------------------------------------------------------------------- 1 | #include "sort.h" 2 | /** 3 | * selection_sort - sorts all selecty like 4 | * @array: array to sort 5 | * @size: size of array 6 | */ 7 | void selection_sort(int *array, size_t size) 8 | { 9 | int index = 0, min, tmp; 10 | size_t i, j; 11 | 12 | if (array == NULL || size < 2) 13 | return; 14 | 15 | for (i = 0; i < size - 1; i++) 16 | { 17 | min = array[i]; 18 | for (j = i + 1; j < size; j++) 19 | { 20 | if (array[j] < min) 21 | { 22 | min = array[j]; 23 | index = j; 24 | } 25 | } 26 | if (min != array[i]) 27 | { 28 | tmp = array[i]; 29 | array[i] = array[index]; 30 | array[index] = tmp; 31 | print_array(array, size); 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /0x1A-sorting_algorithms/3-O: -------------------------------------------------------------------------------- 1 | O(nlog(n)) 2 | O(nlog(n)) 3 | O(n^2) 4 | -------------------------------------------------------------------------------- /0x1A-sorting_algorithms/3-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "sort.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always 0 9 | */ 10 | int main(void) 11 | { 12 | int array[] = {19, 48, 99, 71, 13, 52, 96, 73, 86, 7}; 13 | size_t n = sizeof(array) / sizeof(array[0]); 14 | 15 | print_array(array, n); 16 | printf("\n"); 17 | quick_sort(array, n); 18 | printf("\n"); 19 | print_array(array, n); 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /0x1A-sorting_algorithms/print_array.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * print_array - Prints an array of integers 5 | * 6 | * @array: The array to be printed 7 | * @size: Number of elements in @array 8 | */ 9 | void print_array(const int *array, size_t size) 10 | { 11 | size_t i; 12 | 13 | i = 0; 14 | while (array && i < size) 15 | { 16 | if (i > 0) 17 | printf(", "); 18 | printf("%d", array[i]); 19 | ++i; 20 | } 21 | printf("\n"); 22 | } 23 | -------------------------------------------------------------------------------- /0x1A-sorting_algorithms/print_list.c: -------------------------------------------------------------------------------- 1 | #include "sort.h" 2 | 3 | /** 4 | * print_list - Prints a list of integers 5 | * 6 | * @list: The list to be printed 7 | */ 8 | void print_list(const listint_t *list) 9 | { 10 | int i; 11 | 12 | i = 0; 13 | while (list) 14 | { 15 | if (i > 0) 16 | printf(", "); 17 | printf("%d", list->n); 18 | ++i; 19 | list = list->next; 20 | } 21 | printf("\n"); 22 | } 23 | -------------------------------------------------------------------------------- /0x1A-sorting_algorithms/sort.h: -------------------------------------------------------------------------------- 1 | #ifndef SORT_H_ 2 | #define SORT_H_ 3 | #include 4 | #include 5 | /** 6 | * struct listint_s - Doubly linked list node 7 | * 8 | * @n: Integer stored in the node 9 | * @prev: Pointer to the previous element of the list 10 | * @next: Pointer to the next element of the list 11 | */ 12 | typedef struct listint_s 13 | { 14 | const int n; 15 | struct listint_s *prev; 16 | struct listint_s *next; 17 | } listint_t; 18 | 19 | /* print functions */ 20 | void print_list(const listint_t *list); 21 | void print_array(const int *array, size_t size); 22 | 23 | /* sorting functions */ 24 | void bubble_sort(int *array, size_t size); 25 | void insertion_sort_list(listint_t **list); 26 | void selection_sort(int *array, size_t size); 27 | void quick_sort(int *array, size_t size); 28 | void shell_sort(int *array, size_t size); 29 | void cocktail_sort_list(listint_t **list); 30 | void counting_sort(int *array, size_t size); 31 | void merge_sort(int *array, size_t size); 32 | #endif 33 | -------------------------------------------------------------------------------- /0x1B-makefiles/0-Makefile: -------------------------------------------------------------------------------- 1 | all: m.h main.c holberton.c 2 | gcc main.c holberton.c -o holberton 3 | -------------------------------------------------------------------------------- /0x1B-makefiles/1-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c holberton.c 3 | 4 | all: m.h $(SRC) 5 | $(CC) $(SRC) -o holberton 6 | -------------------------------------------------------------------------------- /0x1B-makefiles/100-Makefile: -------------------------------------------------------------------------------- 1 | # Usage: make [all/clean/oclean/fclean] -f [file] 2 | 3 | 4 | CC = gcc 5 | SRC = main.c holberton.c 6 | OBJ = $(SRC:.c=.o) 7 | NAME = holberton 8 | CFLAGS = -Wall -Werror -Wextra -pedantic 9 | 10 | # standard rule to say rules below are not filenames 11 | .PHONY: all clean oclean fclean re 12 | 13 | # makes executable 14 | all: m.h $(OBJ) 15 | $(CC) $(OBJ) -o $(NAME) 16 | 17 | # deletes tmp files & executable 18 | clean: 19 | $(RM) *~ $(NAME) 20 | 21 | # deletes object files 22 | oclean: 23 | $(RM) $(OBJ) 24 | 25 | # deletes all (tmp files, executable, & obj files) 26 | fclean: clean oclean 27 | 28 | # deletes all & recompiles all source files 29 | re: oclean all 30 | -------------------------------------------------------------------------------- /0x1B-makefiles/2-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c holberton.c 3 | OBJ = $(SRC:.c=.o) 4 | NAME = holberton 5 | 6 | all: m.h $(OBJ) 7 | $(CC) $(OBJ) -o $(NAME) 8 | -------------------------------------------------------------------------------- /0x1B-makefiles/3-Makefile: -------------------------------------------------------------------------------- 1 | # Usage: make [all/clean/oclean/fclean] -f [file] 2 | 3 | 4 | CC = gcc 5 | SRC = main.c holberton.c 6 | OBJ = $(SRC:.c=.o) 7 | NAME = holberton 8 | RM = rm -f 9 | 10 | # make all == makes executable 11 | all: m.h $(OBJ) 12 | $(CC) $(OBJ) -o $(NAME) 13 | 14 | # deletes tmp files & executable 15 | clean: 16 | $(RM) *~ $(NAME) 17 | 18 | # deletes object files 19 | oclean: 20 | $(RM) $(OBJ) 21 | 22 | # deletes all (tmp files, executable, & obj files) 23 | fclean: clean oclean 24 | 25 | # deletes all & recompiles all source files 26 | re: oclean all 27 | -------------------------------------------------------------------------------- /0x1B-makefiles/4-Makefile: -------------------------------------------------------------------------------- 1 | # Usage: make [all/clean/oclean/fclean] -f [file] 2 | 3 | 4 | CC = gcc 5 | SRC = main.c holberton.c 6 | OBJ = $(SRC:.c=.o) 7 | NAME = holberton 8 | RM = rm -f 9 | CFLAGS = -Wall -Werror -Wextra -pedantic 10 | 11 | # makes executable 12 | all: m.h $(OBJ) 13 | $(CC) $(OBJ) -o $(NAME) 14 | 15 | # deletes tmp files & executable 16 | clean: 17 | $(RM) *~ $(NAME) 18 | 19 | # deletes object files 20 | oclean: 21 | $(RM) $(OBJ) 22 | 23 | # deletes all (tmp files, executable, & obj files) 24 | fclean: clean oclean 25 | 26 | # deletes all & recompiles all source files 27 | re: oclean all 28 | -------------------------------------------------------------------------------- /0x1B-makefiles/m.h: -------------------------------------------------------------------------------- 1 | #ifndef __M_H__ 2 | #define __M_H__ 3 | 4 | #include 5 | #include 6 | 7 | void print_holberton(void); 8 | 9 | #endif 10 | -------------------------------------------------------------------------------- /0x1B-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_holberton(); 11 | return (EXIT_SUCCESS); 12 | } 13 | /* Holberton */ 14 | -------------------------------------------------------------------------------- /0x1D-search_algorithms/0-linear.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * linear_search - search linearly 5 | * @array: given array of ints 6 | * @size: size of array 7 | * @value: value to search for 8 | * Return: index at which value's found 9 | */ 10 | int linear_search(int *array, size_t size, int value) 11 | { 12 | size_t i; 13 | 14 | if (!array) 15 | return (-1); 16 | 17 | i = 0; 18 | while (i < size) 19 | { 20 | printf("Value checked array[%lu] = [%d]\n", i, array[i]); 21 | if (array[i] == value) 22 | return (i); 23 | i++; 24 | } 25 | return (-1); 26 | } 27 | -------------------------------------------------------------------------------- /0x1D-search_algorithms/101-O: -------------------------------------------------------------------------------- 1 | O(sqrt(n)) 2 | -------------------------------------------------------------------------------- /0x1D-search_algorithms/2-O: -------------------------------------------------------------------------------- 1 | O(n) 2 | -------------------------------------------------------------------------------- /0x1D-search_algorithms/3-O: -------------------------------------------------------------------------------- 1 | O(1) 2 | -------------------------------------------------------------------------------- /0x1D-search_algorithms/4-O: -------------------------------------------------------------------------------- 1 | O(log(n)) 2 | -------------------------------------------------------------------------------- /0x1D-search_algorithms/5-O: -------------------------------------------------------------------------------- 1 | O(1) 2 | -------------------------------------------------------------------------------- /0x1D-search_algorithms/6-O: -------------------------------------------------------------------------------- 1 | O(nm) 2 | -------------------------------------------------------------------------------- /0x1D-search_algorithms/README.md: -------------------------------------------------------------------------------- 1 | ## Search Algorithms in C 2 | > Linear search, binary search, space complexity. Resources: [Wiki on Binary Search](https://en.wikipedia.org/wiki/Binary_search_algorithm), 3 | > [Space Complexity & Data Structures in general](http://btechsmartclass.com/DS/U1_T3.html) 4 | 5 | ### Environment 6 | * Language: C v89 7 | * OS: Ubuntu 14.04 LTS 8 | * Compiler: gcc 4.8.4 9 | * Style guidelines: [Betty style for C89](https://github.com/holbertonschool/Betty/wiki) 10 | 11 | --- 12 | ### Authors 13 | Melissa Ng [![M](https://upload.wikimedia.org/wikipedia/fr/thumb/c/c8/Twitter_Bird.svg/30px-Twitter_Bird.svg.png)](https://twitter.com/MelissaNg__) 14 | 15 | -------------------------------------------------------------------------------- /0x1D-search_algorithms/main_files/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 | -------------------------------------------------------------------------------- /0x1D-search_algorithms/main_files/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 | -------------------------------------------------------------------------------- /0x1D-search_algorithms/main_files/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 | 21 | int arr[15] = { 22 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 23, 47, 89, 160 23 | }; 24 | printf("Found at index: %d\n", jump_search(arr, 15, 123456789)); 25 | return (EXIT_SUCCESS); 26 | } 27 | -------------------------------------------------------------------------------- /0x1D-search_algorithms/main_files/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 | 21 | int arr[35] = { 22 | 0, 0, 1, 2, 2, 2, 2, 3, 3, 4, 4, 5, 6, 6, 7, 8, 8, 8, 9, 9, 23 | 10, 11, 12, 12, 12, 19, 19, 19, 71, 71, 71, 81, 82, 83, 84 24 | }; 25 | printf("Found at index: %d\n", interpolation_search(arr, 35, 123456789)); 26 | printf("Found at index: %d\n", interpolation_search(NULL, 0, 0)); 27 | return (EXIT_SUCCESS); 28 | } 29 | -------------------------------------------------------------------------------- /0x1D-search_algorithms/search_algos.h: -------------------------------------------------------------------------------- 1 | #ifndef SEARCH_ALGOS 2 | #define SEARCH_ALGOS 3 | 4 | #include 5 | #include 6 | 7 | int linear_search(int *array, size_t size, int value); 8 | int binary_search(int *array, size_t size, int value); 9 | int jump_search(int *array, size_t size, int value); 10 | int interpolation_search(int *array, size_t size, int value); 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Low Level Programming :octocat: 2 | > Each folder in this repository illustrates an essential concept of programming, 3 | > specific to the C programming language. The file names reference the concepts 4 | > (i.e. pointers, recursion, malloc, free, structures, function pointers, variadic 5 | > functions, linked lists, etc) Inside these folders are files containing bash 6 | > scripts and functions around that particular concept. Some functions are 7 | > re-creations of functions found in the C standard libraries (i.e. _strcpy, _strlen, 8 | > etc). 9 | 10 | ### Environment 11 | * Language: C 12 | * OS: Ubuntu 14.04 LTS 13 | * Compiler: gcc 4.8.4 14 | * Style guidelines: [Betty style](https://github.com/holbertonschool/Betty/wiki) 15 | 16 | --- 17 | ### Authors 18 | Melissa Ng [![M](https://upload.wikimedia.org/wikipedia/fr/thumb/c/c8/Twitter_Bird.svg/30px-Twitter_Bird.svg.png)](https://twitter.com/MelissaNg__) 19 | --------------------------------------------------------------------------------