├── README.md ├── argc_argv ├── 0-whatsmyname.c ├── 1-args.c ├── 2-args.c ├── 3-mul.c ├── 4-add.c ├── README.md └── main.h ├── bit_manipulation ├── 0-binary_to_uint.c ├── 1-print_binary.c ├── 2-get_bit.c ├── 3-set_bit.c ├── 4-clear_bit.c ├── 5-flip_bits.c ├── README.md └── main.h ├── doubly_linked_lists ├── 0-print_dlistint.c ├── 1-dlistint_len.c ├── 2-add_dnodeint.c ├── 3-add_dnodeint_end.c ├── 4-free_dlistint.c ├── 5-get_dnodeint.c ├── 6-sum_dlistint.c ├── 7-insert_dnodeint.c ├── 8-delete_dnodeint.c ├── README.md └── lists.h ├── file_io ├── 0-read_textfile.c ├── 1-create_file.c ├── 2-append_text_to_file.c ├── 3-cp.c ├── README.md └── main.h ├── function_pointers ├── 0-print_name.c ├── 1-array_iterator.c ├── 2-int_index.c ├── 3-calc.h ├── 3-copy_main.c ├── 3-get_op_func.c ├── 3-main.c ├── 3-op_functions.c ├── README.md └── function_pointers.h ├── functions_nested_loops ├── 0-putchar.c ├── 1-alphabet.c ├── 10-add.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 └── main.h ├── hash_tables ├── 0-hash_table_create.c ├── 0-main.c ├── 1-djb2.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 ├── a └── hash_tables.h ├── 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 ├── a.out └── c ├── iacta ├── iacta.sh ├── makefiles ├── 0-Makefile ├── 1-Makefile ├── 100-Makefile ├── 2-Makefile ├── 3-Makefile ├── 4-Makefile ├── README.md ├── a.out ├── m.h ├── main.c ├── main.o ├── school ├── school.c └── school.o ├── malloc_free ├── 0-create_array.c ├── 0-main.c ├── 1-main.c ├── 1-strdup.c ├── 2-main.c ├── 2-str_concat ├── 2-str_concat.c ├── 3-alloc_grid ├── 3-alloc_grid.c ├── 3-main.c ├── 4-free_grid ├── 4-free_grid.c ├── 4-main.c ├── README.md ├── _putchar.c ├── a ├── main.h └── s ├── more_functions_nested_loops ├── 0-isuper ├── 0-isupper.c ├── 0-main.c ├── 1-isdigit ├── 1-isdigit.C ├── 1-isdigit.c ├── 1-main.c ├── 10-main.c ├── 10-print_triangle.c ├── 10-triangles ├── 100-prime_factor ├── 100-prime_factor.c ├── 101-main.c ├── 101-print_number.c ├── 101-print_numbers ├── 2-main.c ├── 2-mul ├── 2-mul.c ├── 3-main.c ├── 3-print_numbers ├── 3-print_numbers.c ├── 4-main.c ├── 4-print_most_numbers ├── 4-print_most_numbers.c ├── 5-main.c ├── 5-more_numbers ├── 5-more_numbers.c ├── 6-lines ├── 6-main.c ├── 6-print_line.c ├── 7-diagonals ├── 7-main.c ├── 7-print_diagonal.c ├── 8-main.c ├── 8-print_square.c ├── 8-squares ├── 9-fizz_buzz ├── 9-fizz_buzz.C ├── 9-fizz_buzz.c ├── README.md ├── _putchar.c ├── clear └── main.h ├── more_malloc_free ├── 0-main.c ├── 0-malloc_checked.c ├── 1-main.c ├── 1-string_nconcat ├── 1-string_nconcat.c ├── 2-calloc ├── 2-calloc.c ├── 2-main.c ├── 3-array_range ├── 3-array_range.c ├── 3-main.c ├── README.md ├── a └── main.h ├── pointers_arrays_strings ├── 0-98 ├── 0-memset.c ├── 0-reset_to_98.c ├── 0-strcat.c ├── 1-memcpy.c ├── 1-strncat.c ├── 1-swap.c ├── 100-atoi.c ├── 2-strchr.c ├── 2-strlen.c ├── 2-strncpy.c ├── 3-puts.c ├── 3-strcmp.c ├── 3-strspn.c ├── 4-print_rev.c ├── 4-rev_array.c ├── 4-strpbrk.c ├── 5-rev_string.c ├── 5-string_toupper.c ├── 5-strstr.c ├── 6-cap_string.c ├── 6-puts2.c ├── 7-leet.c ├── 7-print_chessboard.c ├── 7-puts_half.c ├── 8-print_array.c ├── 8-print_diagsums.c ├── 9-strcpy.c ├── README.md ├── _putchar.c ├── clear └── main.h ├── 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 ├── _putchar.c ├── a ├── b ├── c ├── d └── e ├── recursion ├── 0-main.c ├── 0-puts_recursion ├── 0-puts_recursion.c ├── 1-main.c ├── 1-print_rev_recursion ├── 1-print_rev_recursion.c ├── 100-is_palindrome.c ├── 100-main.c ├── 100-palindrome ├── 101-main.c ├── 101-wildcmp ├── 101-wildcmp.c ├── 2-main.c ├── 2-strlen_recursion ├── 2-strlen_recursion.c ├── 3-factorial ├── 3-factorial.c ├── 3-main.c ├── 4-main.c ├── 4-pow ├── 4-pow_recursion.c ├── 5-main.c ├── 5-sqrt ├── 5-sqrt_recursion.c ├── 6-is_prime_number.c ├── 6-main.c ├── 6-prime ├── README.md ├── _putchar.c └── main.h ├── search_algorithms ├── 0-linear.c ├── 1-binary.c ├── 2-O ├── 3-O ├── 4-O ├── 5-O ├── 6-O ├── README.md └── search_algos.h ├── singly_linked_lists ├── 0-print_list.c ├── 1-list_len.c ├── 2-add_node.c ├── 3-add_node_end.c ├── 4-free_list.c ├── README.md └── lists.h ├── static_libraries ├── 0-isupper.c ├── 0-isupper.o ├── 0-memset.c ├── 0-memset.o ├── 0-strcat.c ├── 0-strcat.o ├── 1-isdigit.c ├── 1-isdigit.o ├── 1-memcpy.c ├── 1-memcpy.o ├── 1-strncat.c ├── 1-strncat.o ├── 100-atoi.c ├── 100-atoi.o ├── 2-strchr.c ├── 2-strchr.o ├── 2-strlen.c ├── 2-strlen.o ├── 2-strncpy.c ├── 2-strncpy.o ├── 3-islower.c ├── 3-islower.o ├── 3-puts.c ├── 3-puts.o ├── 3-strcmp.c ├── 3-strcmp.o ├── 3-strspn.c ├── 3-strspn.o ├── 4-isalpha.c ├── 4-isalpha.o ├── 4-strpbrk.c ├── 4-strpbrk.o ├── 5-strstr.c ├── 5-strstr.o ├── 6-abs.c ├── 6-abs.o ├── 9-strcpy.c ├── 9-strcpy.o ├── README.md ├── _putchar.c ├── _putchar.o ├── create_static_lib.sh ├── liball.a ├── libmy.a ├── main.c ├── main.h ├── main.o └── quote ├── 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 ├── a ├── b ├── c ├── c* ├── d ├── dog.h ├── e └── f ├── variables_if_else_while ├── 0-positive_or_negative ├── 0-positive_or_negative.c ├── 1-last_digit ├── 1-last_digit.c ├── 2-print_alphabet ├── 2-print_alphabet.c ├── 3-print_alphabets.c ├── 4-print_alphabt ├── 4-print_alphabt.c ├── 5-print_numbers ├── 5-print_numbers.c ├── 6-print_numberz ├── 6-print_numberz.c ├── 7-print_tebahpla ├── 7-print_tebahpla.c ├── 8-print_base16 ├── 8-print_base16.c ├── 9-print_comb ├── 9-print_comb.c ├── README.md └── a.out └── variadic_functions ├── 0-sum_them_all.c ├── 1-print_numbers.c ├── 2-print_strings.c ├── 3-print_all.c ├── README.md ├── _putchar.c └── variadic_functions.h /README.md: -------------------------------------------------------------------------------- 1 | 2 | ![github readme lo level](https://github.com/chloe0524/holbertonschool-low_level_programming/assets/127857895/27b70110-5843-47f3-a2d3-2664921a25e3) 3 | 4 | # **holbertonschool-low_level_programming :star:** 5 | 6 | This repository contains various C :heart: projects completed while learning coding fundamentals during the first trimester at Holberton School Toulouse. These projects served as practice exercises with the goal of learning essential coding concepts and applying them to practical problems. 7 | 8 | #### Project List: 9 | 10 | 11 | | :arrow_up: | :arrow_down: | 12 | |-------------------------------|---------------------------------| 13 | | 1. ``hello_world`` | 2. ``argc_argv`` | 14 | | 3. ``bit_manipulation`` | 4. ``doubly_linked_lists`` | 15 | | 5. ``file_io`` | 6. ``function_pointers`` | 16 | | 7. ``functions_nested_loops`` | 8. ``hash_tables`` | 17 | | 9. ``makefiles`` | 10. ``malloc_free`` | 18 | | 11.more_functions_nested_loops`` | 12. ``more_malloc_free`` | 19 | | 13. ``pointers_arrays_strings`` | 14. ``preprocessor`` | 20 | | 15. ``recursion`` | 16. ``search_algorithms`` | 21 | | 17. ``singly_linked_lists`` | 18. ``static_libraries`` | 22 | | 19. ``structures_typedef`` | 20. ``variables_if_else_while`` | 23 | | 21. ``variadic_functions`` | | 24 | 25 | 26 | #### Skills Learned/Acquired :thumbsup: 27 | 28 | * C programming fundamentals 29 | * Data structures (linked lists, hash tables) 30 | * Memory management (malloc, free) 31 | * Functions and pointers 32 | * File I/O 33 | * Makefiles 34 | * Recursion 35 | * Search algorithms 36 | -------------------------------------------------------------------------------- /argc_argv/0-whatsmyname.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - entry point of the program 4 | * @argc: number of command-line arguments 5 | * @argv: array of strings containing the command-line arguments 6 | * 7 | * Return: 0 for successful execution. 8 | */ 9 | 10 | int main(int argc, char *argv[]) 11 | { 12 | int i; 13 | 14 | for (i = 0; i <= argc; i++) 15 | { 16 | printf("%s\n", argv[0]); 17 | break; 18 | } 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /argc_argv/1-args.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - entry point of the program 4 | * @argc: number of command-line arguments 5 | * @argv: array of strings containing the command-line arguments 6 | * 7 | * Return: 0 if success 8 | */ 9 | 10 | int main(int argc, char *argv[]) 11 | { 12 | (void)argv; 13 | 14 | printf("%d\n", argc - 1); 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /argc_argv/2-args.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - entry point of the program 4 | * @argc: number of command-line arguments 5 | * @argv: array of strings containing the command-line arguments 6 | * 7 | * Return: 0 for successful execution. 8 | */ 9 | 10 | int main(int argc, char *argv[]) 11 | { 12 | int i; 13 | 14 | for (i = 0; i < argc; i++) 15 | { 16 | printf("%s\n", argv[i]); 17 | } 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /argc_argv/3-mul.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main - entry point of the program 5 | * @argc: number of command-line arguments 6 | * @argv: array of strings containing the command-line arguments 7 | * 8 | * Return: 0 for successful execution. 9 | */ 10 | 11 | int main(int argc, char *argv[]) 12 | { 13 | int n, r, d; 14 | 15 | if (argc != 3) 16 | { 17 | printf("Error\n"); 18 | return (1); 19 | } 20 | 21 | n = atoi(argv[1]); 22 | r = atoi(argv[2]); 23 | d = n * r; 24 | 25 | printf("%i\n", d); 26 | 27 | return (0); 28 | } 29 | -------------------------------------------------------------------------------- /argc_argv/4-add.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main - entry point of the program 5 | * @argc: number of command-line arguments 6 | * @argv: array of strings containing the command-line arguments 7 | * 8 | * Return: 0 for successful execution 9 | */ 10 | int main(int argc, char *argv[]) 11 | { 12 | int d = 0; 13 | int i, j; 14 | char *c; 15 | 16 | if (argc < 2) 17 | { 18 | printf("0\n"); 19 | return (0); 20 | } 21 | 22 | for (i = 1; i < argc; i++) 23 | { 24 | for (c = argv[i]; *c; c++) 25 | { 26 | if (*c < '0' || *c > '9') 27 | { 28 | printf("Error\n"); 29 | return (1); 30 | } 31 | } 32 | j = atoi(argv[i]); 33 | if (j <= 0) 34 | { 35 | printf("Error\n"); 36 | return (1); 37 | } 38 | d += j; 39 | } 40 | 41 | printf("%d\n", d); 42 | return (0); 43 | } 44 | 45 | -------------------------------------------------------------------------------- /argc_argv/README.md: -------------------------------------------------------------------------------- 1 | # Tasks 2 | 3 | ![text (3)](https://github.com/chloe0524/holbertonschool-low_level_programming/assets/127857895/fafd2df8-ed47-46c6-bd71-f29a8e22ebdc) 4 | 5 | 1. [It ain't what they call you, it's what you answer to](#task-0-it-aint-what-they-call-you-its-what-you-answer-to) 6 | 2. [Silence is argument carried out by other means](#task-1-silence-is-argument-carried-out-by-other-means) 7 | 3. [The best argument against democracy is a five-minute conversation with the average voter](#task-2-the-best-argument-against-democracy-is-a-five-minute-conversation-with-the-average-voter) 8 | 4. [Neither irony nor sarcasm is argument](#task-3-neither-irony-nor-sarcasm-is-argument) 9 | 5. [To infinity and beyond](#task-4-to-infinity-and-beyond) 10 | 11 | ## Task 0: It ain't what they call you, it's what you answer to 12 | 13 | Mandatory 14 | 15 | Write a program that prints its name, followed by a new line. 16 | 17 | If you rename the program, it will print the new name, without having to compile it again. You should not remove the path before the name of the program. 18 | 19 | ## Task 1: Silence is argument carried out by other means 20 | 21 | Mandatory 22 | 23 | Write a program that prints the number of arguments passed into it. 24 | 25 | Your program should print a number, followed by a new line. 26 | 27 | ## Task 2: The best argument against democracy is a five-minute conversation with the average voter 28 | 29 | Mandatory 30 | 31 | Write a program that prints all arguments it receives. 32 | 33 | All arguments should be printed, including the first one. Only print one argument per line, ending with a new line. 34 | 35 | ## Task 3: Neither irony nor sarcasm is argument 36 | 37 | Mandatory 38 | 39 | Write a program that multiplies two numbers. 40 | 41 | Your program should print the result of the multiplication, followed by a new line. You can assume that the two numbers and result of the multiplication can be stored in an integer. If the program does not receive two arguments, your program should print Error, followed by a new line, and return 1. 42 | 43 | ## Task 4: To infinity and beyond 44 | 45 | Mandatory 46 | 47 | Write a program that adds positive numbers. 48 | 49 | Print the result, followed by a new line. If no number is passed to the program, print 0, followed by a new line. If one of the numbers contains symbols that are not digits, print Error, followed by a new line, and return 1. You can assume that numbers and the addition of all the numbers can be stored in an int. 50 | 51 | -------------------------------------------------------------------------------- /argc_argv/main.h: -------------------------------------------------------------------------------- 1 | int _putchar (char c); 2 | -------------------------------------------------------------------------------- /bit_manipulation/0-binary_to_uint.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * binary_to_uint - converts binary number to unsigned int 5 | * 6 | * @b: char string to convert 7 | * 8 | * Return: converted value as unsigned int, or 0 9 | */ 10 | 11 | unsigned int binary_to_uint(const char *b) 12 | { 13 | unsigned int pain = 0; 14 | 15 | if (!b) 16 | return (0); 17 | 18 | 19 | for (b = b ; *b; b++) 20 | { 21 | if (*b == '1') 22 | pain = (pain << 1) + 1; 23 | else if (*b == '0') 24 | pain = pain << 1; 25 | else 26 | return (0); 27 | } 28 | 29 | return (pain); 30 | } 31 | -------------------------------------------------------------------------------- /bit_manipulation/1-print_binary.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_binary - prints binary representation of number 5 | * @n: number to convert and print 6 | */ 7 | 8 | void print_binary(unsigned long int n) 9 | { 10 | unsigned long int the_bit = 1UL << 63; 11 | char ici = '0'; 12 | 13 | while (!(the_bit & n) && the_bit != 0) 14 | the_bit = the_bit >> 1; 15 | 16 | if (the_bit == 0) 17 | { 18 | _putchar(ici); 19 | return; 20 | } 21 | 22 | while (the_bit) 23 | { 24 | if (the_bit & n) 25 | ici = '1'; 26 | else 27 | 28 | ici = '0'; 29 | _putchar(ici); 30 | the_bit = the_bit >> 1; 31 | 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /bit_manipulation/2-get_bit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | 4 | /** 5 | * get_bit - gets value of a bit at an index 6 | * 7 | * @n: number to extract the bit from 8 | * @index: index of the bit to retrieve 9 | * 10 | * Return: value of the bit -1 11 | */ 12 | 13 | int get_bit(unsigned long int n, unsigned int index) 14 | { 15 | unsigned long int mask; 16 | 17 | if (index > 63) 18 | return (-1); 19 | 20 | mask = 1UL << index; 21 | return ((n & mask) > 0); 22 | } 23 | 24 | -------------------------------------------------------------------------------- /bit_manipulation/3-set_bit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * set_bit - sets the value of a bit at a given index to 1 5 | * 6 | * @n: pointer to the number to modify 7 | * @index: index of the bit to set 8 | * 9 | * Return: 1 if successful, or -1 on error 10 | */ 11 | 12 | int set_bit(unsigned long int *n, unsigned int index) 13 | { 14 | unsigned long int mask; 15 | 16 | if (index > 63) 17 | return (-1); 18 | 19 | mask = 1UL << index; 20 | *n = *n | mask; 21 | 22 | return (1); 23 | } 24 | 25 | -------------------------------------------------------------------------------- /bit_manipulation/4-clear_bit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * clear_bit - Clears the value of a bit at a given index to 0. 5 | * 6 | * @n: Pointer to the number to modify. 7 | * @index: Index of the bit to clear. 8 | * 9 | * Return: 1 if successful, or -1 on error. 10 | */ 11 | int clear_bit(unsigned long int *n, unsigned int index) 12 | { 13 | unsigned long int mask; 14 | 15 | if (index > 63) 16 | return (-1); 17 | 18 | mask = 1UL << index; 19 | *n = *n & (~mask); 20 | 21 | return (1); 22 | } 23 | 24 | -------------------------------------------------------------------------------- /bit_manipulation/5-flip_bits.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * flip_bits - returns num of bits flip to get n to m 5 | * 6 | * @n: first number 7 | * @m: second number 8 | * 9 | * Return: number of bits to flip 10 | */ 11 | unsigned int flip_bits(unsigned long int n, unsigned long int m) 12 | { 13 | unsigned int ctr = 0; 14 | unsigned long int xor = n ^ m; 15 | 16 | while (xor > 0) 17 | { 18 | ctr += xor & 1; 19 | xor >>= 1; 20 | } 21 | 22 | return (ctr); 23 | } 24 | 25 | -------------------------------------------------------------------------------- /bit_manipulation/README.md: -------------------------------------------------------------------------------- 1 | ![ici_pexels-tibe-de-kort-9951077](https://github.com/chloe0524/holbertonschool-low_level_programming/assets/127857895/abf77bf5-5e14-45f1-8c5b-22cd5b55a74a) 2 | 3 | # :zero: C - Bit manipulation :one: 4 | 5 | - [Task 0.0](#task-00) 6 | - [Task 1.1](#task-11) 7 | - [Task 2.10](#task-210) 8 | - [Task 3.11](#task-311) 9 | - [Task 4.100](#task-4100) 10 | - [Task 5.101](#task-5101) 11 | 12 | ## Task 0.0 13 | 14 | **Mandatory** 15 | 16 | Write a function that converts a binary number to an unsigned int. 17 | 18 | - **Prototype:** `unsigned int binary_to_uint(const char *b);` 19 | - `b` is pointing to a string of 0 and 1 chars. 20 | - **Return:** the converted number, or 0 if there is one or more chars in the string `b` that is not 0 or 1. 21 | - `b` is NULL. 22 | 23 | ## Task 1.1 24 | 25 | **Mandatory** 26 | 27 | Write a function that prints the binary representation of a number. 28 | 29 | - **Prototype:** `void print_binary(unsigned long int n);` 30 | - **Format:** see example. 31 | - You are not allowed to use arrays. 32 | - You are not allowed to use malloc. 33 | - You are not allowed to use the % or / operators. 34 | 35 | ## Task 2.10 36 | 37 | **Mandatory** 38 | 39 | Write a function that returns the value of a bit at a given index. 40 | 41 | - **Prototype:** `int get_bit(unsigned long int n, unsigned int index);` 42 | - `index` is the index, starting from 0, of the bit you want to get. 43 | - **Returns:** the value of the bit at index `index` or -1 if an error occurred. 44 | 45 | ## Task 3.11 46 | 47 | **Mandatory** 48 | 49 | Write a function that sets the value of a bit to 1 at a given index. 50 | 51 | - **Prototype:** `int set_bit(unsigned long int *n, unsigned int index);` 52 | - `index` is the index, starting from 0, of the bit you want to set. 53 | - **Returns:** 1 if it worked, or -1 if an error occurred. 54 | 55 | ## Task 4.100 56 | 57 | **Mandatory** 58 | 59 | Write a function that sets the value of a bit to 0 at a given index. 60 | 61 | - **Prototype:** `int clear_bit(unsigned long int *n, unsigned int index);` 62 | - `index` is the index, starting from 0, of the bit you want to set. 63 | - **Returns:** 1 if it worked, or -1 if an error occurred. 64 | 65 | ## Task 5.101 66 | 67 | **Mandatory** 68 | 69 | Write a function that returns the number of bits you would need to flip to get from one number to another. 70 | 71 | - **Prototype:** `unsigned int flip_bits(unsigned long int n, unsigned long int m);` 72 | - You are not allowed to use the % or / operators. 73 | 74 | -------------------------------------------------------------------------------- /bit_manipulation/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_HEADERS_H 2 | #define MAIN_HEADERS_H 3 | 4 | int _putchar(char c); 5 | 6 | unsigned int binary_to_uint(const char *b); 7 | void print_binary(unsigned long int n); 8 | int get_bit(unsigned long int n, unsigned int index); 9 | int set_bit(unsigned long int *n, unsigned int index); 10 | int clear_bit(unsigned long int *n, unsigned int index); 11 | unsigned int flip_bits(unsigned long int n, unsigned long int m); 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /doubly_linked_lists/0-print_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * print_dlistint - prints all elements of list dlistint_t 5 | * @h: pointer to head of list. 6 | * 7 | * Return: number of nodes 8 | * 9 | */ 10 | 11 | size_t print_dlistint(const dlistint_t *h) 12 | { 13 | size_t i; 14 | 15 | for (i = 0; h != NULL; i++) 16 | { 17 | printf("%d\n", h->n); 18 | h = h->next; 19 | } 20 | 21 | return (i); 22 | } 23 | 24 | -------------------------------------------------------------------------------- /doubly_linked_lists/1-dlistint_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * dlistint_len - prints number of elements dlistint_t 5 | * @h: pointer to head of list. 6 | * 7 | * Return: number of elements 8 | */ 9 | size_t dlistint_len(const dlistint_t *h) 10 | { 11 | size_t i; 12 | 13 | for (i = 0; h != NULL; i++) 14 | { 15 | h = h->next; 16 | } 17 | 18 | return (i); 19 | } 20 | -------------------------------------------------------------------------------- /doubly_linked_lists/2-add_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_dnodeint - adds new node at the beginning of dlistint_t list 5 | * @head: pointer to a pointer to head of list 6 | * @n: integer value in the new node 7 | * 8 | * Return: address of new element, or NULL if it failed 9 | */ 10 | 11 | dlistint_t *add_dnodeint(dlistint_t **head, const int n) 12 | { 13 | dlistint_t *plus_node; 14 | 15 | plus_node = malloc(sizeof(dlistint_t)); 16 | if (plus_node == NULL) 17 | return (NULL); 18 | 19 | plus_node->n = n; 20 | plus_node->prev = NULL; 21 | plus_node->next = *head; 22 | 23 | if (*head != NULL) 24 | (*head)->prev = plus_node; 25 | 26 | *head = plus_node; 27 | 28 | return (plus_node); 29 | } 30 | -------------------------------------------------------------------------------- /doubly_linked_lists/3-add_dnodeint_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_dnodeint_end - adds new node at the end of dlistint_t list 5 | * @head: pointer to a pointer to head of the list 6 | * @n: integer value in the new node 7 | * 8 | * Return: address of the new element, or NULL if it failed 9 | */ 10 | 11 | dlistint_t *add_dnodeint_end(dlistint_t **head, const int n) 12 | { 13 | dlistint_t *other_node; 14 | dlistint_t *end_node; 15 | 16 | other_node = malloc(sizeof(dlistint_t)); 17 | if (other_node == NULL) 18 | return (NULL); 19 | 20 | other_node->n = n; 21 | other_node->next = NULL; 22 | other_node->prev = NULL; 23 | 24 | if (*head != NULL) 25 | { 26 | end_node = *head; 27 | while (end_node->next != NULL) 28 | { 29 | end_node = end_node->next; 30 | } 31 | end_node->next = other_node; 32 | other_node->prev = end_node; 33 | } 34 | else 35 | { 36 | *head = other_node; 37 | } 38 | 39 | return (other_node); 40 | } 41 | 42 | -------------------------------------------------------------------------------- /doubly_linked_lists/4-free_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_dlistint - frees memory occupied by list 5 | * @head: pointer to head of linked list. 6 | */ 7 | void free_dlistint(dlistint_t *head) 8 | { 9 | if (head) 10 | { 11 | dlistint_t *next_node = head->next; 12 | dlistint_t *prev_node = head->prev; 13 | 14 | if (prev_node) 15 | prev_node->next = next_node; 16 | if (next_node) 17 | next_node->prev = prev_node; 18 | 19 | free_dlistint(next_node); 20 | free_dlistint(prev_node); 21 | free(head); 22 | } 23 | } 24 | 25 | -------------------------------------------------------------------------------- /doubly_linked_lists/5-get_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * get_dnodeint_at_index - get node at index in doubly linked list 5 | * @head: pointer to list head 6 | * @index: Index of node to get (start = 0) 7 | * 8 | * Return: Node at index, or NULL 9 | */ 10 | 11 | dlistint_t *get_dnodeint_at_index(dlistint_t *head, unsigned int index) 12 | { 13 | unsigned int ctr = 0; 14 | 15 | while (head && ctr < index) 16 | { 17 | head = head->next; 18 | ctr++; 19 | } 20 | 21 | if (ctr == index) 22 | { 23 | return (head); 24 | } 25 | else 26 | { 27 | return (NULL); 28 | } 29 | } 30 | 31 | -------------------------------------------------------------------------------- /doubly_linked_lists/6-sum_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * sum_dlistint - calculate the sum of elements in a doubly linked list 5 | * @head: pointer to list head 6 | * 7 | * Return: Sum of elements 8 | */ 9 | int sum_dlistint(dlistint_t *head) 10 | { 11 | int ctr = 0; 12 | 13 | while (head != NULL) 14 | { 15 | ctr += head->n; 16 | head = head->next; 17 | } 18 | 19 | return (ctr); 20 | } 21 | 22 | 23 | -------------------------------------------------------------------------------- /doubly_linked_lists/7-insert_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * insert_dnodeint_at_index - Inserts new node at given position in list 5 | * @h: Pointer to head of list 6 | * @idx: Index 7 | * @n: Integer value for new node 8 | * 9 | * Return: Address of the new node, or NULL if it failed. 10 | */ 11 | 12 | dlistint_t *insert_dnodeint_at_index(dlistint_t **h, unsigned int idx, int n) 13 | { 14 | dlistint_t *n_node, *ctr; 15 | unsigned int index; 16 | 17 | if (!h || (idx && *h == NULL)) 18 | return (NULL); 19 | 20 | ctr = *h; 21 | 22 | n_node = malloc(sizeof(dlistint_t)); 23 | if (!n_node) 24 | return (NULL); 25 | 26 | n_node->n = n; 27 | 28 | if (!idx) 29 | { 30 | n_node->prev = NULL; 31 | n_node->next = ctr; 32 | if (ctr) 33 | ctr->prev = n_node; 34 | *h = n_node; 35 | return (n_node); 36 | } 37 | 38 | for (index = 0; index < (idx - 1) && (ctr->next); index++) 39 | ctr = ctr->next; 40 | 41 | if (index < (idx - 1)) 42 | { 43 | free(n_node); 44 | return (NULL); 45 | } 46 | 47 | n_node->prev = ctr; 48 | if (ctr->next) 49 | (ctr->next)->prev = n_node; 50 | n_node->next = ctr->next; 51 | ctr->next = n_node; 52 | 53 | return (n_node); 54 | } 55 | -------------------------------------------------------------------------------- /doubly_linked_lists/8-delete_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * delete_dnodeint_at_index - Deletes the node at index of list. 5 | * @head: Pointer to the head of list. 6 | * @index: Position of node to delete. 7 | * 8 | * Return: 1 for success or -1 9 | **/ 10 | 11 | int delete_dnodeint_at_index(dlistint_t **head, unsigned int index) 12 | { 13 | dlistint_t *node_bye, *plus_node = *head; 14 | 15 | if (!*head) 16 | return (-1); 17 | 18 | if (index == 0) 19 | { 20 | *head = (*head)->next; 21 | free(plus_node); 22 | if (*head) 23 | (*head)->prev = NULL; 24 | return (1); 25 | } 26 | 27 | for (; index > 0 && plus_node; index--) 28 | plus_node = plus_node->next; 29 | 30 | if (index != 0) 31 | return (-1); 32 | 33 | node_bye = plus_node; 34 | if (plus_node->prev) 35 | plus_node->prev->next = plus_node->next; 36 | if (plus_node->next) 37 | plus_node->next->prev = plus_node->prev; 38 | 39 | free(node_bye); 40 | return (1); 41 | } 42 | 43 | -------------------------------------------------------------------------------- /doubly_linked_lists/README.md: -------------------------------------------------------------------------------- 1 | # :spiral_notepad: C- Doubly linked lists 2 | ![pain-all-i-f9fbbce4bd](https://github.com/chloe0524/holbertonschool-low_level_programming/assets/127857895/978fa947-e42f-4f75-96c8-c1303d726c29) 3 | 4 | - [0. Print list](#0-print-list-mandatory) 5 | - [1. List length](#1-list-length-mandatory) 6 | - [2. Add node](#2-add-node-mandatory) 7 | - [3. Add node at the end](#3-add-node-at-the-end-mandatory) 8 | - [4. Free list](#4-free-list-mandatory) 9 | - [5. Get node at index](#5-get-node-at-index-mandatory) 10 | - [6. Sum list](#6-sum-list-mandatory) 11 | - [7. Insert at index](#7-insert-at-index-mandatory) 12 | - [8. Delete at index](#8-delete-at-index-mandatory) 13 | 14 | --- 15 | 16 | ## 0. Print list (mandatory) 17 | 18 | Write a function that prints all the elements of a `dlistint_t` list. 19 | 20 | **Prototype:** `size_t print_dlistint(const dlistint_t *h);` 21 | 22 | **Return:** the number of nodes 23 | 24 | **Format:** see example 25 | 26 | --- 27 | 28 | ## 1. List length (mandatory) 29 | 30 | Write a function that returns the number of elements in a linked `dlistint_t` list. 31 | 32 | **Prototype:** `size_t dlistint_len(const dlistint_t *h);` 33 | 34 | --- 35 | 36 | ## 2. Add node (mandatory) 37 | 38 | Write a function that adds a new node at the beginning of a `dlistint_t` list. 39 | 40 | **Prototype:** `dlistint_t *add_dnodeint(dlistint_t **head, const int n);` 41 | 42 | **Return:** the address of the new element, or NULL if it failed 43 | 44 | --- 45 | 46 | ## 3. Add node at the end (mandatory) 47 | 48 | Write a function that adds a new node at the end of a `dlistint_t` list. 49 | 50 | **Prototype:** `dlistint_t *add_dnodeint_end(dlistint_t **head, const int n);` 51 | 52 | **Return:** the address of the new element, or NULL if it failed 53 | 54 | --- 55 | 56 | ## 4. Free list (mandatory) 57 | 58 | Write a function that frees a `dlistint_t` list. 59 | 60 | **Prototype:** `void free_dlistint(dlistint_t *head);` 61 | 62 | --- 63 | 64 | ## 5. Get node at index (mandatory) 65 | 66 | Write a function that returns the nth node of a `dlistint_t` linked list. 67 | 68 | **Prototype:** `dlistint_t *get_dnodeint_at_index(dlistint_t *head, unsigned int index);` 69 | 70 | - where index is the index of the node, starting from 0 71 | - if the node does not exist, return NULL 72 | 73 | --- 74 | 75 | ## 6. Sum list (mandatory) 76 | 77 | Write a function that returns the sum of all the data (n) of a `dlistint_t` linked list. 78 | 79 | **Prototype:** `int sum_dlistint(dlistint_t *head);` 80 | 81 | - if the list is empty, return 0 82 | 83 | --- 84 | 85 | ## 7. Insert at index (mandatory) 86 | 87 | Write a function that inserts a new node at a given position. 88 | 89 | **Prototype:** `dlistint_t *insert_dnodeint_at_index(dlistint_t **h, unsigned int idx, int n);` 90 | 91 | - where idx is the index of the list where the new node should be added. Index starts at 0 92 | - Returns: the address of the new node, or NULL if it failed 93 | - if it is not possible to add the new node at index idx, do not add the new node and return NULL 94 | - your files `2-add_dnodeint.c` and `3-add_dnodeint_end.c` will be compiled during the correction 95 | 96 | --- 97 | 98 | ## 8. Delete at index (mandatory) 99 | 100 | Write a function that deletes the node at index index of a `dlistint_t` linked list. 101 | 102 | **Prototype:** `int delete_dnodeint_at_index(dlistint_t **head, unsigned int index);` 103 | 104 | - where index is the index of the node that should be deleted. 105 | - Index starts at 0 106 | - Returns: 1 if it succeeded, -1 if it failed 107 | 108 | -------------------------------------------------------------------------------- /doubly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef LISTS_HEADER_H 2 | #define LISTS_HEADER_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include "lists.h" 9 | 10 | /** 11 | * struct dlistint_s - doubly linked list 12 | * @n: integer 13 | * @prev: points to the previous node 14 | * @next: points to the next node 15 | * 16 | * Description: doubly linked list node structure 17 | * 18 | */ 19 | typedef struct dlistint_s 20 | { 21 | int n; 22 | struct dlistint_s *prev; 23 | struct dlistint_s *next; 24 | } dlistint_t; 25 | 26 | int _putchar(char c); 27 | 28 | size_t print_dlistint(const dlistint_t *h); 29 | size_t dlistint_len(const dlistint_t *h); 30 | dlistint_t *add_dnodeint(dlistint_t **head, const int n); 31 | dlistint_t *add_dnodeint_end(dlistint_t **head, const int n); 32 | void free_dlistint(dlistint_t *head); 33 | dlistint_t *get_dnodeint_at_index(dlistint_t *head, unsigned int index); 34 | int sum_dlistint(dlistint_t *head); 35 | dlistint_t *insert_dnodeint_at_index(dlistint_t **h, unsigned int idx, int n); 36 | int delete_dnodeint_at_index(dlistint_t **head, unsigned int index); 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /file_io/0-read_textfile.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /** 6 | * read_textfile - prints text from a file 7 | * 8 | * @filename: name of the file 9 | * @letters: number of characters to read 10 | * 11 | * Return: actual number of letters read, 0 if end of file 12 | */ 13 | ssize_t read_textfile(const char *filename, size_t letters) 14 | { 15 | int file; 16 | ssize_t actual_number, bytes_read; 17 | char *buf; 18 | 19 | if (filename == NULL || letters == 0) 20 | return (0); 21 | buf = malloc(sizeof(char) * letters); 22 | if (buf == NULL) 23 | return (0); 24 | 25 | file = open(filename, O_RDONLY); 26 | if (file == -1) 27 | { 28 | free(buf); 29 | return (0); 30 | } 31 | actual_number = read(file, buf, letters); 32 | if (actual_number == -1) 33 | { 34 | free(buf); 35 | close(file); 36 | return (0); 37 | } 38 | 39 | bytes_read = write(STDOUT_FILENO, buf, actual_number); 40 | 41 | free(buf); 42 | close(file); 43 | if (bytes_read != actual_number) 44 | return (0); 45 | return (actual_number); 46 | } 47 | 48 | -------------------------------------------------------------------------------- /file_io/1-create_file.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * create_file - creates a file and writes text to it 5 | * @filename: the name of the file to be created 6 | * @text_content: the text to be written to the file 7 | * 8 | * Return: the number of bytes written to the file, or -1 on error 9 | * 10 | */ 11 | int create_file(const char *filename, char *text_content) 12 | { 13 | int fd; 14 | ssize_t text_size = 0; 15 | ssize_t write_status; 16 | 17 | if (filename == NULL) 18 | return (-1); 19 | 20 | fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0600); 21 | if (fd == -1) 22 | return (-1); 23 | 24 | if (text_content != NULL) 25 | { 26 | while (text_content[text_size] != '\0') 27 | text_size++; 28 | 29 | write_status = write(fd, text_content, text_size); 30 | 31 | if (write_status == -1 || write_status != text_size) 32 | { 33 | close(fd); 34 | return (-1); 35 | } 36 | } 37 | 38 | close(fd); 39 | return (1); 40 | } 41 | 42 | -------------------------------------------------------------------------------- /file_io/2-append_text_to_file.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * append_text_to_file - appends text to a file 5 | * @filename: the name of the file to be created or appended to 6 | * @text_content: the text to be written to the file 7 | * 8 | * Return: 1 on success, -1 on failure 9 | */ 10 | int append_text_to_file(const char *filename, char *text_content) 11 | { 12 | int fd; 13 | ssize_t text_size = 0; 14 | ssize_t write_status; 15 | 16 | if (filename == NULL) 17 | return (-1); 18 | 19 | fd = open(filename, O_CREAT | O_WRONLY | O_APPEND, 0600); 20 | if (fd == -1) 21 | return (-1); 22 | 23 | if (text_content != NULL) 24 | { 25 | while (text_content[text_size] != '\0') 26 | text_size++; 27 | 28 | write_status = write(fd, text_content, text_size); 29 | 30 | if (write_status == -1 || write_status != text_size) 31 | { 32 | close(fd); 33 | return (-1); 34 | } 35 | } 36 | 37 | close(fd); 38 | return (1); 39 | } 40 | 41 | -------------------------------------------------------------------------------- /file_io/3-cp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * close_andprint - closes a file descriptor and prints 5 | * an error message if it fails 6 | * 7 | * @fd: file descriptor to close 8 | * 9 | * Return: 0 on success, 100 on failure 10 | */ 11 | int close_andprint(int fd) 12 | { 13 | int oops; 14 | 15 | oops = close(fd); 16 | if (oops == -1) 17 | { 18 | dprintf(STDERR_FILENO, "Oops: Can't close fd %d\n", fd); 19 | return (100); 20 | } 21 | return (0); 22 | } 23 | 24 | /** 25 | * writing_oops - error handler for a write error 26 | * 27 | * @fd1: first descriptor to close 28 | * @fd_go: second descriptor to close 29 | * @filename: filename prompting the error 30 | * 31 | * Return: 99 32 | */ 33 | int writing_oops(int fd1, int fd_go, char *filename) 34 | { 35 | dprintf(STDERR_FILENO, "Oops: Can't write to %s\n", filename); 36 | close_andprint(fd1); 37 | close_andprint(fd_go); 38 | return (99); 39 | } 40 | 41 | /** 42 | * reading_oops - error handler for a read error 43 | * 44 | * @fd1: first descriptor to close 45 | * @fd_go: second descriptor to close 46 | * @filename: filename prompting the error 47 | * 48 | * Return: 98 49 | */ 50 | int reading_oops(int fd1, int fd_go, char *filename) 51 | { 52 | dprintf(STDERR_FILENO, "Oops: Can't read from file %s\n", filename); 53 | close_andprint(fd1); 54 | close_andprint(fd_go); 55 | return (98); 56 | } 57 | 58 | /** 59 | * error_close - close file descriptors 60 | * 61 | * @fd_back: source file descriptor 62 | * @fd_go: destination file descriptor 63 | * 64 | * Return: 0 on success, or 100 65 | */ 66 | int error_close(int fd_back, int fd_go) 67 | { 68 | if ((close_andprint(fd_back) == 100) || (close_andprint(fd_go) == 100)) 69 | { 70 | dprintf(STDERR_FILENO, "Oops: Can't close file descriptors\n"); 71 | return (100); 72 | } 73 | 74 | return (0); 75 | } 76 | 77 | 78 | /** 79 | * main - copy one file to another, new file with perms 664 80 | * usage - cp file_from file_to 81 | * 82 | * @ac: number of arg 83 | * @av: list of args 84 | * 85 | * Return: 97 if incorrect num of args 86 | * 98 if file_from does not exist or unreadable 87 | * 99 if write fails 88 | * 100 if file close fails 89 | * 0 otherwise 90 | */ 91 | int main(int ac, char *av[]) 92 | { 93 | char buf[1024]; 94 | int read_len, write_len, file_back, file_go, error; 95 | 96 | if (ac != 3) 97 | { 98 | dprintf(STDERR_FILENO, "Usage: cp file_from file_to\n"); 99 | return (97); 100 | } 101 | file_back = open(av[1], O_RDONLY); 102 | if (file_back == -1) 103 | { 104 | dprintf(STDERR_FILENO, "Error: Can't read from file %s\n", av[1]); 105 | return (98); 106 | } 107 | file_go = open(av[2], O_WRONLY | O_CREAT | O_TRUNC, 108 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH); 109 | if (file_go == -1) 110 | { 111 | dprintf(STDERR_FILENO, "Error: Can't write to %s\n", av[2]); 112 | close_andprint(file_back); 113 | return (99); 114 | } 115 | do { 116 | read_len = read(file_back, buf, 1024); 117 | if (read_len == -1) 118 | return (reading_oops(file_back, file_go, av[1])); 119 | write_len = write(file_go, buf, read_len); 120 | if (write_len == -1 || write_len != read_len) 121 | return (writing_oops(file_back, file_go, av[2])); 122 | } while (read_len == 1024); 123 | error = error_close(file_back, file_go); 124 | if (error != 0) 125 | return (100); 126 | return (0); 127 | } 128 | -------------------------------------------------------------------------------- /file_io/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/file_io/README.md -------------------------------------------------------------------------------- /file_io/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_HEADER_FILE 2 | #define MAIN_HEADER_FILE 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | ssize_t read_textfile(const char *filename, size_t letters); 13 | int create_file(const char *filename, char *text_content); 14 | int append_text_to_file(const char *filename, char *text_content); 15 | int close_andprint(int fd); 16 | int writing_oops(int fd1, int fd_go, char *filename); 17 | int reading_oops(int fd1, int fd_go, char *filename); 18 | int error_close(int fd_back, int fd_go); 19 | 20 | #endif 21 | -------------------------------------------------------------------------------- /function_pointers/0-print_name.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "function_pointers.h" 3 | /** 4 | * print_name - Prints a name 5 | * @name: The name to prints 6 | * @f: Pointer to function 7 | * 8 | * Return: Nothing 9 | */ 10 | 11 | void print_name(char *name, void (*f)(char *)) 12 | { 13 | if (f != NULL) 14 | f(name); 15 | } 16 | -------------------------------------------------------------------------------- /function_pointers/1-array_iterator.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * array_iterator - function that executes a function given as a parameter 6 | * 7 | * @array: pointer to array 8 | * @size: size array 9 | * @action: pointer to function 10 | * 11 | * Return: void 12 | */ 13 | 14 | void array_iterator(int *array, size_t size, void (*action)(int)) 15 | { 16 | size_t i; 17 | 18 | if (size < 1) 19 | return; 20 | if (array == NULL || action == NULL) 21 | return; 22 | 23 | 24 | for (i = 0; i < size; i++) 25 | { 26 | (*action)(array[i]); 27 | } 28 | } 29 | 30 | 31 | -------------------------------------------------------------------------------- /function_pointers/2-int_index.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * int_index - function that searches for an integer 6 | * 7 | * @size: look for size 8 | * @cmp: pointer 9 | * @array: pointer to array 10 | * 11 | * Return: integer or -1 12 | * 13 | */ 14 | 15 | int int_index(int *array, int size, int (*cmp)(int)) 16 | { 17 | int i; 18 | 19 | if (array == NULL || cmp == NULL) 20 | return (-1); 21 | 22 | if (size <= 0) 23 | return (-1); 24 | 25 | for (i = 0; i < size; i++) 26 | { 27 | if ((*cmp)(array[i])) 28 | return (i); 29 | } 30 | return (-1); 31 | } 32 | 33 | 34 | -------------------------------------------------------------------------------- /function_pointers/3-calc.h: -------------------------------------------------------------------------------- 1 | #ifndef CALC_H 2 | #define CALC_H 3 | /** 4 | * struct op - structure op 5 | * @op:operator 6 | * @f: function 7 | */ 8 | typedef struct op 9 | { 10 | char *op; 11 | int (*f)(int a, int b); 12 | } op_t; 13 | 14 | int op_add(int a, int b); 15 | int op_sub(int a, int b); 16 | int op_mul(int a, int b); 17 | int op_div(int a, int b); 18 | int op_mod(int a, int b); 19 | 20 | int (*get_op_func(char *s))(int, int); 21 | 22 | #endif 23 | -------------------------------------------------------------------------------- /function_pointers/3-copy_main.c: -------------------------------------------------------------------------------- 1 | #include "3-calc.h" 2 | #include 3 | #include 4 | 5 | #define DEBUG_CC 1 6 | 7 | /** 8 | * main - calculates two numbers given a string 9 | * 10 | * @ac: number of arguments 11 | * @av: array of argument strings 12 | * 13 | * Return: 0 on success. 14 | */ 15 | int main(int ac, char *av[]) 16 | { 17 | int a; 18 | int b; 19 | int rslt; 20 | int (*f)(int, int); 21 | 22 | #if DEBUG_CC 23 | 24 | int i; 25 | 26 | printf("argc = %d\n", ac); 27 | 28 | for (i = 0; i < ac; i++) 29 | { 30 | printf("argv[%d] = %s\n", i, av[i]); 31 | } 32 | #endif /*DEBUG_CC*/ 33 | 34 | if (ac != 4) 35 | { 36 | printf("Error\n"); 37 | return (98); 38 | } 39 | 40 | a = atoi(av[1]); 41 | b = atoi(av[3]); 42 | f = get_op_func(av[2]); 43 | 44 | if (f == NULL || av[2][1] != '\0') 45 | { 46 | printf("Error\n"); 47 | return (99); 48 | } 49 | 50 | if ((av[2][0] == '/' || av[2][0] == '%') && b == 0) 51 | { 52 | printf("Error\n"); 53 | return (100); 54 | } 55 | 56 | rslt = f(a, b); 57 | 58 | printf("%d\n", rslt); 59 | return (0); 60 | } 61 | -------------------------------------------------------------------------------- /function_pointers/3-get_op_func.c: -------------------------------------------------------------------------------- 1 | #include "3-calc.h" 2 | #include 3 | /** 4 | * get_op_func - choose function to perform op 5 | * 6 | * @s: operator as arg 7 | * Return: pointer function 8 | */ 9 | 10 | int (*get_op_func(char *s))(int, int) 11 | { 12 | op_t ops[] = { 13 | {"+", op_add}, 14 | {"-", op_sub}, 15 | {"*", op_mul}, 16 | {"/", op_div}, 17 | {"%", op_mod}, 18 | {NULL, NULL} 19 | }; 20 | 21 | int i = 0; 22 | 23 | while (ops[i].op != NULL && *(ops[i].op) != *s) 24 | i++; 25 | 26 | return (ops[i].f); 27 | } 28 | -------------------------------------------------------------------------------- /function_pointers/3-main.c: -------------------------------------------------------------------------------- 1 | #include "3-calc.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * main - calculates two numbers given a string 7 | * 8 | * @argc: number of arguments 9 | * @argv: array of argument strings 10 | * 11 | * Return: 0 on success. 12 | */ 13 | 14 | int main(int argc, char *argv[]) 15 | { 16 | int a, b; 17 | int rslt; 18 | int (*f)(int, int); 19 | 20 | if (argc != 4) 21 | { 22 | printf("Error\n"); 23 | return (98); 24 | } 25 | 26 | a = atoi(argv[1]); 27 | b = atoi(argv[3]); 28 | f = get_op_func(argv[2]); 29 | 30 | if (f == NULL || argv[2][1] != '\0') 31 | { 32 | printf("Error\n"); 33 | return (99); 34 | } 35 | 36 | if ((*argv[2] == '/' || *argv[2] == '%') && b == 0) 37 | { 38 | printf("Error\n"); 39 | return (100); 40 | } 41 | 42 | rslt = f(a, b); 43 | 44 | printf("%d\n", rslt); 45 | return (0); 46 | } 47 | -------------------------------------------------------------------------------- /function_pointers/3-op_functions.c: -------------------------------------------------------------------------------- 1 | #include "3-calc.h" 2 | 3 | int op_add(int a, int b); 4 | int op_sub(int a, int b); 5 | int op_mul(int a, int b); 6 | int op_div(int a, int b); 7 | int op_mod(int a, int b); 8 | 9 | /** 10 | * op_add - Returns sum of two numbers 11 | * @a: First number 12 | * @b: Second number 13 | * 14 | * Return: Sum of a and b 15 | */ 16 | int op_add(int a, int b) 17 | { 18 | return (a + b); 19 | } 20 | 21 | /** 22 | * op_sub - Returns difference of two numbers 23 | * @a: First number 24 | * @b: Second number 25 | * 26 | * Return: Difference of a and b 27 | */ 28 | int op_sub(int a, int b) 29 | { 30 | return (a - b); 31 | } 32 | 33 | /** 34 | * op_mul - Returns product of two numbers 35 | * @a: First number 36 | * @b: Second number 37 | * 38 | * Return: Product of a and b 39 | */ 40 | int op_mul(int a, int b) 41 | { 42 | return (a * b); 43 | } 44 | 45 | /** 46 | * op_div - Returns division of two numbers 47 | * @a: First number 48 | * @b: Second number 49 | * 50 | * Return: Quotient of a and b 51 | */ 52 | int op_div(int a, int b) 53 | { 54 | return (a / b); 55 | } 56 | 57 | /** 58 | * op_mod - Returns remainder of the division of two numbers 59 | * @a: First number 60 | * @b: Second number 61 | * 62 | * Return: Remainder of the division of a by b 63 | */ 64 | int op_mod(int a, int b) 65 | { 66 | return (a % b); 67 | } 68 | 69 | -------------------------------------------------------------------------------- /function_pointers/README.md: -------------------------------------------------------------------------------- 1 | # Tasks 2 | 3 | ![Qn5ZL](https://github.com/chloe0524/holbertonschool-low_level_programming/assets/127857895/2d78a1ec-946a-453f-a720-616785bb3d48) 4 | 5 | 1. [What's my name](#whats-my-name) 6 | 2. [If you spend too much time thinking about a thing, you'll never get it done](#if-you-spend-too-much-time-thinking-about-a-thing-youll-never-get-it-done) 7 | 3. [To hell with circumstances; I create opportunities](#to-hell-with-circumstances-i-create-opportunities) 8 | 4. [A goal is not always meant to be reached, it often serves simply as something to aim at](#a-goal-is-not-always-meant-to-be-reached-it-often-serves-simply-as-something-to-aim-at) 9 | 10 | ## What's my name 11 | 12 | **Mandatory** 13 | 14 | Write a function that prints a name. 15 | 16 | **Prototype:** `void print_name(char *name, void (*f)(char *));` 17 | 18 | ## If you spend too much time thinking about a thing, you'll never get it done 19 | 20 | **Mandatory** 21 | 22 | Write a function that executes a function given as a parameter on each element of an array. 23 | 24 | **Prototype:** `void array_iterator(int *array, size_t size, void (*action)(int));` where size is the size of the array and action is a pointer to the function you need to use 25 | 26 | ## To hell with circumstances; I create opportunities 27 | 28 | **Mandatory** 29 | 30 | Write a function that searches for an integer. 31 | 32 | **Prototype:** `int int_index(int *array, int size, int (*cmp)(int));` where size is the number of elements in the array array cmp is a pointer to the function to be used to compare values int_index returns the index of the first element for which the cmp function does not return 0 If no element matches, return -1 If size <= 0, return -1 33 | 34 | ## A goal is not always meant to be reached, it often serves simply as something to aim at 35 | 36 | **Mandatory** 37 | 38 | Write a program that performs simple operations. 39 | 40 | You are allowed to use the standard library 41 | Usage: `calc num1 operator num2` 42 | You can assume num1 and num2 are integers, so use the `atoi` function to convert them from the string input toint operator is one of the following: 43 | - `+`: addition 44 | - `-`: subtraction 45 | - `*`: multiplication 46 | - `/`: division 47 | - `%`: modulo The program prints the result of the operation, followed by a new line 48 | 49 | You can assume that the result of all operations can be stored in an int if the number of arguments is wrong, print `Error`, followed by a new line, and exit with the status 98 50 | If the operator is none of the above, print `Error`, followed by a new line, and exit with the status 99 51 | If the user tries to divide (`/` or `%`) by 0, print `Error`, followed by a new line, and exit with the status 100 **This task requires that you create four different files.** 52 | 53 | -------------------------------------------------------------------------------- /function_pointers/function_pointers.h: -------------------------------------------------------------------------------- 1 | #ifndef FUNC_POINTERS_H 2 | #define FUNC_POINTERS_H 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 | int _putchar(char c); 8 | 9 | #endif 10 | -------------------------------------------------------------------------------- /functions_nested_loops/0-putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always 0 (Success) 9 | */ 10 | int main(void) 11 | { 12 | int i; 13 | char chainput[] = "_putchar"; 14 | 15 | for (i = 0; i <= 7; i++) 16 | { 17 | _putchar(chainput[i]); 18 | } 19 | 20 | _putchar('\n'); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /functions_nested_loops/1-alphabet.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_alphabet - print alphabet 5 | * 6 | * Return: nothing 7 | */ 8 | 9 | void print_alphabet(void) 10 | { 11 | char alphabet; 12 | 13 | 14 | for (alphabet = 'a'; alphabet <= 'z'; alphabet++) 15 | { 16 | _putchar(alphabet); 17 | 18 | } 19 | _putchar('\n'); 20 | } 21 | -------------------------------------------------------------------------------- /functions_nested_loops/10-add.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * add - Adds two integers. 5 | * @number1: first integer to be added. 6 | * @number2: second integer to be added. 7 | * 8 | * Return: The result of the addition. 9 | */ 10 | int add(int number1, int number2) 11 | { 12 | return (number1 + number2); 13 | } 14 | -------------------------------------------------------------------------------- /functions_nested_loops/11-print_to_98.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | 5 | /** 6 | * print_to_98 - Prints all natural num from n to 98. 7 | * @n: The starting number. 8 | * 9 | * Return: nothing 10 | */ 11 | void print_to_98(int n) 12 | { 13 | int i; 14 | 15 | if (n <= 98) 16 | { 17 | for (i = n; i <= 98; i++) 18 | { 19 | printf("%d", i); 20 | if (i < 98) 21 | { 22 | printf(", "); 23 | } 24 | } 25 | } 26 | else 27 | { 28 | for (i = n; i >= 98; i--) 29 | { 30 | printf("%d", i); 31 | if (i > 98) 32 | { 33 | printf(", "); 34 | } 35 | } 36 | } 37 | printf("\n"); 38 | } 39 | -------------------------------------------------------------------------------- /functions_nested_loops/2-print_alphabet_x10.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_alphabet_x10 - print alphabet 5 | * 6 | * Return: nothing 7 | */ 8 | 9 | void print_alphabet_x10(void) 10 | { 11 | char alphabet; 12 | int i; 13 | 14 | for (i = 1; i <= 10; i++) 15 | { 16 | for (alphabet = 'a'; alphabet <= 'z'; alphabet++) 17 | { 18 | _putchar(alphabet); 19 | } 20 | _putchar('\n'); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /functions_nested_loops/3-islower.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _islower - checks if a character is lowercase 5 | * 6 | * @c: the character to check 7 | * 8 | * Return: 1 if c lowercase, return 0 otherwise 9 | */ 10 | 11 | int _islower(int c) 12 | { 13 | if (c >= 'a' && c <= 'z') 14 | { 15 | return (1); 16 | } 17 | else 18 | { 19 | return (0); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /functions_nested_loops/4-isalpha.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isalpha - checks if a character is a letter 5 | * 6 | * @c: the character to check 7 | * 8 | * Return: 1 if c is a letter, return 0 otherwise 9 | */ 10 | int _isalpha(int c) 11 | { 12 | if (c >= 'a' && c <= 'z') 13 | return (1); 14 | else if (c >= 'A' && c <= 'Z') 15 | return (1); 16 | else 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /functions_nested_loops/5-sign.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_sign - Print sign of number. 5 | * 6 | * @n: Number to check for sign. 7 | * 8 | * function checks and prints the sign symbol. 9 | * 10 | * @n: Integer to check for sign. 11 | * 12 | * Return: 1 if 'n' > 0, 0 if 'n' is 0, -1 if 'n' < 0. 13 | */ 14 | 15 | int print_sign(int n) 16 | { 17 | if (n > 0) 18 | { 19 | _putchar('+'); 20 | return (1); 21 | } 22 | else if (n == 0) 23 | { 24 | _putchar('0'); 25 | return (0); 26 | } 27 | else 28 | { 29 | _putchar('-'); 30 | return (-1); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /functions_nested_loops/6-abs.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _abs - computes the absolute value of an integer 5 | * 6 | * @n: the character to check 7 | * 8 | * Return: n or -n 9 | */ 10 | int _abs(int n) 11 | { 12 | if (n >= 0) 13 | return (n); 14 | return (-n); 15 | } 16 | -------------------------------------------------------------------------------- /functions_nested_loops/7-print_last_digit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_last_digit - print the last digit of a number 5 | *@n: integer with last digit printed 6 | * Return: lastdigit 7 | */ 8 | int print_last_digit(int n) 9 | { 10 | int lastdigit = n % 10; 11 | 12 | if (lastdigit < 0) 13 | { 14 | lastdigit = -lastdigit; 15 | } 16 | _putchar(lastdigit + '0'); 17 | 18 | return (lastdigit); 19 | } 20 | -------------------------------------------------------------------------------- /functions_nested_loops/8-24_hours.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * jack_bauer - prints every minute of the day of Jack Bauer 5 | * Return: nothing 6 | */ 7 | void jack_bauer(void) 8 | { 9 | char h1, h2, m1, m2; 10 | 11 | for (h1 = '0' ; h1 <= '2'; h1++) 12 | { 13 | for (h2 = '0' ; ((h1 < '2' && h2 <= '9') || (h1 == '2' && h2 <= '3')); h2++) 14 | { 15 | for (m1 = '0' ; m1 <= '5'; m1++) 16 | { 17 | for (m2 = '0' ; m2 <= '9'; m2++) 18 | { 19 | _putchar(h1); 20 | _putchar(h2); 21 | _putchar(':'); 22 | _putchar(m1); 23 | _putchar(m2); 24 | _putchar('\n'); 25 | } 26 | } 27 | } 28 | } 29 | } 30 | 31 | -------------------------------------------------------------------------------- /functions_nested_loops/9-times_table.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | 5 | /** 6 | * times_table - prints 9 times table, starting with 0 7 | * @i , @j to multiply 8 | * Return: nothing 9 | */ 10 | void times_table(void) 11 | { 12 | int i, j; 13 | 14 | for (i = 0 ; i <= 9 ; i++) 15 | { 16 | printf("%d, ", i * 0); 17 | for (j = 1 ; j <= 8 ; j++) 18 | { 19 | printf("%2d, ", i * j); 20 | } 21 | printf("%2d\n", i * j); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /functions_nested_loops/README.md: -------------------------------------------------------------------------------- 1 | # Tasks 2 | 3 | ![NestedLoopsDiagram300x500](https://github.com/chloe0524/holbertonschool-low_level_programming/assets/127857895/44629ce6-70de-4a49-89d5-dd5e37c35d78) 4 | 5 | 1. **[_putchar](#_putchar)** 6 | 2. **[Alphabet](#alphabet)** 7 | 3. **[10 x Alphabet](#10-x-alphabet)** 8 | 4. **[islower](#islower)** 9 | 5. **[isalpha](#isalpha)** 10 | 6. **[Sign](#sign)** 11 | 7. **[Absolute Value](#absolute-value)** 12 | 8. **[Last Digit](#last-digit)** 13 | 9. **[Jack Bauer](#jack-bauer)** 14 | 10. **[Times Table](#times-table)** 15 | 11. **[Add](#add)** 16 | 12. **[Print to 98](#print-to-98)** 17 | 18 | # Tasks 19 | 20 | 0. **_putchar** 21 | - Write a program that prints `_putchar`, followed by a new line. 22 | - Prototype: `void print_name(char *name, void (*f)(char *));` 23 | - Return: 0 24 | 25 | 1. **Alphabet** 26 | - Write a function that prints the alphabet in lowercase. 27 | - Prototype: `void print_alphabet(void);` 28 | - Constraints: Use `_putchar` twice. 29 | 30 | 2. **10 x Alphabet** 31 | - Write a function that prints the alphabet 10 times. 32 | - Prototype: `void print_alphabet_x10(void);` 33 | - Constraints: Use `_putchar` twice. 34 | 35 | 3. **islower** 36 | - Write a function that checks for a lowercase character. 37 | - Prototype: `int _islower(int c);` 38 | - Returns 1 if c is lowercase, 0 otherwise. 39 | 40 | 4. **isalpha** 41 | - Write a function that checks for an alphabetic character. 42 | - Prototype: `int _isalpha(int c);` 43 | - Returns 1 if c is a letter (lowercase or uppercase), 0 otherwise. 44 | 45 | 5. **Sign** 46 | - Write a function that prints the sign of a number. 47 | - Prototype: `int print_sign(int n);` 48 | - Returns 1 and prints '+' if n is greater than zero, 0 and prints '0' if n is zero, -1 and prints '-' if n is less than zero. 49 | 50 | 6. **Absolute Value** 51 | - Write a function that computes the absolute value of an integer. 52 | - Prototype: `int _abs(int);` 53 | 54 | 7. **Last Digit** 55 | - Write a function that prints the last digit of a number. 56 | - Prototype: `int print_last_digit(int);` 57 | - Returns the value of the last digit. 58 | 59 | 8. **Jack Bauer** 60 | - Write a function that prints every minute of the day of Jack Bauer. 61 | - Prototype: `void jack_bauer(void);` 62 | 63 | 9. **Times Table** 64 | - Write a function that prints the 9 times table. 65 | - Prototype: `void times_table(void);` 66 | 67 | 10. **Add** 68 | - Write a function that adds two integers and returns the result. 69 | - Prototype: `int add(int, int);` 70 | 71 | 11. **Print to 98** 72 | - Write a function that prints all natural numbers from n to 98. 73 | - Prototype: `void print_to_98(int n);` 74 | - Numbers separated by commas, in ascending order. 75 | 76 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /functions_nested_loops/main.h: -------------------------------------------------------------------------------- 1 | int _putchar(char c); 2 | 3 | void print_alphabet(void); 4 | 5 | void print_alphabet_x10(void); 6 | 7 | int _islower(int c); 8 | 9 | int _isalpha(int c); 10 | 11 | int print_sign(int n); 12 | 13 | int _abs(int); 14 | 15 | int print_last_digit(int); 16 | 17 | void jack_bauer(void); 18 | 19 | void times_table(void); 20 | 21 | int add(int, int); 22 | 23 | void print_to_98(int n); 24 | -------------------------------------------------------------------------------- /hash_tables/0-hash_table_create.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_create - create hash table 5 | * 6 | * @size: size of array 7 | * Return: return pointer or NUll 8 | * 9 | */ 10 | 11 | hash_table_t *hash_table_create(unsigned long int size) 12 | { 13 | hash_table_t *phash; 14 | 15 | phash = malloc(sizeof(hash_table_t)); 16 | if (phash == NULL) 17 | return (NULL); 18 | 19 | phash->size = size; 20 | 21 | phash->array = malloc(sizeof(hash_node_t *) * size); 22 | if (phash->array == NULL) 23 | return (NULL); 24 | 25 | return (phash); 26 | } 27 | -------------------------------------------------------------------------------- /hash_tables/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "hash_tables.h" 5 | 6 | /** 7 | * main - check the code for 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 | } 19 | -------------------------------------------------------------------------------- /hash_tables/1-djb2.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_djb2 - implement djb2 algorithm 5 | * 6 | * @str: array of characters 7 | * Return: value in hash 8 | */ 9 | 10 | unsigned long int hash_djb2(const unsigned char *str) 11 | { 12 | unsigned long int hash; 13 | int c; 14 | 15 | hash = 5381; 16 | while ((c = *str++)) 17 | { 18 | hash = ((hash << 5) + hash) + c; /* hash * 33 + c */ 19 | } 20 | return (hash); 21 | } 22 | -------------------------------------------------------------------------------- /hash_tables/2-key_index.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * key_index - gets index of a key in a hash table 5 | * @key: key hashed and indexed 6 | * @size: size of the hash table. 7 | * 8 | * Return: index placed wher key should be 9 | */ 10 | 11 | unsigned long int key_index(const unsigned char *key, unsigned long int size) 12 | { 13 | return (hash_djb2(key) % size); 14 | } 15 | -------------------------------------------------------------------------------- /hash_tables/3-hash_table_set.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_set - adds an element to the hash table 5 | * @ht: hash table to add/update the key/value to 6 | * @key: the key, cannot be an empty string 7 | * @value: the value associated with the key 8 | * 9 | * Return: 1 on success, 0 on failure 10 | */ 11 | int hash_table_set(hash_table_t *ht, const char *key, const char *value) 12 | { 13 | unsigned long int index; 14 | hash_node_t *after_node, *before_node; 15 | 16 | if (ht == NULL || key == NULL || *key == '\0' || value == NULL) 17 | return (0); 18 | 19 | index = key_index((const unsigned char *)key, ht->size); 20 | 21 | before_node = ht->array[index]; 22 | while (before_node != NULL) 23 | { 24 | if (strcmp(before_node->key, key) == 0) 25 | { 26 | free(before_node->value); 27 | before_node->value = strdup(value); 28 | if (before_node->value == NULL) 29 | return (0); 30 | return (1); 31 | } 32 | before_node = before_node->next; 33 | } 34 | 35 | after_node = malloc(sizeof(hash_node_t)); 36 | if (after_node == NULL) 37 | return (0); 38 | 39 | after_node->key = strdup(key); 40 | after_node->value = strdup(value); 41 | if (after_node->key == NULL || after_node->value == NULL) 42 | { 43 | free(after_node->key); 44 | free(after_node->value); 45 | free(after_node); 46 | return (0); 47 | } 48 | 49 | after_node->next = ht->array[index]; 50 | ht->array[index] = after_node; 51 | 52 | return (1); 53 | } 54 | 55 | -------------------------------------------------------------------------------- /hash_tables/4-hash_table_get.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | /** 3 | * hash_table_get - get value associated with a key in hash table 4 | * @ht: hash table 5 | * @key: key to search 6 | * 7 | * Return:value with key, or NULL 8 | */ 9 | char *hash_table_get(const hash_table_t *ht, const char *key) 10 | { 11 | unsigned long int index; 12 | hash_node_t *after_node; 13 | 14 | if (!ht || !key) 15 | return (NULL); 16 | 17 | index = key_index((const unsigned char *)key, ht->size); 18 | after_node = ht->array[index]; 19 | 20 | while (after_node) 21 | { 22 | if (strcmp(after_node->key, key) == 0) 23 | return (after_node->value); 24 | after_node = after_node->next; 25 | } 26 | 27 | return (NULL); 28 | } 29 | 30 | -------------------------------------------------------------------------------- /hash_tables/5-hash_table_print.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_print - Prints the key-value pairs of a hash table. 5 | * @ht: The hash table to print. 6 | */ 7 | 8 | void hash_table_print(const hash_table_t *ht) 9 | { 10 | unsigned long int i; 11 | hash_node_t *node; 12 | char *ma = ""; 13 | 14 | if (ht == NULL) 15 | { 16 | return; 17 | } 18 | else 19 | 20 | printf("{"); 21 | 22 | for (i = 0; i < ht->size; i++) 23 | { 24 | node = ht->array[i]; 25 | while (node) 26 | { 27 | printf("%s'%s': '%s'", ma, node->key, node->value); 28 | ma = ", "; 29 | node = node->next; 30 | } 31 | } 32 | 33 | printf("}\n"); 34 | } 35 | -------------------------------------------------------------------------------- /hash_tables/6-hash_table_delete.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_delete - Deletes a hash table 5 | * @ht: the hash table 6 | */ 7 | void hash_table_delete(hash_table_t *ht) 8 | { 9 | unsigned long int i; 10 | hash_node_t *current, *next; 11 | 12 | if (ht == NULL) 13 | return; 14 | 15 | for (i = 0; i < ht->size; i++) 16 | { 17 | current = ht->array[i]; 18 | while (current != NULL) 19 | { 20 | next = current->next; 21 | free(current->key); 22 | free(current->value); 23 | free(current); 24 | current = next; 25 | } 26 | } 27 | 28 | free(ht->array); 29 | free(ht); 30 | } 31 | -------------------------------------------------------------------------------- /hash_tables/README.md: -------------------------------------------------------------------------------- 1 | placeholder 2 | -------------------------------------------------------------------------------- /hash_tables/a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/hash_tables/a -------------------------------------------------------------------------------- /hash_tables/hash_tables.h: -------------------------------------------------------------------------------- 1 | #ifndef HEADER_HASH_H 2 | #define HEADER_HASH_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | /** 10 | * struct hash_node_s - Node of a hash table 11 | * 12 | * @key: The key, string 13 | * The key is unique in the HashTable 14 | * @value: The value corresponding to a key 15 | * @next: A pointer to the next node of the List 16 | */ 17 | typedef struct hash_node_s 18 | { 19 | char *key; 20 | char *value; 21 | struct hash_node_s *next; 22 | } hash_node_t; 23 | 24 | /** 25 | * struct hash_table_s - Hash table data structure 26 | * 27 | * @size: The size of the array 28 | * @array: An array of size @size 29 | * Each cell of this array is a pointer to the first node of a linked list, 30 | * because we want our HashTable to use a Chaining collision handling 31 | */ 32 | typedef struct hash_table_s 33 | { 34 | unsigned long int size; 35 | hash_node_t **array; 36 | } hash_table_t; 37 | 38 | hash_table_t *hash_table_create(unsigned long int size); 39 | unsigned long int hash_djb2(const unsigned char *str); 40 | unsigned long int key_index(const unsigned char *key, unsigned long int size); 41 | int hash_table_set(hash_table_t *ht, const char *key, const char *value); 42 | char *hash_table_get(const hash_table_t *ht, const char *key); 43 | void hash_table_print(const hash_table_t *ht); 44 | void hash_table_delete(hash_table_t *ht); 45 | #endif 46 | -------------------------------------------------------------------------------- /hello_world/0-preprocessor: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -E $CFILE -o c 3 | -------------------------------------------------------------------------------- /hello_world/1-compiler: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -c $CFILE 3 | -------------------------------------------------------------------------------- /hello_world/100-intel: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -S -masm=intel $CFILE 3 | -------------------------------------------------------------------------------- /hello_world/101-quote.c: -------------------------------------------------------------------------------- 1 | /* 2 | * File: 101-quote.c 3 | * by Chloe.c 4 | */ 5 | 6 | #include 7 | 8 | /** 9 | * main - Prints "and that piece of art is useful" - Dora Korpar, 2015-10-19", 10 | * followed by a new line, to the standard error. 11 | * 12 | * Return: Always 1. 13 | */ 14 | int main(void) 15 | { 16 | write(2, "and that piece of art is useful\" - Dora Korpar, 2015-10-19\n", 59); 17 | return (1); 18 | } 19 | -------------------------------------------------------------------------------- /hello_world/2-assembler: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -S $CFILE 3 | -------------------------------------------------------------------------------- /hello_world/3-name: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -c $CFILE -o cisfun 3 | -------------------------------------------------------------------------------- /hello_world/4-puts.c: -------------------------------------------------------------------------------- 1 | /* 2 | * file name: 4-puts.c 3 | * made by Chloe.c 4 | */ 5 | 6 | #include 7 | 8 | /** 9 | * main - Entry point 10 | * 11 | * Description: 'print sentence' 12 | * 13 | * Return: Always 0 (Success) 14 | */ 15 | 16 | int main(void) 17 | { 18 | puts("\"Programming is like building a multilingual puzzle"); 19 | 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /hello_world/5-printf.c: -------------------------------------------------------------------------------- 1 | /* 2 | * file name: 5-printf.c 3 | * made by Chloe.c 4 | */ 5 | 6 | #include 7 | 8 | /** 9 | * main - Entry point 10 | * 11 | * Description: 'printf' 12 | * 13 | * Return: Always 0 (Success) 14 | */ 15 | 16 | int main(void) 17 | { 18 | printf("with proper grammar, but the outcome is a piece of art,\n"); 19 | 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /hello_world/6-size.c: -------------------------------------------------------------------------------- 1 | /* 2 | * file name: 6-size.c 3 | * made by Chloe.c 4 | */ 5 | 6 | #include 7 | 8 | /** 9 | * main - Entry point 10 | * 11 | * Description: 'printf sizeof' 12 | * 13 | * Return: Always 0 (Success) 14 | */ 15 | int main(void) 16 | { 17 | printf("Size of a char: %zu byte(s)\n", sizeof(char)); 18 | printf("Size of an int: %zu byte(s)\n", sizeof(int)); 19 | printf("Size of a long int: %zu byte(s)\n", sizeof(long int)); 20 | printf("Size of a long long int: %zu byte(s)\n", sizeof(long long int)); 21 | printf("Size of a float: %zu byte(s)\n", sizeof(float)); 22 | 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /hello_world/README.md: -------------------------------------------------------------------------------- 1 | # holbertonschool-low_level_programming 2 | C 3 | -------------------------------------------------------------------------------- /hello_world/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/hello_world/a.out -------------------------------------------------------------------------------- /hello_world/c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/hello_world/c -------------------------------------------------------------------------------- /iacta: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | -------------------------------------------------------------------------------- /iacta.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | -------------------------------------------------------------------------------- /makefiles/0-Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc main.c school.c -o school 3 | -------------------------------------------------------------------------------- /makefiles/1-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | all: $(SRC) 4 | $(CC) $(SRC) -o school 5 | -------------------------------------------------------------------------------- /makefiles/100-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | OBJ = $(SRC:.c=.o) 4 | NAME = school 5 | CFLAGS = -Wall -Werror -Wextra -pedantic 6 | 7 | .PHONY: all clean oclean fclean re 8 | 9 | all: m.h $(OBJ) 10 | $(CC) $(OBJ) -o $(NAME) 11 | clean: 12 | $(RM) *~ $(NAME) 13 | oclean: 14 | $(RM) $(OBJ) 15 | 16 | fclean: clean oclean 17 | 18 | re: oclean all 19 | -------------------------------------------------------------------------------- /makefiles/2-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | OBJ = $(SRC:.c=.o) 4 | NAME = school 5 | 6 | all: $(NAME) 7 | 8 | $(NAME): $(OBJ) 9 | $(CC) $^ -o $@ 10 | 11 | -------------------------------------------------------------------------------- /makefiles/3-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | OBJ = $(SRC:.c=.o) 4 | NAME = school 5 | RM = rm -f 6 | 7 | all: $(OBJ) 8 | 9 | $(CC) $(OBJ) -o $(NAME) 10 | clean: 11 | $(RM) *~ $(NAME) 12 | oclean: 13 | $(RM) $(OBJ) 14 | 15 | fclean: clean oclean 16 | 17 | re: oclean all 18 | 19 | -------------------------------------------------------------------------------- /makefiles/4-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | OBJ = $(SRC:.c=.o) 4 | NAME = school 5 | RM = rm -f 6 | CFLAGS = -Wall -Werror -Wextra -pedantic 7 | 8 | all: $(OBJ) 9 | $(CC) $(OBJ) -o $(NAME) 10 | clean: 11 | $(RM) *~ $(NAME) 12 | oclean: 13 | $(RM) $(OBJ) 14 | 15 | fclean: clean oclean 16 | 17 | re: oclean all 18 | -------------------------------------------------------------------------------- /makefiles/README.md: -------------------------------------------------------------------------------- 1 | Tasks 2 | 0. make -f 0-Makefile 3 | Create your first Makefile. 4 | -------------------------------------------------------------------------------- /makefiles/m.h: -------------------------------------------------------------------------------- 1 | #ifndef __M_H__ 2 | #define __M_H__ 3 | 4 | #include 5 | #include 6 | 7 | void print_school(void); 8 | 9 | #endif 10 | -------------------------------------------------------------------------------- /makefiles/main.c: -------------------------------------------------------------------------------- 1 | #include "m.h" 2 | 3 | /** 4 | * main - Entry point 5 | * 6 | * Return: Always 7 | */ 8 | int main(void) 9 | { 10 | print_school(); 11 | return (EXIT_SUCCESS); 12 | } 13 | /* School */ 14 | /* School */ 15 | /* School */ 16 | -------------------------------------------------------------------------------- /makefiles/main.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/makefiles/main.o -------------------------------------------------------------------------------- /makefiles/school: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/makefiles/school -------------------------------------------------------------------------------- /makefiles/school.c: -------------------------------------------------------------------------------- 1 | #include "m.h" 2 | 3 | /** 4 | * print_school 5 | * 6 | * Return: Nothing 7 | */ 8 | void print_school(void) 9 | { 10 | printf("j#0000000000000000000000000000000000000\n"); 11 | printf("j#000000000000000000@Q**g00000000000000\n"); 12 | printf("j#0000000000000000*]++]4000000000000000\n"); 13 | printf("j#000000000000000k]++]++*N#000000000000\n"); 14 | printf("j#0000000000000*C+++]++]++]J*0000000000\n"); 15 | printf("j#00000000000@+]++qwwwp=]++++]*00000000\n"); 16 | printf("j#0000000000*+++]q#0000k+]+]++]4#000000\n"); 17 | printf("j#00000000*C+]+]w#0000*]+++]+]++0000000\n"); 18 | printf("j#0000we+]wW000***C++]++]+]++++40000000\n"); 19 | printf("j#000000000*C+]+]]+]++]++]++]+q#0000000\n"); 20 | printf("j#0000000*]+]+++++++]++]+++]+++J0000000\n"); 21 | printf("j#000000C++]=]+]+]+]++]++]+]+]+]=000000\n"); 22 | printf("j#00000k+]++]+++]+]++qwW0000000AgW00000\n"); 23 | printf("j#00000k++]++]+]+++qW#00000000000000000\n"); 24 | printf("j#00000A]++]++]++]++J**0000000000000000\n"); 25 | printf("j#000000e]++]+++]++]++]J000000000000000\n"); 26 | printf("j#0000000A]++]+]++]++]++000000000000000\n"); 27 | printf("j#000000000w]++]+]++]+qW#00000000000000\n"); 28 | printf("j#00000000000w]++++]*0##000000000000000\n"); 29 | printf("j#0000000000000Ag]+]++*0000000000000000\n"); 30 | printf("j#00000000000000000we]+]Q00000000000000\n"); 31 | printf("j#0000000000000@@+wgdA]+J00000000000000\n"); 32 | printf("j#0000000000000k?qwgdC=]4#0000000000000\n"); 33 | printf("j#00000000000000w]+]++qw#00000000000000\n"); 34 | printf("\"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"); 35 | } 36 | -------------------------------------------------------------------------------- /makefiles/school.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/makefiles/school.o -------------------------------------------------------------------------------- /malloc_free/0-create_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * create_array - creates array of chars and initializes it with a char 6 | * 7 | * @size: size of the array 8 | * @c: char to initialize the array with 9 | * 10 | * Return: pointer to the array, or NULL if it fails 11 | */ 12 | 13 | char *create_array(unsigned int size, char c) 14 | { 15 | unsigned int i; 16 | char *s; 17 | 18 | s = malloc(sizeof(char) * size); 19 | 20 | if (size == 0 || s == NULL) 21 | { 22 | return (NULL); 23 | } 24 | 25 | i = 0; 26 | while (i < size) 27 | { 28 | s[i] = c; 29 | i++; 30 | } 31 | 32 | return (s); 33 | } 34 | -------------------------------------------------------------------------------- /malloc_free/0-main.c: -------------------------------------------------------------------------------- 1 | #include "main.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 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 | -------------------------------------------------------------------------------- /malloc_free/1-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * main - check the code 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(void) 11 | { 12 | char *s; 13 | 14 | s = _strdup("Duplicated"); 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 | -------------------------------------------------------------------------------- /malloc_free/1-strdup.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _strdup - returns pointer to new allocated space in memory, 6 | * containing copy of string given parameter 7 | * 8 | * @str:string to copy 9 | * 10 | * Return: pointer to duplicated string 11 | */ 12 | 13 | char *_strdup(char *str) 14 | { 15 | char *a; 16 | unsigned int i = 0; 17 | unsigned int j = 0; 18 | 19 | if (str == NULL) 20 | return (NULL); 21 | 22 | while (str[i]) 23 | i++; 24 | 25 | a = malloc(sizeof(char) * i + 1); 26 | 27 | if (a == NULL) 28 | return (NULL); 29 | 30 | while (str[j]) 31 | { 32 | a[j] = str[j]; 33 | j++; 34 | } 35 | return (a); 36 | } 37 | 38 | -------------------------------------------------------------------------------- /malloc_free/2-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * main - check the code 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(void) 11 | { 12 | char *s; 13 | 14 | s = str_concat("Best ", "School"); 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 | -------------------------------------------------------------------------------- /malloc_free/2-str_concat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/malloc_free/2-str_concat -------------------------------------------------------------------------------- /malloc_free/2-str_concat.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /** 6 | * str_concat - concatenates two string 7 | * 8 | * @s1: the first string 9 | * @s2: the string to add to @s1 10 | * 11 | * Return: pointer that points to a newly allocated memory space 12 | * containing @s1, followed by contents of @s2, 13 | * and null terminated 14 | */ 15 | char *str_concat(char *s1, char *s2) 16 | { 17 | size_t i; 18 | char *r; 19 | size_t len1 = strlen(s1); 20 | size_t len2 = strlen(s2); 21 | 22 | 23 | r = (char *)malloc(sizeof(char) * (len1 + len2 + 1)); 24 | 25 | 26 | if (s1 == NULL) 27 | s1 = ""; 28 | if (s2 == NULL) 29 | s2 = ""; 30 | 31 | if (r == NULL) 32 | { 33 | return (NULL); 34 | } 35 | 36 | for (i = 0; i < len1; i++) 37 | { 38 | r[i] = s1[i]; 39 | } 40 | 41 | for (i = 0; i < len2; i++) 42 | { 43 | r[len1 + i] = s2[i]; 44 | } 45 | 46 | r[len1 + len2] = '\0'; 47 | 48 | return (r); 49 | } 50 | 51 | -------------------------------------------------------------------------------- /malloc_free/3-alloc_grid: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/malloc_free/3-alloc_grid -------------------------------------------------------------------------------- /malloc_free/3-alloc_grid.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * alloc_grid - Allocate and initialize a 2D grid of integers. 6 | * @width: The width of the grid. 7 | * @height: The height of the grid. 8 | * 9 | * Return: A pointer to the allocated grid, or NULL on failure. 10 | */ 11 | 12 | int **alloc_grid(int width, int height) 13 | { 14 | int **a; 15 | int i; 16 | 17 | if (width <= 0 || height <= 0) 18 | return (NULL); 19 | 20 | a = malloc(height * sizeof(int *)); 21 | if (a == NULL) 22 | return (NULL); 23 | 24 | for (i = 0; i < height; i++) 25 | { 26 | a[i] = malloc(width * sizeof(int)); 27 | if (a[i] == NULL) 28 | { 29 | while (i >= 0) 30 | free(a[i--]); 31 | free(a); 32 | return (NULL); 33 | } 34 | } 35 | 36 | return (a); 37 | } 38 | -------------------------------------------------------------------------------- /malloc_free/3-main.c: -------------------------------------------------------------------------------- 1 | #include "main.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 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 | -------------------------------------------------------------------------------- /malloc_free/4-free_grid: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/malloc_free/4-free_grid -------------------------------------------------------------------------------- /malloc_free/4-free_grid.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * free_grid - free a 2D grid of integers. 6 | * @grid: pointer to grid 7 | * @height: The height of the grid. 8 | * 9 | * Return: void 10 | */ 11 | 12 | 13 | void free_grid(int **grid, int height) 14 | { 15 | int i; 16 | 17 | for (i = 0; i < height; i++) 18 | { 19 | free(grid[i]); 20 | } 21 | 22 | free(grid); 23 | } 24 | -------------------------------------------------------------------------------- /malloc_free/4-main.c: -------------------------------------------------------------------------------- 1 | #include "main.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 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 | free_grid(grid, 4); 52 | return (0); 53 | } 54 | -------------------------------------------------------------------------------- /malloc_free/README.md: -------------------------------------------------------------------------------- 1 | Tasks 2 | 0. Float like a butterfly, sting like a bee 3 | mandatory 4 | Write a function that creates an array of chars, and initializes it with a specific char. 5 | 6 | Prototype: char *create_array(unsigned int size, char c); 7 | Returns NULL if size = 0 8 | Returns a pointer to the array, or NULL if it fails 9 | 10 | 1. The woman who has no imagination has no wings 11 | mandatory 12 | Write a function that returns a pointer to a newly allocated space in memory, which contains a copy of the string given as a parameter. 13 | 14 | Prototype: char *_strdup(char *str); 15 | The _strdup() function returns a pointer to a new string which is a duplicate of the string str. Memory for the new string is obtained with malloc, and can be freed with free. 16 | Returns NULL if str = NULL 17 | On success, the _strdup function returns a pointer to the duplicated string. It returns NULL if insufficient memory was available 18 | 19 | 2. He who is not courageous enough to take risks will accomplish nothing in life 20 | mandatory 21 | Write a function that concatenates two strings. 22 | 23 | Prototype: char *str_concat(char *s1, char *s2); 24 | The returned pointer should point to a newly allocated space in memory which contains the contents of s1, followed by the contents of s2, and null terminated 25 | if NULL is passed, treat it as an empty string 26 | The function should return NULL on failure 27 | 28 | 3. If you even dream of beating me you'd better wake up and apologize 29 | mandatory 30 | Write a function that returns a pointer to a 2 dimensional array of integers. 31 | 32 | Prototype: int **alloc_grid(int width, int height); 33 | Each element of the grid should be initialized to 0 34 | The function should return NULL on failure 35 | If width or height is 0 or negative, return NULL 36 | 37 | 4. It's not bragging if you can back it up 38 | mandatory 39 | Write a function that frees a 2 dimensional grid previously created by your alloc_grid function. 40 | 41 | Prototype: void free_grid(int **grid, int height); 42 | Note that we will compile with your alloc_grid.c file. Make sure it compiles. 43 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /malloc_free/a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/malloc_free/a -------------------------------------------------------------------------------- /malloc_free/main.h: -------------------------------------------------------------------------------- 1 | int _putchar(char c); 2 | 3 | char *create_array(unsigned int size, char c); 4 | 5 | char *_strdup(char *str); 6 | 7 | 8 | char *str_concat(char *s1, char *s2); 9 | 10 | 11 | int **alloc_grid(int width, int height); 12 | 13 | void free_grid(int **grid, int height); 14 | 15 | 16 | -------------------------------------------------------------------------------- /malloc_free/s: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/malloc_free/s -------------------------------------------------------------------------------- /more_functions_nested_loops/0-isuper: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/more_functions_nested_loops/0-isuper -------------------------------------------------------------------------------- /more_functions_nested_loops/0-isupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isupper - checks if letter is uppercase 5 | * 6 | * @c: the character to check 7 | * 8 | * Return: 1 if c is a uppercase letter, return 0 otherwise 9 | */ 10 | int _isupper(int c) 11 | { 12 | if (c >= 'A' && c <= 'Z') 13 | return (1); 14 | else 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /more_functions_nested_loops/0-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - check the code. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char c; 12 | 13 | c = 'A'; 14 | printf("%c: %d\n", c, _isupper(c)); 15 | c = 'a'; 16 | printf("%c: %d\n", c, _isupper(c)); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /more_functions_nested_loops/1-isdigit: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/more_functions_nested_loops/1-isdigit -------------------------------------------------------------------------------- /more_functions_nested_loops/1-isdigit.C: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/more_functions_nested_loops/1-isdigit.C -------------------------------------------------------------------------------- /more_functions_nested_loops/1-isdigit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isdigit - print digit of c 5 | *@c: integer with digit printed 6 | * Return: 1 and 0 7 | */ 8 | int _isdigit(int c) 9 | { 10 | if (c >= '0' && c <= '9') 11 | { 12 | return (1); 13 | } 14 | else 15 | { 16 | return (0); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /more_functions_nested_loops/1-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char c; 12 | 13 | c = '0'; 14 | printf("%c: %d\n", c, _isdigit(c)); 15 | c = 'a'; 16 | printf("%c: %d\n", c, _isdigit(c)); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /more_functions_nested_loops/10-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - check the code 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | print_triangle(2); 11 | print_triangle(10); 12 | print_triangle(1); 13 | print_triangle(0); 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /more_functions_nested_loops/10-print_triangle.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_triangle - print triangle # 5 | * @size : max 6 | * Return: nothing 7 | */ 8 | 9 | void print_triangle(int size) 10 | { 11 | int i, j, k; 12 | 13 | if (size < 1) 14 | { 15 | _putchar('\n'); 16 | return; 17 | } 18 | 19 | for (i = 1 ; i <= size ; i++) 20 | { 21 | for (j = size - i ; j > 0 ; j--) 22 | { 23 | _putchar(' '); 24 | } 25 | for (k = 1 ; k <= i ; k++) 26 | { 27 | _putchar('#'); 28 | } 29 | _putchar('\n'); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /more_functions_nested_loops/10-triangles: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/more_functions_nested_loops/10-triangles -------------------------------------------------------------------------------- /more_functions_nested_loops/100-prime_factor: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/more_functions_nested_loops/100-prime_factor -------------------------------------------------------------------------------- /more_functions_nested_loops/100-prime_factor.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * main - main block 6 | * des: Find and print largest prime factor of 7 | * 612852475143 8 | * Return: 0 9 | */ 10 | int main(void) 11 | { 12 | int j; 13 | long num = 612852475143; 14 | 15 | for (j = (int) sqrt(num); j > 2; j++) 16 | { 17 | if (num % j == 0) 18 | { 19 | printf("%d\n", j); 20 | break; 21 | } 22 | } 23 | 24 | return (0); 25 | } 26 | -------------------------------------------------------------------------------- /more_functions_nested_loops/101-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - check the code 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | 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 | -------------------------------------------------------------------------------- /more_functions_nested_loops/101-print_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_number - Prints an integer. 5 | * @n: integer to be printed. 6 | */ 7 | void print_number(int n) 8 | { 9 | unsigned int num = n; 10 | 11 | if (n < 0) 12 | { 13 | _putchar('-'); 14 | num = -num; 15 | } 16 | 17 | if ((num / 10) > 0) 18 | print_number(num / 10); 19 | 20 | _putchar((num % 10) + '0'); 21 | } 22 | -------------------------------------------------------------------------------- /more_functions_nested_loops/101-print_numbers: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/more_functions_nested_loops/101-print_numbers -------------------------------------------------------------------------------- /more_functions_nested_loops/2-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | printf("%d\n", mul(98, 1024)); 12 | printf("%d\n", mul(-402, 4096)); 13 | return (0); 14 | } 15 | -------------------------------------------------------------------------------- /more_functions_nested_loops/2-mul: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/more_functions_nested_loops/2-mul -------------------------------------------------------------------------------- /more_functions_nested_loops/2-mul.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * mul - Adds two integers. 5 | * @a: first integer. 6 | * @b: second integer. 7 | * 8 | * Return: The result of the addition. 9 | */ 10 | int mul(int a, int b) 11 | { 12 | return (a * b); 13 | } 14 | -------------------------------------------------------------------------------- /more_functions_nested_loops/3-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - check the code 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | print_numbers(); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /more_functions_nested_loops/3-print_numbers: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/more_functions_nested_loops/3-print_numbers -------------------------------------------------------------------------------- /more_functions_nested_loops/3-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_numbers - print number 0 to 9 5 | * Return: nothing 6 | */ 7 | 8 | void print_numbers(void) 9 | { 10 | char c; 11 | 12 | for (c = '0'; c <= '9'; c++) 13 | { 14 | _putchar(c); 15 | } 16 | _putchar('\n'); 17 | } 18 | 19 | -------------------------------------------------------------------------------- /more_functions_nested_loops/4-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - check the code 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | print_most_numbers(); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /more_functions_nested_loops/4-print_most_numbers: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/more_functions_nested_loops/4-print_most_numbers -------------------------------------------------------------------------------- /more_functions_nested_loops/4-print_most_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_most_numbers - print number 0 to 9 5 | * Return: nothing 6 | */ 7 | 8 | void print_most_numbers(void) 9 | { 10 | char c; 11 | 12 | for (c = '0'; c <= '9'; c++) 13 | { 14 | if (c != '2' && c != '4') 15 | _putchar(c); 16 | } 17 | _putchar('\n'); 18 | } 19 | 20 | -------------------------------------------------------------------------------- /more_functions_nested_loops/5-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - check the code 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | more_numbers(); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /more_functions_nested_loops/5-more_numbers: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/more_functions_nested_loops/5-more_numbers -------------------------------------------------------------------------------- /more_functions_nested_loops/5-more_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * more_numbers - print number 0 to 14 ten times 5 | * Return: nothing 6 | */ 7 | 8 | void more_numbers(void) 9 | { 10 | int i, j; 11 | 12 | for (i = 0; i < 10; i++) 13 | { 14 | for (j = 0; j <= 14; j++) 15 | { 16 | if (j >= 10) 17 | { 18 | _putchar(j / 10 + '0'); 19 | } 20 | _putchar(j % 10 + '0'); 21 | } 22 | _putchar('\n'); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /more_functions_nested_loops/6-lines: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/more_functions_nested_loops/6-lines -------------------------------------------------------------------------------- /more_functions_nested_loops/6-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - check the code 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | print_line(0); 11 | print_line(2); 12 | print_line(10); 13 | print_line(-4); 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /more_functions_nested_loops/6-print_line.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_line - print straight lines _ 5 | * @n : integer 6 | * Return: nothing 7 | */ 8 | void print_line(int n) 9 | { 10 | int i; 11 | 12 | if (n <= 0) 13 | { 14 | _putchar('\n'); 15 | return; 16 | } 17 | 18 | for (i = 1 ; i <= n ; i++) 19 | { 20 | _putchar('_'); 21 | } 22 | _putchar('\n'); 23 | } 24 | -------------------------------------------------------------------------------- /more_functions_nested_loops/7-diagonals: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/more_functions_nested_loops/7-diagonals -------------------------------------------------------------------------------- /more_functions_nested_loops/7-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - check the code 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | print_diagonal(0); 11 | print_diagonal(2); 12 | print_diagonal(10); 13 | print_diagonal(-4); 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /more_functions_nested_loops/7-print_diagonal.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_diagonal - print diagonal 5 | * @limit : max 6 | * Return: nothing 7 | */ 8 | 9 | void print_diagonal(int limit) 10 | { 11 | int i, j; 12 | 13 | if (limit < 1) 14 | { 15 | _putchar('\n'); 16 | return; 17 | } 18 | 19 | for (i = 0 ; i < limit ; i++) 20 | { 21 | for (j = 0 ; j < i ; j++) 22 | { 23 | _putchar(' '); 24 | } 25 | _putchar('\\'); 26 | _putchar('\n'); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /more_functions_nested_loops/8-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - check the code 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | print_square(2); 11 | print_square(10); 12 | print_square(0); 13 | return (0); 14 | } 15 | -------------------------------------------------------------------------------- /more_functions_nested_loops/8-print_square.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_square - print square # 5 | * @size : size 6 | * Return: nothing 7 | */ 8 | 9 | void print_square(int size) 10 | { 11 | int i, j; 12 | 13 | if (size < 1) 14 | { 15 | _putchar('\n'); 16 | return; 17 | } 18 | 19 | for (i = 0 ; i < size ; i++) 20 | { 21 | for (j = 0 ; j < size ; j++) 22 | { 23 | _putchar('#'); 24 | } 25 | _putchar('\n'); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /more_functions_nested_loops/8-squares: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/more_functions_nested_loops/8-squares -------------------------------------------------------------------------------- /more_functions_nested_loops/9-fizz_buzz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/more_functions_nested_loops/9-fizz_buzz -------------------------------------------------------------------------------- /more_functions_nested_loops/9-fizz_buzz.C: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/more_functions_nested_loops/9-fizz_buzz.C -------------------------------------------------------------------------------- /more_functions_nested_loops/9-fizz_buzz.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Entry point 5 | * Return: 0 6 | */ 7 | int main(void) 8 | { 9 | int i; 10 | 11 | for (i = 1; i <= 100; i++) 12 | { 13 | if (i % 3 == 0) 14 | printf("Fizz"); 15 | if (i % 5 == 0) 16 | printf("Buzz"); 17 | if (i % 3 != 0 && i % 5 != 0) 18 | printf("%d", i); 19 | if (i < 100) 20 | printf(" "); 21 | } 22 | printf("\n"); 23 | 24 | return (0); 25 | } 26 | -------------------------------------------------------------------------------- /more_functions_nested_loops/README.md: -------------------------------------------------------------------------------- 1 | C - More functions, more nested loops 2 | 3 | Tasks 4 | 0. isupper 5 | mandatory 6 | Write a function that checks for uppercase character. 7 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /more_functions_nested_loops/clear: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_line - print straight line 5 | * @n : integer for line 6 | * Return: 0 7 | */ 8 | 9 | void print_line(int n) 10 | { 11 | int i; 12 | 13 | if (n <= 0) 14 | { 15 | _putchar('\n'); 16 | return; 17 | } 18 | for (i = 0; i < n; i++) 19 | { 20 | _putchar('_'); 21 | } 22 | _putchar('\n'); 23 | } 24 | 25 | -------------------------------------------------------------------------------- /more_functions_nested_loops/main.h: -------------------------------------------------------------------------------- 1 | int _putchar(char c); 2 | 3 | int _isupper(int c); 4 | 5 | int _isdigit(int c); 6 | 7 | int mul(int a, int b); 8 | 9 | void print_numbers(void); 10 | 11 | void print_most_numbers(void); 12 | 13 | void more_numbers(void); 14 | 15 | void print_line(int n); 16 | 17 | void print_diagonal(int n); 18 | 19 | void print_square(int size); 20 | 21 | void print_triangle(int size); 22 | 23 | void print_number(int n); 24 | -------------------------------------------------------------------------------- /more_malloc_free/0-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | #include 5 | 6 | /** 7 | * main - check the code 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 | -------------------------------------------------------------------------------- /more_malloc_free/0-malloc_checked.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * *malloc_checked - allocate memory with malloc 6 | * @b: number of bytes 7 | * Return: pointer to memory 8 | */ 9 | 10 | void *malloc_checked(unsigned int b) 11 | { 12 | int *p; 13 | 14 | p = malloc(b); 15 | 16 | if (p == NULL) 17 | { 18 | exit(98); 19 | } 20 | return (p); 21 | } 22 | 23 | -------------------------------------------------------------------------------- /more_malloc_free/1-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * main - check the code 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(void) 11 | { 12 | char *concat; 13 | 14 | concat = string_nconcat("Best ", "School !!!", 6); 15 | printf("%s\n", concat); 16 | free(concat); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /more_malloc_free/1-string_nconcat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/more_malloc_free/1-string_nconcat -------------------------------------------------------------------------------- /more_malloc_free/1-string_nconcat.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /** 6 | * string_nconcat - Concatenates two strings with the first n bytes of s2 7 | * @s1: first string to concatenate 8 | * @s2: second string to concatenate (up to the first n bytes). 9 | * @n: maximum number of bytes from s2 to concatenate 10 | * 11 | * Return: pointer to the newly allocated concatenated string, or NULL 12 | */ 13 | char *string_nconcat(char *s1, char *s2, unsigned int n) 14 | { 15 | unsigned int i, j; 16 | char *cat; 17 | unsigned int len1 = 0; 18 | unsigned int len2 = 0; 19 | 20 | if (s1 == NULL) 21 | s1 = ""; 22 | if (s2 == NULL) 23 | s2 = ""; 24 | 25 | while (s1[len1]) 26 | len1++; 27 | while (s2[len2]) 28 | len2++; 29 | 30 | if (n >= len2) 31 | n = len2; 32 | 33 | cat = (char *)malloc(sizeof(char) * (len1 + n + 1)); 34 | 35 | if (cat == NULL) 36 | { 37 | return (NULL); 38 | } 39 | 40 | for (i = 0; i < len1; i++) 41 | { 42 | cat[i] = s1[i]; 43 | } 44 | 45 | for (j = 0; j < n; j++) 46 | { 47 | cat[i + j] = s2[j]; 48 | } 49 | 50 | cat[i + j] = '\0'; 51 | 52 | return (cat); 53 | } 54 | 55 | -------------------------------------------------------------------------------- /more_malloc_free/2-calloc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/more_malloc_free/2-calloc -------------------------------------------------------------------------------- /more_malloc_free/2-calloc.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _calloc - allocates memory for an array and initializes it to zero 6 | * @nmemb: number of elements in the array 7 | * @size: size of each element 8 | * 9 | * Return: a pointer to the allocated memory, or NULL on failure 10 | */ 11 | void *_calloc(unsigned int nmemb, unsigned int size) 12 | { 13 | unsigned int i; 14 | char *s; 15 | 16 | if (nmemb == 0 || size == 0) 17 | { 18 | return (NULL); 19 | } 20 | 21 | s = malloc(nmemb * size); 22 | 23 | if (s == NULL) 24 | { 25 | return (NULL); 26 | } 27 | 28 | for (i = 0; i < (nmemb * size); i++) 29 | { 30 | s[i] = 0; 31 | } 32 | 33 | return (s); 34 | } 35 | -------------------------------------------------------------------------------- /more_malloc_free/2-main.c: -------------------------------------------------------------------------------- 1 | #include "main.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 36 | * 37 | * Return: Always 0. 38 | */ 39 | int main(void) 40 | { 41 | char *a; 42 | 43 | a = _calloc(98, sizeof(char)); 44 | strcpy(a, "Best"); 45 | strcpy(a + 4, " School! :)\n"); 46 | a[97] = '!'; 47 | simple_print_buffer(a, 98); 48 | free(a); 49 | return (0); 50 | } 51 | -------------------------------------------------------------------------------- /more_malloc_free/3-array_range: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/more_malloc_free/3-array_range -------------------------------------------------------------------------------- /more_malloc_free/3-array_range.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * array_range - creates an array of integers from min to max 6 | * 7 | * @min: minimum value 8 | * @max: maximum value 9 | * 10 | * Return: pointer to the array, or NULL if it fails 11 | */ 12 | int *array_range(int min, int max) 13 | { 14 | int *array; 15 | int i, size; 16 | 17 | if (min > max) 18 | return (NULL); 19 | 20 | size = max - min + 1; 21 | array = malloc(sizeof(int) * size); 22 | 23 | if (array == NULL) 24 | return (NULL); 25 | 26 | for (i = 0; i < size; i++) 27 | { 28 | array[i] = min; 29 | min++; 30 | } 31 | 32 | return (array); 33 | } 34 | 35 | -------------------------------------------------------------------------------- /more_malloc_free/3-main.c: -------------------------------------------------------------------------------- 1 | #include "main.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 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 | -------------------------------------------------------------------------------- /more_malloc_free/README.md: -------------------------------------------------------------------------------- 1 | Tasks 2 | 0. Trust no one 3 | mandatory 4 | Write a function that allocates memory using malloc. 5 | 6 | Prototype: void *malloc_checked(unsigned int b); 7 | Returns a pointer to the allocated memory 8 | if malloc fails, the malloc_checked function should cause normal process termination with a status value of 98 9 | 10 | 1. string_nconcat 11 | mandatory 12 | Write a function that concatenates two strings. 13 | 14 | Prototype: char *string_nconcat(char *s1, char *s2, unsigned int n); 15 | The returned pointer shall point to a newly allocated space in memory, which contains s1, followed by the first n bytes of s2, and null terminated 16 | If the function fails, it should return NULL 17 | If n is greater or equal to the length of s2 then use the entire string s2 18 | if NULL is passed, treat it as an empty string 19 | 20 | 2. _calloc 21 | mandatory 22 | Write a function that allocates memory for an array, using malloc. 23 | 24 | Prototype: void *_calloc(unsigned int nmemb, unsigned int size); 25 | The _calloc function allocates memory for an array of nmemb elements of size bytes each and returns a pointer to the allocated memory. 26 | The memory is set to zero 27 | If nmemb or size is 0, then _calloc returns NULL 28 | If malloc fails, then _calloc returns NULL 29 | 30 | 3. array_range 31 | mandatory 32 | Write a function that creates an array of integers. 33 | 34 | Prototype: int *array_range(int min, int max); 35 | The array created should contain all the values from min (included) to max (included), ordered from min to max 36 | Return: the pointer to the newly created array 37 | If min > max, return NULL 38 | If malloc fails, return NULL 39 | 40 | -------------------------------------------------------------------------------- /more_malloc_free/a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/more_malloc_free/a -------------------------------------------------------------------------------- /more_malloc_free/main.h: -------------------------------------------------------------------------------- 1 | int _putchar(char c); 2 | 3 | char *create_array(unsigned int size, char c); 4 | 5 | char *_strdup(char *str); 6 | 7 | 8 | char *str_concat(char *s1, char *s2); 9 | 10 | 11 | int **alloc_grid(int width, int height); 12 | 13 | void free_grid(int **grid, int height); 14 | 15 | void *malloc_checked(unsigned int b); 16 | 17 | char *string_nconcat(char *s1, char *s2, unsigned int n); 18 | 19 | void *_calloc(unsigned int nmemb, unsigned int size); 20 | 21 | int *array_range(int min, int max); 22 | 23 | 24 | -------------------------------------------------------------------------------- /pointers_arrays_strings/0-98: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/pointers_arrays_strings/0-98 -------------------------------------------------------------------------------- /pointers_arrays_strings/0-memset.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * *_memset - fill memory with constant byte 4 | *@s: memory area 5 | *@b: constant byte 6 | *@n: n bytes from memory 7 | *Return: memory area pointed to 8 | */ 9 | 10 | char *_memset(char *s, char b, unsigned int n) 11 | { 12 | unsigned int i; 13 | 14 | for (i = 0; n > 0; i++, n--) 15 | { 16 | s[i] = b; 17 | } 18 | return (s); 19 | } 20 | -------------------------------------------------------------------------------- /pointers_arrays_strings/0-reset_to_98.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * reset_to_98 - update value of n to 98 5 | * @n: integer to be updated 6 | * Return: 0 7 | */ 8 | 9 | void reset_to_98(int *n) 10 | { 11 | *n = 98; 12 | } 13 | -------------------------------------------------------------------------------- /pointers_arrays_strings/0-strcat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _strcat - Concatenate two strings. 6 | * @dest: The destination string to which the source will be appended. 7 | * @src: The source string that will be appended to the destination. 8 | * 9 | * Return: A pointer to the destination string (dest). 10 | */ 11 | 12 | char *_strcat(char *dest, char *src) 13 | { 14 | int i = 0; 15 | int j = 0; 16 | 17 | while (dest[i] != '\0') 18 | { 19 | i++; 20 | } 21 | 22 | while (src[j] != '\0') 23 | { 24 | dest[i] = src[j]; 25 | i++; 26 | j++; 27 | } 28 | 29 | dest[i] = '\0'; 30 | 31 | return (dest); 32 | } 33 | 34 | -------------------------------------------------------------------------------- /pointers_arrays_strings/1-memcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * *_memcpy - Copy string 4 | *@dest: destination 5 | *@src: source 6 | *@n: n bytes from memory 7 | *Return: dest 8 | */ 9 | 10 | char *_memcpy(char *dest, char *src, unsigned int n) 11 | { 12 | unsigned int i; 13 | 14 | for (i = 0; i < n; i++) 15 | { 16 | dest[i] = src[i]; 17 | } 18 | return (dest); 19 | } 20 | -------------------------------------------------------------------------------- /pointers_arrays_strings/1-strncat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strncat - Concatenate 2 strings 5 | * @dest: The destination string to which the source will be appended. 6 | * @src: The source string that will be appended to the destination. 7 | * @n: The maximum number of characters to append from src. 8 | * 9 | * Return: A pointer to the resulting string (dest). 10 | */ 11 | 12 | char *_strncat(char *dest, char *src, int n) 13 | { 14 | int i = 0; 15 | int j = 0; 16 | 17 | 18 | while (dest[i] != '\0') 19 | { 20 | i++; 21 | } 22 | 23 | 24 | while (src[j] != '\0' && j < n) 25 | { 26 | dest[i] = src[j]; 27 | i++; 28 | j++; 29 | } 30 | 31 | dest[i] = '\0'; 32 | 33 | return (dest); 34 | } 35 | 36 | -------------------------------------------------------------------------------- /pointers_arrays_strings/1-swap.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * swap_int - swap value a and b 5 | * @a: integer to be swaped with b 6 | * @b: integer to be swapped with a 7 | * Return: 0 8 | */ 9 | void swap_int(int *a, int *b) 10 | { 11 | int tmp = *b; 12 | *b = *a; 13 | *a = tmp; 14 | } 15 | -------------------------------------------------------------------------------- /pointers_arrays_strings/100-atoi.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | 5 | /** 6 | * _atoi - Converts a string to an integer, considering negative values. 7 | * @nombre: The input string to convert. 8 | * Return: The converted integer value, or 0 if conversion fails. 9 | */ 10 | 11 | int _atoi(char *nombre) 12 | { 13 | int i; 14 | int negatif = 1; 15 | unsigned int entier = 0; 16 | int traitementfait = 0; 17 | 18 | for (i = 0; nombre[i] != 0; i++) 19 | { 20 | if (nombre[i] == '-') 21 | negatif = negatif * -1; 22 | 23 | if (nombre[i] >= '0' && nombre[i] <= '9') 24 | { 25 | entier = (entier * 10) + nombre[i] - '0'; 26 | traitementfait = 1; 27 | } 28 | else if (traitementfait == 1) 29 | break; 30 | } 31 | 32 | entier = entier * negatif; 33 | return (entier); 34 | } 35 | -------------------------------------------------------------------------------- /pointers_arrays_strings/2-strchr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _strchr - locates a character in a string 6 | * 7 | * @s: string to check 8 | * @c: character we are trying to find 9 | * 10 | * Return: pointer to first time display @c in the string 11 | * @s. Return NULL if the character isn't found 12 | */ 13 | 14 | char *_strchr(char *s, char c) 15 | { 16 | while (*s) 17 | { 18 | if (*s != c) 19 | s++; 20 | else 21 | return (s); 22 | } 23 | if (c == '\0') 24 | return (s); 25 | 26 | return ('\0'); 27 | } 28 | -------------------------------------------------------------------------------- /pointers_arrays_strings/2-strlen.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _strlen - find length 5 | * @s: char for lenght 6 | * Return: 0 7 | */ 8 | 9 | int _strlen(char *s) 10 | { 11 | int i = 0; 12 | 13 | for (; *s != '\0'; s++) 14 | { 15 | i++; 16 | } 17 | return (i); 18 | } 19 | -------------------------------------------------------------------------------- /pointers_arrays_strings/2-strncpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strncpy - Copy a string with a max length 5 | * 6 | * @dest: The destination string where copy will be stored. 7 | * @src: The source string to copy from. 8 | * @n: ma number of characters to copy from src 9 | * 10 | * Return: dest 11 | */ 12 | 13 | char *_strncpy(char *dest, char *src, int n) 14 | { 15 | int i; 16 | 17 | for (i = 0; i < n && src[i] != '\0'; i++) 18 | { 19 | dest[i] = src[i]; 20 | } 21 | for (; n > i; i++) 22 | { 23 | dest[i] = '\0'; 24 | } 25 | 26 | return (dest); 27 | } 28 | -------------------------------------------------------------------------------- /pointers_arrays_strings/3-puts.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _puts - print the string 5 | * @str: char to print string 6 | * Return: nothing 7 | */ 8 | 9 | void _puts(char *str) 10 | { 11 | for (; *str != '\0'; str++) 12 | { 13 | _putchar(*str); 14 | } 15 | 16 | _putchar('\n'); 17 | } 18 | -------------------------------------------------------------------------------- /pointers_arrays_strings/3-strcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _strcmp - Compare two strings. 6 | * @s1: first string to compare. 7 | * @s2: second string to compare. 8 | * 9 | * Return: 0 if the strings are equal. 10 | * . 11 | */ 12 | 13 | int _strcmp(char *s1, char *s2) 14 | { 15 | int i = 0; 16 | 17 | while (s1[i] != '\0' && s2[i] != '\0') 18 | { 19 | if (s1[i] != s2[i]) 20 | { 21 | return (s1[i] - s2[i]); 22 | } 23 | i++; 24 | } 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /pointers_arrays_strings/3-strspn.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strspn - length of a prefix substring made of chars in accept 5 | * 6 | * @s: string to search. 7 | * @accept: characters to include in the substring. 8 | * 9 | * Return: number of bytes in initial segment made of bytes from accept 10 | */ 11 | unsigned int _strspn(char *s, char *accept) 12 | { 13 | int i = 0; 14 | int j; 15 | 16 | while (s[i] != '\0') 17 | { 18 | if (s[i] == ' ') 19 | break; 20 | 21 | for (j = 0; accept[j] != '\0'; j++) 22 | { 23 | if (s[i] == accept[j]) 24 | break; 25 | } 26 | 27 | if (accept[j] == '\0') 28 | break; 29 | 30 | i++; 31 | } 32 | 33 | return (i); 34 | } 35 | 36 | -------------------------------------------------------------------------------- /pointers_arrays_strings/4-print_rev.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_rev - print string in reverse 5 | * @s: char for lenght 6 | * Return: 0 7 | */ 8 | 9 | void print_rev(char *s) 10 | { 11 | int i = 0; 12 | 13 | while (s[i] != '\0') 14 | { 15 | i++; 16 | } 17 | for (; i > 0; i--) 18 | { 19 | _putchar(s[i - 1]); 20 | } 21 | 22 | 23 | _putchar('\n'); 24 | } 25 | -------------------------------------------------------------------------------- /pointers_arrays_strings/4-rev_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * reverse_array - content of array of integers 6 | * @a: Pointer to the array. 7 | * @n: Number of elements in array 8 | * return: void 9 | */ 10 | void reverse_array(int *a, int n) 11 | { 12 | int i; 13 | int j; 14 | 15 | for (i = 0; i < n / 2; i++) 16 | { 17 | j = a[i]; 18 | a[i] = a[n - i - 1]; 19 | a[n - i - 1] = j; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /pointers_arrays_strings/4-strpbrk.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _strpbrk - locates a character in a string 6 | * 7 | * @s: string to check 8 | * @accept: characters to search for 9 | * 10 | * Return: pointer to byte in @s that matches one byte in @accept 11 | */ 12 | char *_strpbrk(char *s, char *accept) 13 | { 14 | int i; 15 | 16 | while (*s) 17 | { 18 | 19 | for (i = 0; accept[i]; i++) 20 | { 21 | 22 | if (*s == accept[i]) 23 | { 24 | 25 | return (s); 26 | } 27 | } 28 | s++; 29 | } 30 | return ('\0'); 31 | } 32 | -------------------------------------------------------------------------------- /pointers_arrays_strings/5-rev_string.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * rev_string - reverse a string 5 | * @s: input string 6 | * Return: void 7 | */ 8 | 9 | void rev_string(char *s) 10 | { 11 | int length = 0, i = 0; 12 | char c; 13 | 14 | while (s[i++]) 15 | length++; 16 | 17 | for (i = length - 1; i >= length / 2; i--) 18 | { 19 | c = s[i]; 20 | s[i] = s[length - i - 1]; 21 | s[length - i - 1] = c; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /pointers_arrays_strings/5-string_toupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * string_toupper - Convert a string to uppercase 7 | * @a: Pointer to string 8 | * Return: Pointer modified string 9 | */ 10 | 11 | 12 | char *string_toupper(char *a) 13 | { 14 | int i; 15 | 16 | for (i = 0; a[i] != '\0'; i++) 17 | { 18 | if (a[i] >= 'a' && a[i] <= 'z') 19 | { 20 | a[i] = a[i] - 32; 21 | } 22 | } 23 | 24 | return (a); 25 | } 26 | -------------------------------------------------------------------------------- /pointers_arrays_strings/5-strstr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _strstr - locates a substring 6 | * @haystack: the string to search 7 | * @needle: the substring to find 8 | * 9 | * Return: pointer to the beginning of the located substring 10 | */ 11 | char *_strstr(char *haystack, char *needle) 12 | { 13 | while (*haystack) 14 | { 15 | int i = 0; 16 | int j = 0; 17 | 18 | while (needle[j] && (haystack[i] == needle[j])) 19 | { 20 | i++; 21 | j++; 22 | } 23 | 24 | if (!needle[j]) 25 | { 26 | return (haystack); 27 | } 28 | 29 | haystack++; 30 | } 31 | 32 | return ('\0'); 33 | } 34 | 35 | -------------------------------------------------------------------------------- /pointers_arrays_strings/6-cap_string.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * cap_string - capitalizes all words of a string 5 | * 6 | * @c: string to capitalize 7 | * 8 | * Return: capitalized string 9 | */ 10 | 11 | char *cap_string(char *c) 12 | { 13 | int i = 0; 14 | int j; 15 | char headache[] = " \n\t,;.!?\"(){}"; 16 | 17 | 18 | if (c[0] >= 'a' && c[0] <= 'z') 19 | c[0] -= 32; 20 | 21 | for (; c[i] != '\0'; i++) 22 | { 23 | for (j = 0; j < 14; j++) 24 | { 25 | if (c[i] == headache[j]) 26 | { 27 | if (c[i + 1] >= 'a' && c[i + 1] <= 'z') 28 | c[i + 1] -= 32; 29 | } 30 | } 31 | } 32 | 33 | return (c); 34 | } 35 | 36 | -------------------------------------------------------------------------------- /pointers_arrays_strings/6-puts2.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * puts2 - Prints every other char of string, starting with first char 7 | * @str: Input string. 8 | */ 9 | 10 | void puts2(char *str) 11 | { 12 | int i = 0; 13 | 14 | while (str[i] != '\0') 15 | { 16 | if (i % 2 == 0) 17 | _putchar(str[i]); 18 | i++; 19 | } 20 | 21 | _putchar('\n'); 22 | } 23 | -------------------------------------------------------------------------------- /pointers_arrays_strings/7-leet.c: -------------------------------------------------------------------------------- 1 | /** 2 | * leet - encode string to 1337 3 | * @s: string to be encoded 4 | * Return: pointer to the encoded string 5 | */ 6 | char *leet(char *s) 7 | { 8 | char *point = s; 9 | char a[] = { 'a', 'e', 'o', 't', 'l', 'A', 'E', 'O', 'T', 'L'}; 10 | char c[] = { 4, 3, 0, 7, 1 }; 11 | int i = 0; 12 | 13 | while (*s) 14 | { 15 | for (i = 0; i < 5; i++) 16 | { 17 | if (*s == a[i] || *s == a[i] - 32) 18 | *s = c[i] + '0'; 19 | } 20 | s++; 21 | } 22 | return (point); 23 | } 24 | -------------------------------------------------------------------------------- /pointers_arrays_strings/7-print_chessboard.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * print_chessboard - function to print a chessboard 5 | * @plateau: array representing the chessboard 6 | * 7 | * This function takes array representing a chessboard and prints it to the 8 | * standard output. 9 | */ 10 | 11 | void print_chessboard(char (*plateau)[8]) 12 | { 13 | int i, j; 14 | 15 | for (i = 0; i < 8; i++) 16 | { 17 | for (j = 0; j < 8; j++) 18 | putchar (plateau[i][j]); 19 | putchar('\n'); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /pointers_arrays_strings/7-puts_half.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * puts_half - Prints the second half of a string. 6 | * @str: Input string. 7 | */ 8 | 9 | void puts_half(char *str) 10 | { 11 | int i; 12 | int halflen; 13 | int len = strlen(str); 14 | 15 | if (len % 2 == 0) 16 | { 17 | halflen = len / 2; 18 | } 19 | else 20 | { 21 | halflen = (len + 1) / 2; 22 | } 23 | 24 | for (i = halflen; i < len; i++) 25 | { 26 | _putchar(str[i]); 27 | } 28 | 29 | _putchar('\n'); 30 | } 31 | -------------------------------------------------------------------------------- /pointers_arrays_strings/8-print_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * print_array - Prints elements from array 6 | * @a: integer for pointer 7 | * @n: integer for n elements 8 | */ 9 | 10 | void print_array(int *a, int n) 11 | { 12 | int i; 13 | 14 | for (i = 0; i < n ; i++) 15 | { 16 | if (i != n - 1) 17 | printf("%d, ", a[i]); 18 | else 19 | printf("%d", a[i]); 20 | } 21 | printf("\n"); 22 | } 23 | -------------------------------------------------------------------------------- /pointers_arrays_strings/8-print_diagsums.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * print_diagsums - calculate and print diagonal sums of a square matrix 5 | * @a: pointer to the square matrix 6 | * @size: size of the square matrix 7 | * 8 | * This function calculates and prints the sums of the main diagonal and 9 | * anti-diagonal of a square matrix represented by the array 'a' 10 | */ 11 | 12 | void print_diagsums(int *a, int size) 13 | { 14 | int i, cumul = 0; 15 | 16 | for (i = 0; i < (size * size); i += size + 1) 17 | cumul = cumul + a[i]; 18 | printf("%d, ", cumul); 19 | 20 | cumul = 0; 21 | for (i = size ; i < (size * size); i += size - 1) 22 | cumul = cumul + a[i - 1]; 23 | printf("%d\n", cumul); 24 | } 25 | -------------------------------------------------------------------------------- /pointers_arrays_strings/9-strcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strcpy - Copy paste string 4 | *@dest: destination 5 | *@src: source 6 | *Return: dest 7 | */ 8 | char *_strcpy(char *dest, char *src) 9 | { 10 | int i = 0; 11 | 12 | while (*(src + i) != '\0') 13 | { 14 | *(dest + i) = *(src + i); 15 | i++; 16 | } 17 | *(dest + i) = '\0'; 18 | return (dest); 19 | } 20 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /pointers_arrays_strings/clear: -------------------------------------------------------------------------------- 1 | /* 2 | * file name: 3-puts.c 3 | * author: Chloe .C 4 | */ 5 | 6 | #include "main.h" 7 | 8 | /** 9 | * _puts: print the string 10 | * @str: char to print string 11 | * Return: nothing 12 | */ 13 | 14 | void _puts(char *str) 15 | { 16 | for (; *str != '\0'; str++) 17 | { 18 | _putchar(*str); 19 | } 20 | 21 | _putchar('\n'); 22 | } 23 | -------------------------------------------------------------------------------- /pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | int _putchar(char c); 2 | 3 | void reset_to_98(int *n); 4 | 5 | void swap_int(int *a, int *b); 6 | 7 | int _strlen(char *s); 8 | 9 | void _puts(char *str); 10 | 11 | void print_rev(char *s); 12 | 13 | void rev_string(char *s); 14 | 15 | void puts2(char *str); 16 | 17 | void puts_half(char *str); 18 | 19 | void print_array(int *a, int n); 20 | 21 | char *_strcpy(char *dest, char *src); 22 | 23 | int _atoi(char *s); 24 | 25 | char *_strcat(char *dest, char *src); 26 | 27 | char *_strncat(char *dest, char *src, int n); 28 | 29 | char *_strncpy(char *dest, char *src, int n); 30 | 31 | int _strcmp(char *s1, char *s2); 32 | 33 | void reverse_array(int *a, int n); 34 | 35 | char *string_toupper(char *a); 36 | 37 | char *cap_string(char *string); 38 | 39 | char *leet(char *); 40 | 41 | char *_memset(char *s, char b, unsigned int n); 42 | 43 | char *_memcpy(char *dest, char *src, unsigned int n); 44 | 45 | char *_strchr(char *s, char c); 46 | 47 | unsigned int _strspn(char *s, char *accept); 48 | 49 | void print_chessboard(char (*a)[8]); 50 | 51 | void print_diagsums(int *a, int size); 52 | 53 | unsigned int _strspn(char *s, char *accept); 54 | 55 | char *_strpbrk(char *s, char *accept); 56 | 57 | char *_strstr(char *haystack, char *needle); 58 | -------------------------------------------------------------------------------- /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 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 | -------------------------------------------------------------------------------- /preprocessor/0-object_like_macro.h: -------------------------------------------------------------------------------- 1 | #ifndef SIZE 2 | 3 | #define SIZE 1024 4 | 5 | #endif 6 | -------------------------------------------------------------------------------- /preprocessor/1-main.c: -------------------------------------------------------------------------------- 1 | #include "1-pi.h" 2 | #include "1-pi.h" 3 | #include 4 | 5 | /** 6 | * main - check the code 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 | -------------------------------------------------------------------------------- /preprocessor/1-pi.h: -------------------------------------------------------------------------------- 1 | #ifndef PI 2 | 3 | #define PI 3.14159265359 4 | 5 | #endif 6 | -------------------------------------------------------------------------------- /preprocessor/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | printf("%s\n", __FILE__); 6 | 7 | return (0); 8 | } 9 | -------------------------------------------------------------------------------- /preprocessor/3-function_like_macro.h: -------------------------------------------------------------------------------- 1 | #ifndef FUNCTION_ABSX_H 2 | #define FUNCTION_ABSX_H 3 | #define ABS(x) ((x < 0) ? -(x) : (x)) 4 | #endif 5 | -------------------------------------------------------------------------------- /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 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 | -------------------------------------------------------------------------------- /preprocessor/4-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "4-sum.h" 3 | #include "4-sum.h" 4 | 5 | /** 6 | * main - check the code 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 | -------------------------------------------------------------------------------- /preprocessor/4-sum.h: -------------------------------------------------------------------------------- 1 | #ifndef TASK_4_SUM 2 | #define TASK_4_SUM 3 | 4 | #define SUM(x, y) (x + y) 5 | #endif 6 | -------------------------------------------------------------------------------- /preprocessor/README.md: -------------------------------------------------------------------------------- 1 | Tasks 2 | 0. Object-like Macro 3 | mandatory 4 | Create a header file that defines a macro named SIZE as an abbreviation for the token 1024. 5 | 6 | 1. Pi 7 | mandatory 8 | Create a header file that defines a macro named PI as an abbreviation for the token 3.14159265359. 9 | 10 | 2. File name 11 | mandatory 12 | Write a program that prints the name of the file it was compiled from, followed by a new line. 13 | 14 | You are allowed to use the standard library 15 | 16 | 3. Function-like macro 17 | mandatory 18 | Write a function-like macro ABS(x) that computes the absolute value of a number x. 19 | 20 | 4. SUM 21 | mandatory 22 | Write a function-like macro SUM(x, y) that computes the sum of the numbers x and y. 23 | -------------------------------------------------------------------------------- /preprocessor/_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 | -------------------------------------------------------------------------------- /preprocessor/a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/preprocessor/a -------------------------------------------------------------------------------- /preprocessor/b: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/preprocessor/b -------------------------------------------------------------------------------- /preprocessor/c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/preprocessor/c -------------------------------------------------------------------------------- /preprocessor/d: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/preprocessor/d -------------------------------------------------------------------------------- /preprocessor/e: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/preprocessor/e -------------------------------------------------------------------------------- /recursion/0-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - check the code 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | _puts_recursion("Puts with recursion"); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /recursion/0-puts_recursion: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/recursion/0-puts_recursion -------------------------------------------------------------------------------- /recursion/0-puts_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _puts_recursion - Prints a string using recursion 5 | * @s: Pointer to the string 6 | */ 7 | 8 | void _puts_recursion(char *s) 9 | { 10 | if (*s == '\0') 11 | { 12 | _putchar('\n'); 13 | } 14 | else 15 | { 16 | _putchar(*s); 17 | _puts_recursion(s + 1); 18 | } 19 | } 20 | 21 | -------------------------------------------------------------------------------- /recursion/1-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - check the code 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | _print_rev_recursion("\nColton Walker"); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /recursion/1-print_rev_recursion: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/recursion/1-print_rev_recursion -------------------------------------------------------------------------------- /recursion/1-print_rev_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _print_rev_recursion - prints a string in reverse 5 | * @s: the string to print 6 | */ 7 | 8 | void _print_rev_recursion(char *s) 9 | { 10 | if (*s == '\0') 11 | { 12 | } 13 | else 14 | { 15 | _print_rev_recursion(s + 1); 16 | _putchar(*s); 17 | } 18 | } 19 | 20 | -------------------------------------------------------------------------------- /recursion/100-is_palindrome.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * test_palindrome - Checks if a word is a palindrome. 6 | * @s: The input string to check. 7 | * @fin: The index of the last character in the string. 8 | * @debut: The index of the first character in the string. 9 | * 10 | * Return: 1 if the string is a palindrome, 0 otherwise. 11 | */ 12 | 13 | int test_palindrome(char *s, int fin, int debut) 14 | { 15 | if (debut >= fin) 16 | { 17 | return (1); 18 | } 19 | 20 | if (s[fin] == s[debut]) 21 | { 22 | return (test_palindrome(s, fin - 1, debut + 1)); 23 | } 24 | return (0); 25 | } 26 | 27 | /** 28 | * is_palindrome - Checks if a word is a palindrome. 29 | * @s: The input string to check. 30 | * 31 | * Return: 1 if the string is a palindrome, 0 otherwise. 32 | */ 33 | 34 | int is_palindrome(char *s) 35 | { 36 | int longueur = strlen(s) - 1; 37 | int i = 0; 38 | 39 | return (test_palindrome(s, longueur, i)); 40 | } 41 | 42 | -------------------------------------------------------------------------------- /recursion/100-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int r; 12 | 13 | r = is_palindrome("level"); 14 | printf("%d\n", r); 15 | r = is_palindrome("redder"); 16 | printf("%d\n", r); 17 | r = is_palindrome("test"); 18 | printf("%d\n", r); 19 | r = is_palindrome("step on no pets"); 20 | printf("%d\n", r); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /recursion/100-palindrome: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/recursion/100-palindrome -------------------------------------------------------------------------------- /recursion/101-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int 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 | -------------------------------------------------------------------------------- /recursion/101-wildcmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/recursion/101-wildcmp -------------------------------------------------------------------------------- /recursion/101-wildcmp.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * wildcmp - Compare two strings using wildcard characters. 6 | * 7 | * This function compares two strings, 'mot' and 'a_comparer' 8 | * 9 | * @mot: The source string to compare. 10 | * @a_comparer: The string to compare with, may contain '*'²:wq 11 | * 12 | * 13 | * Return: 1 if the strings match, 0 otherwise. 14 | */ 15 | 16 | 17 | int wildcmp(char *mot, char *a_comparer) 18 | { 19 | int ctr = 0, flag = 0; 20 | 21 | if (*mot == '\0' && *a_comparer == '\0') 22 | return (1); 23 | 24 | if (*mot == *a_comparer) 25 | return (wildcmp(++mot, ++a_comparer)); 26 | 27 | if (*a_comparer == '*') 28 | { 29 | while (*a_comparer == '*') 30 | a_comparer++; 31 | 32 | if (*a_comparer == '\0') 33 | return (1); 34 | 35 | while (*(mot + ctr) != '\0') 36 | { 37 | if (*(mot + ctr) == *a_comparer) 38 | flag = ctr; 39 | ctr++; 40 | } 41 | if (flag > 0) 42 | mot = mot + flag; 43 | return (wildcmp(mot, a_comparer)); 44 | } 45 | 46 | return (0); 47 | } 48 | 49 | -------------------------------------------------------------------------------- /recursion/2-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int n; 12 | 13 | n = _strlen_recursion("Corbin Coleman"); 14 | printf("%d\n", n); 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /recursion/2-strlen_recursion: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/recursion/2-strlen_recursion -------------------------------------------------------------------------------- /recursion/2-strlen_recursion.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * _strlen_recursion - print lenght of string 6 | * @s: the string to print 7 | * Return: lenght of string 8 | */ 9 | 10 | int _strlen_recursion(char *s) 11 | { 12 | if (s[0] == '\0') 13 | { 14 | return (0); 15 | } 16 | return ((1) + _strlen_recursion(s + 1)); 17 | } 18 | -------------------------------------------------------------------------------- /recursion/3-factorial: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/recursion/3-factorial -------------------------------------------------------------------------------- /recursion/3-factorial.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * factorial - return the factorial of a number 5 | * @n: number to calculate the factorial 6 | * 7 | * Return: factorial of the given number 8 | */ 9 | 10 | int factorial(int n) 11 | { 12 | if (n <= 0) 13 | { 14 | return (-1); 15 | } 16 | if (n == 1) 17 | { 18 | return (1); 19 | } 20 | return (n * factorial(n - 1)); 21 | } 22 | -------------------------------------------------------------------------------- /recursion/3-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int 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 | -------------------------------------------------------------------------------- /recursion/4-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int 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 | -------------------------------------------------------------------------------- /recursion/4-pow: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/recursion/4-pow -------------------------------------------------------------------------------- /recursion/4-pow_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _pow_recursion - calculate power of number recursively 5 | * @x: base number 6 | * @y: exponent 7 | * 8 | * Return: r = result 9 | */ 10 | 11 | int _pow_recursion(int x, int y) 12 | { 13 | if (y < 0) 14 | { 15 | return (-1); 16 | } 17 | if (y == 0) 18 | return (1); 19 | else if (y == 1) 20 | return (x); 21 | return (x * _pow_recursion(x, (y - 1))); 22 | } 23 | -------------------------------------------------------------------------------- /recursion/5-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int r; 12 | 13 | r = _sqrt_recursion(1); 14 | printf("%d\n", r); 15 | r = _sqrt_recursion(1024); 16 | printf("%d\n", r); 17 | r = _sqrt_recursion(16); 18 | printf("%d\n", r); 19 | r = _sqrt_recursion(17); 20 | printf("%d\n", r); 21 | r = _sqrt_recursion(25); 22 | printf("%d\n", r); 23 | r = _sqrt_recursion(-1); 24 | printf("%d\n", r); 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /recursion/5-sqrt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/recursion/5-sqrt -------------------------------------------------------------------------------- /recursion/5-sqrt_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * square_root - Calc. natural square root. 6 | * @n: Number to calculate square root. 7 | * @r: Recursion value. 8 | * Return: Square root or -1. 9 | */ 10 | 11 | int square_root(int n, int r) 12 | { 13 | if (r * r == n) 14 | { 15 | return (r); 16 | } 17 | else if (r * r < n) 18 | { 19 | return (square_root(n, r + 1)); 20 | } 21 | return (-1); 22 | } 23 | 24 | /** 25 | * _sqrt_recursion - Calculate square root. 26 | * @n: Number to find square root. 27 | * Return: Square root or -1. 28 | */ 29 | 30 | int _sqrt_recursion(int n) 31 | { 32 | 33 | if (n < 0) 34 | return (-1); 35 | return (square_root(n, 1)); 36 | } 37 | -------------------------------------------------------------------------------- /recursion/6-is_prime_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | int is_prime(int n, int r); 5 | 6 | /** 7 | * prime_number - Calc prime number 8 | * @n: Number to calculate 9 | * @r: denominator 10 | * Return: whether number is prime or not 11 | */ 12 | 13 | int prime_number(int n, int r) 14 | { 15 | if (r >= n && n > 1) 16 | return (1); 17 | else if (n % r == 0 || n <= 1) 18 | { 19 | return (0); 20 | } 21 | else 22 | return (prime_number(n, r + 1)); 23 | } 24 | 25 | /** 26 | * is_prime_number - check for prime number 27 | * @n: Number to check 28 | * Return: true = 1 otherwise = 0 29 | */ 30 | 31 | int is_prime_number(int n) 32 | { 33 | return (prime_number(n, 2)); 34 | } 35 | -------------------------------------------------------------------------------- /recursion/6-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int 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 | -------------------------------------------------------------------------------- /recursion/6-prime: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/recursion/6-prime -------------------------------------------------------------------------------- /recursion/README.md: -------------------------------------------------------------------------------- 1 | ![6480d9b63a806a1b35fec007_Holberton](https://github.com/chloe0524/holbertonschool-low_level_programming/assets/127857895/b53938c1-ac78-4645-9b19-dadb3de51a62) 2 | 3 | # Holberton School Simple Shell Project 4 | 5 | ## Description 6 | 7 | This is a custom implementation of a "simple shell" in C, created as part of the Holberton School curriculum. 8 | The project includes a set of functions 9 | ## :file_folder: Files 10 | 11 | - `main.h`: Header file containing function prototypes and necessary includes. 12 | - `simple_shell_main.c`: The main function of the shell. 13 | - `fill_argv.c`: Transform input characters into tokens. 14 | - `print_env.c`: Get the environnement for when you enter env. 15 | - `getenv.c`: `Getenv` function to get the path of the command input. 16 | - `exec_exists_cmd.c`: Implementation of the execute command associated to the path. 17 | - `shell_errors.c`: Implementation of supporting function for errors. 18 | - `man_1_simple_shell`: Manual page for the custom `simple_shell` programm. 19 | 20 | ##Functions and system calls used 21 | 22 | `execve`, `exit`, `fflush`, `fork`, `free`, `getline`, `getpid`, `isatty`, `malloc`, `perror`, `printf`, `fprintf`, `sprintf`, `putchar`, `signal`, `stat`, `strtok`, `wait`, `write` 23 | 24 | Environment 25 | :--------------------------------------------------: | 26 | Language: C 27 | OS: Ubuntu 20.04 LTS 28 | Compiler: gcc 29 | Style guidelines: Betty style 30 | 31 | ## Usage 32 | 33 | To use the custom `simple_shell` programm, include the `main.h` header file in your program and compile all the files associated. Then run the exectuable `./hsh`in our case so you enter in this simple shell. Then, you can use it as a normal shell. Moreover, this programm handle arguments commands such as *ls -l*, and also works in non-interactive (but be cautious, it only works with | for now). 34 | ```c 35 | ./hsh 36 | #simple_shell(*current_pid*)$ 37 | ``` 38 | 39 | ### Compilation 40 | ``` bash 41 | gcc -Wall -Werror -Wextra -pedantic -std=gnu89 *.c -o hsh 42 | ``` 43 | 44 | ### Man page 45 | ``` bash 46 | man ./man_1_simple_shell 47 | ``` 48 | ### :floppy_disk: Examples 49 | 50 | Display the list of directory content: 51 | ``` 52 | ./hsh 53 | #simple_shell(*current_pid*)$ ls 54 | AUTHORS getenv.c man_1_simple_shell shell_errors.c 55 | exec_exists_cmd.c hsh print_env.c simple_shell_main.c 56 | fill_argv.c main.h README.md 57 | #simple_shell(*current_pid*)$ 58 | ``` 59 | ---------- 60 | Display the list of directory content in a long listing format: 61 | ``` 62 | #simple_shell(*current_pid)$ ls -l 63 | total 64 64 | -rw-r--r-- 1 root root 172 Dec 12 00:09 AUTHORS 65 | -rw-r--r-- 1 root root 2596 Dec 22 07:38 exec_exists_cmd.c 66 | -rw-r--r-- 1 root root 847 Dec 22 07:38 fill_argv.c 67 | -rw-r--r-- 1 root root 513 Dec 20 00:06 getenv.c 68 | -rwxr-xr-x 1 root root 18400 Dec 22 07:47 hsh 69 | -rw-r--r-- 1 root root 513 Dec 22 07:38 main.h 70 | -rw-r--r-- 1 root root 5244 Dec 22 07:38 man_1_simple_shell 71 | -rw-r--r-- 1 root root 210 Dec 22 07:38 print_env.c 72 | -rw-r--r-- 1 root root 1971 Dec 22 07:55 README.md 73 | -rw-r--r-- 1 root root 174 Dec 22 07:38 shell_errors.c 74 | -rw-r--r-- 1 root root 1052 Dec 22 07:38 simple_shell_main.c 75 | #simple_shell(*current_pid*)$ 76 | ``` 77 | ---------- 78 | Non-interactive mode: 79 | ``` 80 | root@970d48fbae6b:/holbertonschool-simple_shell# echo ls | ./hsh 81 | AUTHORS getenv.c man_1_simple_shell shell_errors.c 82 | exec_exists_cmd.c hsh print_env.c simple_shell_main.c 83 | fill_argv.c main.h README.md 84 | root@970d48fbae6b:/holbertonschool-simple_shell# 85 | ``` 86 | ---------- 87 | Exit the programm: 88 | ``` 89 | #simple_shell(*current_pid*)$ exit 90 | root@970d48fbae6b:/holbertonschool-simple_shell# 91 | ``` 92 | ---------- 93 | 94 | ## Authors :pencil2: 95 | 96 | - Chloé Correia 97 | - Jonathan Ismaël 98 | - Nathan Raynal 99 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /recursion/main.h: -------------------------------------------------------------------------------- 1 | int _putchar(char c); 2 | 3 | void _puts_recursion(char *s); 4 | 5 | void _print_rev_recursion(char *s); 6 | 7 | int _strlen_recursion(char *s); 8 | 9 | int factorial(int n); 10 | 11 | int _pow_recursion(int x, int y); 12 | 13 | int _sqrt_recursion(int n); 14 | 15 | int is_prime_number(int n); 16 | 17 | int is_palindrome(char *s); 18 | 19 | int wildcmp(char *s1, char *s2); 20 | -------------------------------------------------------------------------------- /search_algorithms/0-linear.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * linear_search - searches for a value in an array using 5 | * Linear search algorithm 6 | * @array: pointer to 1st element of the array 7 | * @size: number of elements in array 8 | * @value: value to search for 9 | * 10 | * Return: index of value or -1 11 | */ 12 | 13 | int linear_search(int *array, size_t size, int value) 14 | { 15 | size_t i = 0; 16 | 17 | if (array == NULL) 18 | return (-1); 19 | 20 | for (i = 0; i < size; i++) 21 | { 22 | printf("Value checked array[%ld] = [%d]\n", i, array[i]); 23 | if (array[i] == value) 24 | return (i); 25 | } 26 | 27 | return (-1); 28 | } 29 | 30 | -------------------------------------------------------------------------------- /search_algorithms/1-binary.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * binary_search - searches for a value in a sorted integer array 5 | * @array: pointer to first element of array to search in 6 | * @size: number of elements in array 7 | * @value: value to search for 8 | * Return: index where value is located, or -1 if not found 9 | */ 10 | 11 | int binary_search(int *array, size_t size, int value) 12 | { 13 | int median = 0; 14 | size_t i = 0; 15 | size_t j = size - 1; 16 | 17 | if (array) 18 | while (i <= j) 19 | { 20 | print_array(array, i, j); 21 | 22 | median = (i + j) / 2; 23 | if (array[median] == value) 24 | return (median); 25 | if (array[median] < value) 26 | i = median + 1; 27 | else if (array[median] > value) 28 | j = median - 1; 29 | else 30 | return (median); 31 | } 32 | return (-1); 33 | } 34 | 35 | /** 36 | * print_array - Prints an array of integers 37 | * 38 | * @array: The array to be printed 39 | * @i: first element 40 | * @j: last element 41 | */ 42 | void print_array(int *array, int i, int j) 43 | { 44 | printf("Searching in array: "); 45 | while (i <= j) 46 | { 47 | printf("%d", array[i]); 48 | i++; 49 | if (i <= j) 50 | printf(", "); 51 | } 52 | printf("\n"); 53 | } 54 | -------------------------------------------------------------------------------- /search_algorithms/2-O: -------------------------------------------------------------------------------- 1 | O(n) 2 | -------------------------------------------------------------------------------- /search_algorithms/3-O: -------------------------------------------------------------------------------- 1 | O(1) 2 | -------------------------------------------------------------------------------- /search_algorithms/4-O: -------------------------------------------------------------------------------- 1 | O(log(n)) 2 | -------------------------------------------------------------------------------- /search_algorithms/5-O: -------------------------------------------------------------------------------- 1 | O(1) 2 | -------------------------------------------------------------------------------- /search_algorithms/6-O: -------------------------------------------------------------------------------- 1 | O(nm) 2 | -------------------------------------------------------------------------------- /search_algorithms/README.md: -------------------------------------------------------------------------------- 1 | 2 | # C - Search Algorithms 3 | 4 | ## Table of Contents 5 | 6 | - [Task 0: Linear search](#task-0-linear-search) 7 | - [Task 1: Binary search](#task-1-binary-search) 8 | - [Task 2: Big O #0](#task-2-big-o-0) 9 | - [Task 3: Big O #1](#task-3-big-o-1) 10 | - [Task 4: Big O #2](#task-4-big-o-2) 11 | - [Task 5: Big O #3](#task-5-big-o-3) 12 | - [Task 6: Big O #4](#task-6-big-o-4) 13 | 14 | ## Tasks 15 | 16 | ### Task 0: Linear search 17 | 18 | Write a function that searches for a value in an array of integers using the Linear search algorithm. 19 | 20 | ```c 21 | int linear_search(int *array, size_t size, int value); 22 | ``` 23 | 24 | - Where `array` is a pointer to the first element of the array to search. 25 | - `size` is the number of elements in the array. 26 | - `value` is the value to search for. 27 | - Return the first index where the value is located, or -1 if not present. 28 | 29 | ### Task 1: Binary search 30 | 31 | Write a function that searches for a value in a sorted array of integers using the Binary search algorithm. 32 | 33 | ```c 34 | int binary_search(int *array, size_t size, int value); 35 | ``` 36 | 37 | - Where `array` is a pointer to the first element of the array to search. 38 | - `size` is the number of elements in the array. 39 | - `value` is the value to search for. 40 | - Return the index where the value is located, or -1 if not present. 41 | 42 | ### Task 2: Big O #0 43 | 44 | Provide the time complexity (worst case) of a linear search in an array of size n. 45 | 46 | ### Task 3: Big O #1 47 | 48 | Provide the space complexity (worst case) of an iterative linear search algorithm in an array of size n. 49 | 50 | ### Task 4: Big O #2 51 | 52 | Provide the time complexity (worst case) of a binary search in an array of size n. 53 | 54 | ### Task 5: Big O #3 55 | 56 | Provide the space complexity (worst case) of a binary search in an array of size n. 57 | 58 | ### Task 6: Big O #4 59 | 60 | Provide the space complexity of: 61 | 62 | ```c 63 | int **allocate_map(int n, int m) 64 | { 65 | int **map; 66 | 67 | map = malloc(sizeof(int *) * n); 68 | for (size_t i = 0; i < n; i++) 69 | { 70 | map[i] = malloc(sizeof(int) * m); 71 | } 72 | return (map); 73 | } 74 | ``` 75 | -------------------------------------------------------------------------------- /search_algorithms/search_algos.h: -------------------------------------------------------------------------------- 1 | #ifndef ALGO_HEADER 2 | #define ALGO_HEADER 3 | 4 | #include 5 | #include 6 | 7 | void print_array(int *array, int i, int j); 8 | 9 | int linear_search(int *array, size_t size, int value); 10 | int binary_search(int *array, size_t size, int value); 11 | 12 | #endif /* ALGO_HEADER */ 13 | -------------------------------------------------------------------------------- /singly_linked_lists/0-print_list.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * print_list - prints all elements of list list_t 5 | * @h: pointer to head of list. 6 | * 7 | * Return: number of nodes 8 | */ 9 | size_t print_list(const list_t *h) 10 | { 11 | size_t i; 12 | 13 | for (i = 0; h != NULL; i++) 14 | { 15 | if (h->str == NULL) 16 | printf("[0] (nil)\n"); 17 | else 18 | printf("[%u] %s\n", h->len, h->str); 19 | 20 | h = h->next; 21 | } 22 | 23 | return (i); 24 | } 25 | -------------------------------------------------------------------------------- /singly_linked_lists/1-list_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * list_len - prints number of elements list_t 5 | * @h: pointer to head of list. 6 | * 7 | * Return: number of elements 8 | */ 9 | size_t list_len(const list_t *h) 10 | { 11 | size_t i; 12 | 13 | for (i = 0; h != NULL; i++) 14 | { 15 | if (h->str == NULL) 16 | printf("[0] (nil)\n"); 17 | else 18 | 19 | h = h->next; 20 | } 21 | 22 | return (i); 23 | } 24 | -------------------------------------------------------------------------------- /singly_linked_lists/2-add_node.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_node - adds new node at beginning of list list_t 5 | * @head: pointer to a pointer oe head of list 6 | * @str: string to be duplicated 7 | * 8 | * Return: address of new element or NULL 9 | */ 10 | list_t *add_node(list_t **head, const char *str) 11 | { 12 | list_t *other_node; 13 | 14 | other_node = malloc(sizeof(list_t)); 15 | while (head != NULL && str != NULL) 16 | { 17 | if (other_node == NULL) 18 | return (NULL); 19 | 20 | other_node->str = strdup(str); 21 | if (other_node->str == NULL) 22 | { 23 | free(other_node); 24 | return (NULL); 25 | } 26 | 27 | other_node->len = strlen(str); 28 | other_node->next = *head; 29 | *head = other_node; 30 | 31 | return (other_node); 32 | } 33 | return (NULL); 34 | } 35 | 36 | 37 | -------------------------------------------------------------------------------- /singly_linked_lists/3-add_node_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_node_end - adds new node at the end of list list_t 5 | * @head: pointer to a pointer oe head of list 6 | * @str: string to be duplicated 7 | * 8 | * Return: address of new element or NULL 9 | */ 10 | 11 | list_t *add_node_end(list_t **head, const char *str) 12 | { 13 | list_t *other_node; 14 | list_t *end_node; 15 | 16 | while (!(other_node = malloc(sizeof(list_t))) || !head || !str) 17 | { 18 | free(other_node); 19 | return (NULL); 20 | } 21 | 22 | other_node->str = strdup(str); 23 | if (!other_node->str) 24 | { 25 | free(other_node); 26 | return (NULL); 27 | } 28 | 29 | other_node->len = strlen(str); 30 | other_node->next = NULL; 31 | 32 | if (*head != NULL) 33 | { 34 | end_node = *head; 35 | while (end_node->next) 36 | { 37 | end_node = end_node->next; 38 | } 39 | end_node->next = other_node; 40 | } 41 | else 42 | { 43 | *head = other_node; 44 | } 45 | 46 | return (other_node); 47 | } 48 | -------------------------------------------------------------------------------- /singly_linked_lists/4-free_list.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_list - frees memory occupied by linked list 5 | * @head: pointer to head of linked list. 6 | */ 7 | 8 | void free_list(list_t *head) 9 | { 10 | if (head) 11 | { 12 | free_list(head->next); 13 | free(head->str); 14 | free(head); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /singly_linked_lists/README.md: -------------------------------------------------------------------------------- 1 | # :spiral_notepad: C- Singly Linked Lists 2 | 3 | ![owgc2k3v0yz91](https://github.com/chloe0524/holbertonschool-low_level_programming/assets/127857895/030a6bbc-4566-436e-ba27-0b163203a65d) 4 | 5 | - [0. Print list](#0-print-list-mandatory) 6 | - [1. List length](#1-list-length-mandatory) 7 | - [2. Add node](#2-add-node-mandatory) 8 | - [3. Add node at the end](#3-add-node-at-the-end-mandatory) 9 | - [4. Free list](#4-free-list-mandatory) 10 | 11 | ## 0. Print list (mandatory) 12 | 13 | Write a function that prints all the elements of a `list_t` list. 14 | 15 | - **Prototype:** `size_t print_list(const list_t *h);` 16 | - **Return:** the number of nodes 17 | - **Format:** see example 18 | - If `str` is `NULL`, print `[0] (nil)` 19 | - You are allowed to use `printf` 20 | 21 | ## 1. List length (mandatory) 22 | 23 | Write a function that returns the number of elements in a `list_t` linked list. 24 | 25 | - **Prototype:** `size_t list_len(const list_t *h);` 26 | 27 | ## 2. Add node (mandatory) 28 | 29 | Write a function that adds a new node at the beginning of a `list_t` list. 30 | 31 | - **Prototype:** `list_t *add_node(list_t **head, const char *str);` 32 | - **Return:** the address of the new element, or `NULL` if it failed 33 | - `str` needs to be duplicated 34 | - You are allowed to use `strdup` 35 | 36 | ## 3. Add node at the end (mandatory) 37 | 38 | Write a function that adds a new node at the end of a `list_t` list. 39 | 40 | - **Prototype:** `list_t *add_node_end(list_t **head, const char *str);` 41 | - **Return:** the address of the new element, or `NULL` if it failed 42 | - `str` needs to be duplicated 43 | - You are allowed to use `strdup` 44 | 45 | ## 4. Free list (mandatory) 46 | 47 | Write a function that frees a `list_t` list. 48 | 49 | - **Prototype:** `void free_list(list_t *head);` 50 | 51 | -------------------------------------------------------------------------------- /singly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef LISTS_HEADER_H 2 | #define LISTS_HEADER_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include "lists.h" 9 | 10 | /** 11 | * struct list_s - singly linked list 12 | * @str: string - (malloc'ed string) 13 | * @len: length of the string 14 | * @next: points to the next node 15 | * 16 | * Description: singly linked list node structure 17 | */ 18 | typedef struct list_s 19 | { 20 | char *str; 21 | unsigned int len; 22 | struct list_s *next; 23 | } list_t; 24 | 25 | int _putchar(char c); 26 | size_t print_list(const list_t *h); 27 | size_t list_len(const list_t *h); 28 | list_t *add_node(list_t **head, const char *str); 29 | list_t *add_node_end(list_t **head, const char *str); 30 | void free_list(list_t *head); 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /static_libraries/0-isupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isupper - checks if letter is uppercase 5 | * 6 | * @c: the character to check 7 | * 8 | * Return: 1 if c is a uppercase letter, return 0 otherwise 9 | */ 10 | int _isupper(int c) 11 | { 12 | if (c >= 'A' && c <= 'Z') 13 | return (1); 14 | else 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /static_libraries/0-isupper.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/static_libraries/0-isupper.o -------------------------------------------------------------------------------- /static_libraries/0-memset.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * *_memset - fill memory with constant byte 4 | *@s: memory area 5 | *@b: constant byte 6 | *@n: n bytes from memory 7 | *Return: memory area pointed to 8 | */ 9 | 10 | char *_memset(char *s, char b, unsigned int n) 11 | { 12 | unsigned int i; 13 | 14 | for (i = 0; n > 0; i++, n--) 15 | { 16 | s[i] = b; 17 | } 18 | return (s); 19 | } 20 | -------------------------------------------------------------------------------- /static_libraries/0-memset.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/static_libraries/0-memset.o -------------------------------------------------------------------------------- /static_libraries/0-strcat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _strcat - Concatenate two strings. 6 | * @dest: The destination string to which the source will be appended. 7 | * @src: The source string that will be appended to the destination. 8 | * 9 | * Return: A pointer to the destination string (dest). 10 | */ 11 | 12 | char *_strcat(char *dest, char *src) 13 | { 14 | int i = 0; 15 | int j = 0; 16 | 17 | while (dest[i] != '\0') 18 | { 19 | i++; 20 | } 21 | 22 | while (src[j] != '\0') 23 | { 24 | dest[i] = src[j]; 25 | i++; 26 | j++; 27 | } 28 | 29 | dest[i] = '\0'; 30 | 31 | return (dest); 32 | } 33 | 34 | -------------------------------------------------------------------------------- /static_libraries/0-strcat.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/static_libraries/0-strcat.o -------------------------------------------------------------------------------- /static_libraries/1-isdigit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isdigit - print digit of c 5 | *@c: integer with digit printed 6 | * Return: 1 and 0 7 | */ 8 | int _isdigit(int c) 9 | { 10 | if (c >= '0' && c <= '9') 11 | { 12 | return (1); 13 | } 14 | else 15 | { 16 | return (0); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /static_libraries/1-isdigit.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/static_libraries/1-isdigit.o -------------------------------------------------------------------------------- /static_libraries/1-memcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * *_memcpy - Copy string 4 | *@dest: destination 5 | *@src: source 6 | *@n: n bytes from memory 7 | *Return: dest 8 | */ 9 | 10 | char *_memcpy(char *dest, char *src, unsigned int n) 11 | { 12 | unsigned int i; 13 | 14 | for (i = 0; i < n; i++) 15 | { 16 | dest[i] = src[i]; 17 | } 18 | return (dest); 19 | } 20 | -------------------------------------------------------------------------------- /static_libraries/1-memcpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/static_libraries/1-memcpy.o -------------------------------------------------------------------------------- /static_libraries/1-strncat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strncat - Concatenate 2 strings 5 | * @dest: The destination string to which the source will be appended. 6 | * @src: The source string that will be appended to the destination. 7 | * @n: The maximum number of characters to append from src. 8 | * 9 | * Return: A pointer to the resulting string (dest). 10 | */ 11 | 12 | char *_strncat(char *dest, char *src, int n) 13 | { 14 | int i = 0; 15 | int j = 0; 16 | 17 | 18 | while (dest[i] != '\0') 19 | { 20 | i++; 21 | } 22 | 23 | 24 | while (src[j] != '\0' && j < n) 25 | { 26 | dest[i] = src[j]; 27 | i++; 28 | j++; 29 | } 30 | 31 | dest[i] = '\0'; 32 | 33 | return (dest); 34 | } 35 | 36 | -------------------------------------------------------------------------------- /static_libraries/1-strncat.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/static_libraries/1-strncat.o -------------------------------------------------------------------------------- /static_libraries/100-atoi.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | 5 | /** 6 | * _atoi - Converts a string to an integer, considering negative values. 7 | * @nombre: The input string to convert. 8 | * Return: The converted integer value, or 0 if conversion fails. 9 | */ 10 | 11 | int _atoi(char *nombre) 12 | { 13 | int i; 14 | int negatif = 1; 15 | unsigned int entier = 0; 16 | int traitementfait = 0; 17 | 18 | for (i = 0; nombre[i] != 0; i++) 19 | { 20 | if (nombre[i] == '-') 21 | negatif = negatif * -1; 22 | 23 | if (nombre[i] >= '0' && nombre[i] <= '9') 24 | { 25 | entier = (entier * 10) + nombre[i] - '0'; 26 | traitementfait = 1; 27 | } 28 | else if (traitementfait == 1) 29 | break; 30 | } 31 | 32 | entier = entier * negatif; 33 | return (entier); 34 | } 35 | -------------------------------------------------------------------------------- /static_libraries/100-atoi.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/static_libraries/100-atoi.o -------------------------------------------------------------------------------- /static_libraries/2-strchr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _strchr - locates a character in a string 6 | * 7 | * @s: string to check 8 | * @c: character we are trying to find 9 | * 10 | * Return: pointer to first time display @c in the string 11 | * @s. Return NULL if the character isn't found 12 | */ 13 | 14 | char *_strchr(char *s, char c) 15 | { 16 | while (*s) 17 | { 18 | if (*s != c) 19 | s++; 20 | else 21 | return (s); 22 | } 23 | if (c == '\0') 24 | return (s); 25 | 26 | return ('\0'); 27 | } 28 | -------------------------------------------------------------------------------- /static_libraries/2-strchr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/static_libraries/2-strchr.o -------------------------------------------------------------------------------- /static_libraries/2-strlen.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _strlen - find length 5 | * @s: char for lenght 6 | * Return: 0 7 | */ 8 | 9 | int _strlen(char *s) 10 | { 11 | int i = 0; 12 | 13 | for (; *s != '\0'; s++) 14 | { 15 | i++; 16 | } 17 | return (i); 18 | } 19 | -------------------------------------------------------------------------------- /static_libraries/2-strlen.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/static_libraries/2-strlen.o -------------------------------------------------------------------------------- /static_libraries/2-strncpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strncpy - Copy a string with a max length 5 | * 6 | * @dest: The destination string where copy will be stored. 7 | * @src: The source string to copy from. 8 | * @n: ma number of characters to copy from src 9 | * 10 | * Return: dest 11 | */ 12 | 13 | char *_strncpy(char *dest, char *src, int n) 14 | { 15 | int i; 16 | 17 | for (i = 0; i < n && src[i] != '\0'; i++) 18 | { 19 | dest[i] = src[i]; 20 | } 21 | for (; n > i; i++) 22 | { 23 | dest[i] = '\0'; 24 | } 25 | 26 | return (dest); 27 | } 28 | -------------------------------------------------------------------------------- /static_libraries/2-strncpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/static_libraries/2-strncpy.o -------------------------------------------------------------------------------- /static_libraries/3-islower.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _islower - checks if a character is lowercase 5 | * 6 | * @c: the character to check 7 | * 8 | * Return: 1 if c lowercase, return 0 otherwise 9 | */ 10 | 11 | int _islower(int c) 12 | { 13 | if (c >= 'a' && c <= 'z') 14 | { 15 | return (1); 16 | } 17 | else 18 | { 19 | return (0); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /static_libraries/3-islower.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/static_libraries/3-islower.o -------------------------------------------------------------------------------- /static_libraries/3-puts.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _puts - print the string 5 | * @str: char to print string 6 | * Return: nothing 7 | */ 8 | 9 | void _puts(char *str) 10 | { 11 | for (; *str != '\0'; str++) 12 | { 13 | _putchar(*str); 14 | } 15 | 16 | _putchar('\n'); 17 | } 18 | -------------------------------------------------------------------------------- /static_libraries/3-puts.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/static_libraries/3-puts.o -------------------------------------------------------------------------------- /static_libraries/3-strcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _strcmp - Compare two strings. 6 | * @s1: first string to compare. 7 | * @s2: second string to compare. 8 | * 9 | * Return: 0 if the strings are equal. 10 | * . 11 | */ 12 | 13 | int _strcmp(char *s1, char *s2) 14 | { 15 | int i = 0; 16 | 17 | while (s1[i] != '\0' && s2[i] != '\0') 18 | { 19 | if (s1[i] != s2[i]) 20 | { 21 | return (s1[i] - s2[i]); 22 | } 23 | i++; 24 | } 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /static_libraries/3-strcmp.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/static_libraries/3-strcmp.o -------------------------------------------------------------------------------- /static_libraries/3-strspn.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strspn - length of a prefix substring made of chars in accept 5 | * 6 | * @s: string to search. 7 | * @accept: characters to include in the substring. 8 | * 9 | * Return: number of bytes in initial segment made of bytes from accept 10 | */ 11 | unsigned int _strspn(char *s, char *accept) 12 | { 13 | int i = 0; 14 | int j; 15 | 16 | while (s[i] != '\0') 17 | { 18 | if (s[i] == ' ') 19 | break; 20 | 21 | for (j = 0; accept[j] != '\0'; j++) 22 | { 23 | if (s[i] == accept[j]) 24 | break; 25 | } 26 | 27 | if (accept[j] == '\0') 28 | break; 29 | 30 | i++; 31 | } 32 | 33 | return (i); 34 | } 35 | 36 | -------------------------------------------------------------------------------- /static_libraries/3-strspn.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/static_libraries/3-strspn.o -------------------------------------------------------------------------------- /static_libraries/4-isalpha.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isalpha - checks if a character is a letter 5 | * 6 | * @c: the character to check 7 | * 8 | * Return: 1 if c is a letter, return 0 otherwise 9 | */ 10 | int _isalpha(int c) 11 | { 12 | if (c >= 'a' && c <= 'z') 13 | return (1); 14 | else if (c >= 'A' && c <= 'Z') 15 | return (1); 16 | else 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /static_libraries/4-isalpha.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/static_libraries/4-isalpha.o -------------------------------------------------------------------------------- /static_libraries/4-strpbrk.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _strpbrk - locates a character in a string 6 | * 7 | * @s: string to check 8 | * @accept: characters to search for 9 | * 10 | * Return: pointer to byte in @s that matches one byte in @accept 11 | */ 12 | char *_strpbrk(char *s, char *accept) 13 | { 14 | int i; 15 | 16 | while (*s) 17 | { 18 | 19 | for (i = 0; accept[i]; i++) 20 | { 21 | 22 | if (*s == accept[i]) 23 | { 24 | 25 | return (s); 26 | } 27 | } 28 | s++; 29 | } 30 | return ('\0'); 31 | } 32 | -------------------------------------------------------------------------------- /static_libraries/4-strpbrk.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/static_libraries/4-strpbrk.o -------------------------------------------------------------------------------- /static_libraries/5-strstr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _strstr - locates a substring 6 | * @haystack: the string to search 7 | * @needle: the substring to find 8 | * 9 | * Return: pointer to the beginning of the located substring 10 | */ 11 | char *_strstr(char *haystack, char *needle) 12 | { 13 | while (*haystack) 14 | { 15 | int i = 0; 16 | int j = 0; 17 | 18 | while (needle[j] && (haystack[i] == needle[j])) 19 | { 20 | i++; 21 | j++; 22 | } 23 | 24 | if (!needle[j]) 25 | { 26 | return (haystack); 27 | } 28 | 29 | haystack++; 30 | } 31 | 32 | return ('\0'); 33 | } 34 | 35 | -------------------------------------------------------------------------------- /static_libraries/5-strstr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/static_libraries/5-strstr.o -------------------------------------------------------------------------------- /static_libraries/6-abs.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _abs - computes the absolute value of an integer 5 | * 6 | * @n: the character to check 7 | * 8 | * Return: n or -n 9 | */ 10 | int _abs(int n) 11 | { 12 | if (n >= 0) 13 | return (n); 14 | return (-n); 15 | } 16 | -------------------------------------------------------------------------------- /static_libraries/6-abs.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/static_libraries/6-abs.o -------------------------------------------------------------------------------- /static_libraries/9-strcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strcpy - Copy paste string 4 | *@dest: destination 5 | *@src: source 6 | *Return: dest 7 | */ 8 | char *_strcpy(char *dest, char *src) 9 | { 10 | int i = 0; 11 | 12 | while (*(src + i) != '\0') 13 | { 14 | *(dest + i) = *(src + i); 15 | i++; 16 | } 17 | *(dest + i) = '\0'; 18 | return (dest); 19 | } 20 | -------------------------------------------------------------------------------- /static_libraries/9-strcpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/static_libraries/9-strcpy.o -------------------------------------------------------------------------------- /static_libraries/README.md: -------------------------------------------------------------------------------- 1 | # holbertonschool-low_level_programming 2 | C 3 | -------------------------------------------------------------------------------- /static_libraries/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _putchar - writes the character c to stdout 5 | * @c: The character to print 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | int _putchar(char c) 11 | { 12 | return (write(1, &c, 1)); 13 | } 14 | -------------------------------------------------------------------------------- /static_libraries/_putchar.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/static_libraries/_putchar.o -------------------------------------------------------------------------------- /static_libraries/create_static_lib.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | gcc -c *.c 4 | 5 | ar -crs liball.a *.o 6 | -------------------------------------------------------------------------------- /static_libraries/liball.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/static_libraries/liball.a -------------------------------------------------------------------------------- /static_libraries/libmy.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/static_libraries/libmy.a -------------------------------------------------------------------------------- /static_libraries/main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | int main(void) 4 | { 5 | _puts("\"At the end of the day, my goal was to be the best hacker\"\n\t- Kevin Mitnick"); 6 | return (0); 7 | } 8 | -------------------------------------------------------------------------------- /static_libraries/main.h: -------------------------------------------------------------------------------- 1 | #ifndef _MAIN_H_ 2 | #define _MAIN_H_ 3 | 4 | int _putchar(char c); 5 | int _islower(int c); 6 | int _isalpha(int c); 7 | int _abs(int n); 8 | int _isupper(int c); 9 | int _isdigit(int c); 10 | int _strlen(char *s); 11 | void _puts(char *s); 12 | char *_strcpy(char *dest, char *src); 13 | int _atoi(char *s); 14 | char *_strcat(char *dest, char *src); 15 | char *_strncat(char *dest, char *src, int n); 16 | char *_strncpy(char *dest, char *src, int n); 17 | int _strcmp(char *s1, char *s2); 18 | char *_memset(char *s, char b, unsigned int n); 19 | char *_memcpy(char *dest, char *src, unsigned int n); 20 | char *_strchr(char *s, char c); 21 | unsigned int _strspn(char *s, char *accept); 22 | char *_strpbrk(char *s, char *accept); 23 | char *_strstr(char *haystack, char *needle); 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /static_libraries/main.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/static_libraries/main.o -------------------------------------------------------------------------------- /static_libraries/quote: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/static_libraries/quote -------------------------------------------------------------------------------- /structures_typedef/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | struct dog my_dog; 12 | 13 | my_dog.name = "Poppy"; 14 | my_dog.age = 3.5; 15 | my_dog.owner = "Bob"; 16 | printf("My name is %s, and I am %.1f :) - Woof!\n", my_dog.name, my_dog.age); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /structures_typedef/1-init_dog.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | 4 | /** 5 | * init_dog - Initializes a dog structure 6 | * @d: Pointer to dog struct 7 | * @name: Dog's name 8 | * @age: Dog's age 9 | * @owner: Owner's name 10 | */ 11 | 12 | void init_dog(struct dog *d, char *name, float age, char *owner) 13 | { 14 | if (d == NULL) 15 | return; 16 | { 17 | d->name = name; 18 | d->age = age; 19 | d->owner = owner; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /structures_typedef/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | struct dog my_dog; 12 | 13 | init_dog(&my_dog, "Poppy", 3.5, "Bob"); 14 | printf("My name is %s, and I am %.1f :) - Woof!\n", my_dog.name, my_dog.age); 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /structures_typedef/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | struct dog my_dog; 12 | 13 | my_dog.name = "Poppy"; 14 | my_dog.age = 3.5; 15 | my_dog.owner = "Bob"; 16 | print_dog(&my_dog); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /structures_typedef/2-print_dog.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "dog.h" 4 | 5 | /** 6 | * print_dog - Prints dog info. 7 | * @d: Pointer to dog struct. 8 | */ 9 | 10 | void print_dog(struct dog *d) 11 | { 12 | if (d == NULL) 13 | return; 14 | 15 | if (d->name == NULL) 16 | { 17 | printf("Name: (nil)\n"); 18 | } 19 | else 20 | { 21 | printf("Name: %s\n", d->name); 22 | } 23 | 24 | printf("Age: %f\n", d->age); 25 | 26 | if (d->owner == NULL) 27 | { 28 | printf("Owner: (nil)\n"); 29 | } 30 | else 31 | { 32 | printf("Owner: %s\n", d->owner); 33 | } 34 | } 35 | 36 | 37 | -------------------------------------------------------------------------------- /structures_typedef/3-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | dog_t my_dog; 12 | 13 | my_dog.name = "Poppy"; 14 | my_dog.age = 3.5; 15 | my_dog.owner = "Bob"; 16 | printf("My name is %s, and I am %.1f :) - Woof!\n", my_dog.name, my_dog.age); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /structures_typedef/4-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | dog_t *my_dog; 12 | 13 | my_dog = new_dog("Poppy", 3.5, "Bob"); 14 | printf("My name is %s, and I am %.1f :) - Woof!\n", my_dog->name, my_dog->age); 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /structures_typedef/4-new_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | 4 | /** 5 | * _strcopy - copy read-only data to mutable. 6 | * @dst: pointer to copy char to. 7 | * @src: read-only data. 8 | */ 9 | void _strcopy(char *dst, char *src) 10 | { 11 | int i; 12 | 13 | for (i = 0; src[i]; i++) 14 | dst[i] = src[i]; 15 | dst[i] = '\0'; 16 | } 17 | 18 | /** 19 | * new_dog - create a new dog. 20 | * @name: name of the dog 21 | * @age: age of the dog 22 | * @owner: owner of the dog 23 | * 24 | * Return: pointer to the new dog 25 | */ 26 | 27 | dog_t *new_dog(char *name, float age, char *owner) 28 | { 29 | dog_t *ouaoua; 30 | int a, b; 31 | 32 | 33 | for (a = 0; name[a]; a++) 34 | ; 35 | for (b = 0; owner[b]; b++) 36 | ; 37 | 38 | 39 | ouaoua = malloc(sizeof(dog_t)); 40 | if (ouaoua == NULL) 41 | return (NULL); 42 | 43 | 44 | ouaoua->name = malloc(a + 1); 45 | ouaoua->owner = malloc(b + 1); 46 | 47 | if (ouaoua->name == NULL || ouaoua->owner == NULL) 48 | { 49 | 50 | free(ouaoua->name); 51 | free(ouaoua->owner); 52 | free(ouaoua); 53 | return (NULL); 54 | } 55 | 56 | 57 | _strcopy(ouaoua->name, name); 58 | _strcopy(ouaoua->owner, owner); 59 | ouaoua->age = age; 60 | 61 | return (ouaoua); 62 | } 63 | -------------------------------------------------------------------------------- /structures_typedef/5-free_dog.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | /** 4 | * free_dog - free dog 5 | * 6 | * @d: dog 7 | * 8 | * Return: void 9 | */ 10 | void free_dog(dog_t *d) 11 | { 12 | if (d == 0) 13 | return; 14 | if (d->name) 15 | free(d->name); 16 | if (d->owner) 17 | free(d->owner); 18 | free(d); 19 | } 20 | -------------------------------------------------------------------------------- /structures_typedef/5-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | dog_t *my_dog; 12 | 13 | my_dog = new_dog("Poppy", 3.5, "Bob"); 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 | -------------------------------------------------------------------------------- /structures_typedef/README.md: -------------------------------------------------------------------------------- 1 | Tasks 2 | 0. Poppy 3 | Define a new type struct dog with the following elements: 4 | 5 | name, type = char * 6 | age, type = float 7 | owner, type = char * 8 | 9 | 10 | 1. A dog is the only thing on earth that loves you more than you love yourself 11 | mandatory 12 | Write a function that initialize a variable of type struct dog 13 | 14 | Prototype: void init_dog(struct dog *d, char *name, float age, char *owner); 15 | 16 | 2. A dog will teach you unconditional love. If you can have that in your life, things won't be too bad 17 | Write a function that prints a struct dog 18 | 19 | Prototype: void print_dog(struct dog *d); 20 | Format: see example bellow 21 | You are allowed to use the standard library 22 | If an element of d is NULL, print (nil) instead of this element. (if name is NULL, print Name: (nil)) 23 | If d is NULL print nothing. 24 | 25 | 3. Outside of a dog, a book is a man's best friend. Inside of a dog it's too dark to read 26 | 27 | Define a new type dog_t as a new name for the type struct dog. 28 | 29 | 4. A door is what a dog is perpetually on the wrong side of 30 | Write a function that creates a new dog. 31 | 32 | Prototype: dog_t *new_dog(char *name, float age, char *owner); 33 | You have to store a copy of name and owner 34 | Return NULL if the function fails 35 | 36 | 5. How many legs does a dog have if you call his tail a leg? Four. Saying that a tail is a leg doesn't make it a leg 37 | 38 | Write a function that frees dogs. 39 | 40 | Prototype: void free_dog(dog_t *d); 41 | -------------------------------------------------------------------------------- /structures_typedef/a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/structures_typedef/a -------------------------------------------------------------------------------- /structures_typedef/b: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/structures_typedef/b -------------------------------------------------------------------------------- /structures_typedef/c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/structures_typedef/c -------------------------------------------------------------------------------- /structures_typedef/c*: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/structures_typedef/c* -------------------------------------------------------------------------------- /structures_typedef/d: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/structures_typedef/d -------------------------------------------------------------------------------- /structures_typedef/dog.h: -------------------------------------------------------------------------------- 1 | #ifndef DOG 2 | #define DOG 3 | 4 | /** 5 | * struct dog - intializes a variable of type struct dog 6 | * @name: the name of the dog 7 | * @age: the age of the dog 8 | * @owner: the owner of the dog 9 | * 10 | * description: 11 | * Defines a structure 'struct_dog' to store information about a dog, 12 | * including its name, age, and owner. 13 | * Return: nothing 14 | */ 15 | struct dog 16 | { 17 | char *name; 18 | float age; 19 | char *owner; 20 | }; 21 | 22 | typedef struct dog dog_t; 23 | 24 | void init_dog(struct dog *d, char *name, float age, char *owner); 25 | 26 | void print_dog(struct dog *d); 27 | 28 | dog_t *new_dog(char *name, float age, char *owner); 29 | 30 | void free_dog(dog_t *d); 31 | 32 | #endif /* DOG */ 33 | -------------------------------------------------------------------------------- /structures_typedef/e: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/structures_typedef/e -------------------------------------------------------------------------------- /structures_typedef/f: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/structures_typedef/f -------------------------------------------------------------------------------- /variables_if_else_while/0-positive_or_negative: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/variables_if_else_while/0-positive_or_negative -------------------------------------------------------------------------------- /variables_if_else_while/0-positive_or_negative.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always 0 (Success) 9 | */ 10 | int main(void) 11 | { 12 | int n; 13 | 14 | srand(time(0)); 15 | n = rand() - RAND_MAX / 2; 16 | 17 | if (n > 0) 18 | { 19 | printf("%d is positive\n", n); 20 | } 21 | else if (n == 0) 22 | { 23 | printf("%d is zero\n", n); 24 | } 25 | else 26 | { 27 | printf("%d is negative\n", n); 28 | } 29 | 30 | return (0); 31 | } 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /variables_if_else_while/1-last_digit: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/variables_if_else_while/1-last_digit -------------------------------------------------------------------------------- /variables_if_else_while/1-last_digit.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | /** 5 | * main - Entry point 6 | * 7 | * Return: Always 0 (Success) 8 | */ 9 | int main(void) 10 | { 11 | int n; 12 | 13 | srand(time(0)); 14 | n = rand() - RAND_MAX / 2; 15 | 16 | if ((n % 10) > 5) 17 | { 18 | printf("Last digit of %d is %d and is greater than 5\n", 19 | n, n % 10); 20 | } 21 | else if ((n % 10) < 6 && (n % 10) != 0) 22 | { 23 | printf("Last digit of %d is %d and is less than 6 and not 0\n", 24 | n, n % 10); 25 | } 26 | else 27 | { 28 | printf("Last digit of %d is %d and is 0\n", 29 | n, n % 10); 30 | } 31 | 32 | return (0); 33 | 34 | } 35 | 36 | -------------------------------------------------------------------------------- /variables_if_else_while/2-print_alphabet: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/variables_if_else_while/2-print_alphabet -------------------------------------------------------------------------------- /variables_if_else_while/2-print_alphabet.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - Entry point 4 | * 5 | * Return: Always 0 (Success) 6 | */ 7 | 8 | int main(void) 9 | { 10 | char alphabet = 'a'; 11 | 12 | while (alphabet <= 'z') 13 | { 14 | putchar(alphabet); 15 | alphabet++; 16 | } 17 | putchar('\n'); 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /variables_if_else_while/3-print_alphabets.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - Entry point 4 | * 5 | * Return: Always 0 (Success) 6 | */ 7 | int main(void) 8 | { 9 | int i; 10 | 11 | for (i = 97; i <= 122; i++) 12 | { 13 | putchar(i); 14 | } 15 | 16 | for (i = 65; i <= 90; i++) 17 | { 18 | putchar(i); 19 | } 20 | putchar('\n'); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /variables_if_else_while/4-print_alphabt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/variables_if_else_while/4-print_alphabt -------------------------------------------------------------------------------- /variables_if_else_while/4-print_alphabt.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - Entry point 4 | * 5 | * Return: Always 0 (Success) 6 | */ 7 | int main(void) 8 | { 9 | int i; 10 | char letter; 11 | 12 | for (i = 97; i <= 122; i++) 13 | { 14 | letter = i; 15 | if (letter != 'q' && letter != 'e') 16 | { 17 | putchar(letter); 18 | } 19 | } 20 | putchar('\n'); 21 | return (0); 22 | 23 | } 24 | -------------------------------------------------------------------------------- /variables_if_else_while/5-print_numbers: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/variables_if_else_while/5-print_numbers -------------------------------------------------------------------------------- /variables_if_else_while/5-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - Entry point 4 | * 5 | * Return: Always 0 (Success) 6 | */ 7 | 8 | int main(void) 9 | { 10 | int number; 11 | 12 | for (number = 0; number < 10; number++) 13 | { 14 | printf("%d", number); 15 | } 16 | 17 | putchar('\n'); 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /variables_if_else_while/6-print_numberz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/variables_if_else_while/6-print_numberz -------------------------------------------------------------------------------- /variables_if_else_while/6-print_numberz.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - Entry point 4 | * 5 | * Return: Always 0 (Success) 6 | */ 7 | 8 | int main(void) 9 | { 10 | int number; 11 | 12 | for (number = 0; number < 10; number++) 13 | { 14 | putchar(number + '0'); 15 | } 16 | 17 | putchar('\n'); 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /variables_if_else_while/7-print_tebahpla: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/variables_if_else_while/7-print_tebahpla -------------------------------------------------------------------------------- /variables_if_else_while/7-print_tebahpla.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - Entry point 4 | * 5 | * Return: Always 0 (Success) 6 | */ 7 | int main(void) 8 | { 9 | char letter; 10 | for (letter = 'z'; letter >= 'a'; letter--) 11 | putchar(letter); 12 | putchar('\n'); 13 | return (0); 14 | } 15 | -------------------------------------------------------------------------------- /variables_if_else_while/8-print_base16: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/variables_if_else_while/8-print_base16 -------------------------------------------------------------------------------- /variables_if_else_while/8-print_base16.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Entry point 5 | * 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | int number; 11 | 12 | for (number = 0; number < 16; number++) 13 | { 14 | if (number < 10) 15 | { 16 | putchar(number + '0'); 17 | } 18 | else 19 | { 20 | putchar(number + 87); 21 | } 22 | } 23 | putchar('\n'); 24 | return (0); 25 | } 26 | -------------------------------------------------------------------------------- /variables_if_else_while/9-print_comb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/variables_if_else_while/9-print_comb -------------------------------------------------------------------------------- /variables_if_else_while/9-print_comb.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - Entry point 4 | * 5 | * Return: Always 0 (Success) 6 | */ 7 | int main(void) 8 | { 9 | int num; 10 | 11 | for (num = 0; num < 10; num++) 12 | { 13 | putchar(num + '0'); 14 | if (num < 9) 15 | { 16 | putchar(','); 17 | putchar(' '); 18 | } 19 | } 20 | 21 | putchar('\n'); 22 | 23 | return (0); 24 | } 25 | 26 | -------------------------------------------------------------------------------- /variables_if_else_while/README.md: -------------------------------------------------------------------------------- 1 | # holbertonschool-low_level_programming 2 | C 3 | -------------------------------------------------------------------------------- /variables_if_else_while/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chloe0524/holbertonschool-low_level_programming/c87be80ee8277bd80b8814ee50c9fd001521bc56/variables_if_else_while/a.out -------------------------------------------------------------------------------- /variadic_functions/0-sum_them_all.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | #include 3 | 4 | /** 5 | * sum_them_all - Calculates sum of variable args 6 | * 7 | * @n: number of args 8 | * Return: sum of variable args 9 | */ 10 | 11 | int sum_them_all(const unsigned int n, ...) 12 | { 13 | unsigned int i; 14 | int sum; 15 | va_list mathlist; 16 | 17 | va_start(mathlist, n); 18 | for (i = 0, sum = 0; i < n; i++) 19 | sum += va_arg(mathlist, int); 20 | 21 | va_end(mathlist); 22 | return (sum); 23 | } 24 | 25 | -------------------------------------------------------------------------------- /variadic_functions/1-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * print_numbers - print numbers with separator 7 | * 8 | * @separator: delimiter between numbers 9 | * @n: number of arguments 10 | * Return: prints varargs 11 | */ 12 | 13 | void print_numbers(const char *separator, const unsigned int n, ...) 14 | { 15 | va_list printslist; 16 | unsigned int i = 0; 17 | 18 | if (n > 0) 19 | { 20 | va_start(printslist, n); 21 | 22 | while (i < n) 23 | { 24 | printf("%d", va_arg(printslist, int)); 25 | 26 | if (i != n - 1 && separator != NULL) 27 | printf("%s", separator); 28 | 29 | i++; 30 | } 31 | va_end(printslist); 32 | } 33 | printf("\n"); 34 | } 35 | -------------------------------------------------------------------------------- /variadic_functions/2-print_strings.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * print_strings - print numbers with separator 7 | * 8 | * @separator: delimiter between numbers 9 | * @n: number of arguments 10 | * Return: prints varargs 11 | */ 12 | 13 | void print_strings(const char *separator, const unsigned int n, ...) 14 | { 15 | va_list printslist; 16 | unsigned int i = 0; 17 | 18 | va_start(printslist, n); 19 | 20 | while (i < n) 21 | { 22 | char *str = va_arg(printslist, char *); 23 | 24 | if (str == NULL) 25 | printf("(nil)"); 26 | else 27 | printf("%s", str); 28 | 29 | if (i != n - 1 && separator != NULL) 30 | printf("%s", separator); 31 | 32 | i++; 33 | } 34 | 35 | printf("\n"); 36 | 37 | va_end(printslist); 38 | } 39 | 40 | -------------------------------------------------------------------------------- /variadic_functions/3-print_all.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "variadic_functions.h" 5 | 6 | /** 7 | * display_char - Display a character. 8 | * @va: va_list containing the character to be displayed. 9 | * 10 | * Description: function takes a va_list containing a character 11 | * and prints it to the standard output. 12 | */ 13 | 14 | void display_char(va_list va) 15 | { 16 | printf("%c", va_arg(va, int)); 17 | } 18 | 19 | /** 20 | * display_int - Display an integer. 21 | * @va: va_list containing the integer to be displayed. 22 | * 23 | * Description: function takes a va_list containing an integer 24 | * and prints it to the standard output. 25 | */ 26 | 27 | void display_int(va_list va) 28 | { 29 | printf("%d", va_arg(va, int)); 30 | } 31 | /** 32 | * display_float - Display a floating-point number. 33 | * @va: va_list containing the floating-point number to be displayed. 34 | * 35 | * Description: function takes a va_list containing a double value 36 | * and prints it to the standard output. 37 | */ 38 | 39 | void display_float(va_list va) 40 | { 41 | printf("%f", va_arg(va, double)); 42 | } 43 | 44 | /** 45 | * display_string - Display a string. 46 | * @va: A va_list containing the string to be displayed. 47 | * 48 | * Description: This function takes a va_list containing a string 49 | * and prints it to the stdout if string = NULL prints "(nil)" 50 | */ 51 | 52 | void display_string(va_list va) 53 | { 54 | char *la_chaine = va_arg(va, char *); 55 | (la_chaine != NULL) ? printf("%s", la_chaine) : printf("(nil)"); 56 | } 57 | 58 | /** 59 | * print_all - can print anything 60 | * 61 | * @format: define format 62 | * 63 | * Descritption: This function can print anything. 64 | * Return: void 65 | * 66 | */ 67 | 68 | void print_all(const char * const format, ...) 69 | { 70 | va_list liste_parametre; 71 | int i = 0; 72 | int position = 0; 73 | 74 | tdisplay display[] = { 75 | {'c', display_char}, 76 | {'i', display_int}, 77 | {'f', display_float}, 78 | {'s', display_string}, 79 | {'\0', NULL} 80 | }; 81 | 82 | va_start(liste_parametre, format); 83 | 84 | while (format[i]) 85 | { 86 | position = 0; 87 | while (display[position].format_type != format[i] && 88 | display[position].format_type != '\0') 89 | position++; 90 | 91 | if (display[position].format_type != '\0') 92 | { 93 | display[position].fdisplay(liste_parametre); 94 | if (format[i + 1] != '\0') 95 | printf(", "); 96 | } 97 | i++; 98 | } 99 | va_end(liste_parametre); 100 | putchar('\n'); 101 | } 102 | 103 | -------------------------------------------------------------------------------- /variadic_functions/README.md: -------------------------------------------------------------------------------- 1 | # Tasks 2 | 3 | ![giphy](https://github.com/chloe0524/holbertonschool-low_level_programming/assets/127857895/44ca4193-b221-4cba-94bd-958fac8541bb) 4 | 5 | 1. [Beauty is variable, ugliness is constant](#beauty-is-variable-ugliness-is-constant) 6 | 2. [To be is to be the value of a variable](#to-be-is-to-be-the-value-of-a-variable) 7 | 3. [One woman's constant is another woman's variable](#one-womans-constant-is-another-womans-variable) 8 | 4. [To be is a to be the value of a variable](#to-be-is-a-to-be-the-value-of-a-variable) 9 | 10 | ## Beauty is variable, ugliness is constant 11 | 12 | Mandatory 13 | 14 | Write a function that returns the sum of all its parameters. 15 | 16 | Prototype: `int sum_them_all(const unsigned int n, ...);` 17 | 18 | If n == 0, return 0 19 | 20 | ## To be is to be the value of a variable :card_index: 21 | 22 | Mandatory 23 | 24 | Write a function that prints numbers, followed by a new line. 25 | 26 | Prototype: `void print_numbers(const char *separator, const unsigned int n, ...);` 27 | 28 | where separator is the string to be printed between numbers and n is the number of integers passed to the function. You are allowed to use printf. If separator is NULL, don’t print it. Print a new line at the end of your function. 29 | 30 | ## One woman's constant is another woman's variable 31 | 32 | Mandatory 33 | 34 | Write a function that prints strings, followed by a new line. 35 | 36 | Prototype: `void print_strings(const char *separator, const unsigned int n, ...);` 37 | 38 | where separator is the string to be printed between the strings and n is the number of strings passed to the function. You are allowed to use printf. If separator is NULL, don’t print it. If one of the strings is NULL, print (nil) instead. Print a new line at the end of your function. 39 | 40 | ## To be is a to be the value of a variable 41 | 42 | Mandatory 43 | 44 | Write a function that prints anything. 45 | 46 | Prototype: `void print_all(const char * const format, ...);` 47 | 48 | where format is a list of types of arguments passed to the function: 49 | 50 | - c: char 51 | - i: integer 52 | - f: float 53 | - s: char * (if the string is NULL, print (nil) instead) 54 | 55 | Any other char should be ignored. You are not allowed to use for, goto, ternary operator, else, do ... while. You can use a maximum of 2 while loops, 2 if statements. You are allowed to use printf. Print a new line at the end of your function. 56 | 57 | -------------------------------------------------------------------------------- /variadic_functions/_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 | -------------------------------------------------------------------------------- /variadic_functions/variadic_functions.h: -------------------------------------------------------------------------------- 1 | #ifndef VAR_FUNCTIONS_H 2 | #define VAR_FUNCTIONS_H 3 | 4 | #include 5 | 6 | /** 7 | * struct tdisplay - Format specifier and display function pair 8 | * @format_type: Format specifier character ('c', 'i', 'f', 's') 9 | * @fdisplay: Pointer to the display function 10 | */ 11 | typedef struct tdisplay 12 | { 13 | char format_type; 14 | void (*fdisplay)(va_list); 15 | } tdisplay; 16 | 17 | int sum_them_all(const unsigned int n, ...); 18 | void print_numbers(const char *separator, const unsigned int n, ...); 19 | void print_strings(const char *separator, const unsigned int n, ...); 20 | void print_all(const char * const format, ...); 21 | int _putchar(char c); 22 | 23 | #endif /* VAR_FUNCTIONS_H */ 24 | --------------------------------------------------------------------------------