├── 0x00-hello_world ├── 0-preprocessor ├── 1-compiler ├── 100-intel ├── 101-quote.c ├── 2-assembler ├── 3-name ├── 4-puts.c ├── 5-printf.c ├── 6-size.c └── README.md ├── 0x01-variables_if_else_while ├── .gitignore ├── .vscode │ ├── settings.json │ └── tasks.json ├── 0-positive_or_negative.c ├── 1-last_digit.c ├── 100-print_comb3.c ├── 101-print_comb4 ├── 101-print_comb4.c ├── 102-print_comb5.c ├── 2-print_alphabet.c ├── 3-print_alphabets.c ├── 4-print_alphabt.c ├── 5-print_numbers.c ├── 6-print_numberz.c ├── 7-print_tebahpla.c ├── 8-print_base16.c ├── 9-print_comb.c └── README.md ├── 0x02-functions_nested_loops ├── 0-putchar.c ├── 1-alphabet.c ├── 10-add.c ├── 100-times_table.c ├── 101-natural.c ├── 102-fibonacci.c ├── 103-fibonacci.c ├── 104-fibonacci.c ├── 11-print_to_98.c ├── 2-print_alphabet_x10.c ├── 3-islower.c ├── 4-isalpha.c ├── 5-sign.c ├── 6-abs.c ├── 7-print_last_digit.c ├── 8-24_hours.c ├── 9-times_table.c ├── README.md ├── _putchar.c └── main.h ├── 0x03-debugging ├── 0-main.c ├── 1-main.c ├── 2-largest_number.c ├── 3-print_remaining_days.c ├── README.md └── main.h ├── 0x04-more_functions_nested_loops ├── 0-isupper.c ├── 1-isdigit.c ├── 10-print_triangle.c ├── 100-prime_factor.c ├── 101-print_number.c ├── 2-mul.c ├── 3-print_numbers.c ├── 4-print_most_numbers.c ├── 5-more_numbers.c ├── 6-print_line.c ├── 7-print_diagonal.c ├── 8-print_square.c ├── 9-fizz_buzz.c ├── README.md ├── _putchar.c └── main.h ├── 0x05-pointers_arrays_strings ├── 0-reset_to_98.c ├── 1-swap.c ├── 100-atoi.c ├── 2-strlen.c ├── 3-puts.c ├── 4-print_rev.c ├── 5-rev_string.c ├── 6-puts2.c ├── 7-puts_half.c ├── 8-print_array.c ├── 9-strcpy.c ├── README.md ├── _putchar.c └── main.h ├── 0x06-pointers_arrays_strings ├── 0-strcat.c ├── 1-strncat.c ├── 2-strncpy.c ├── 3-strcmp.c ├── 4-rev_array.c ├── 5-string_toupper.c ├── 6-cap_string.c ├── 7-leet.c ├── README.md ├── _putchar.c └── main.h ├── 0x07-pointers_arrays_strings ├── 0-memset.c ├── 1-memcpy.c ├── 100-set_string.c ├── 2-strchr.c ├── 3-strspn.c ├── 4-strpbrk.c ├── 5-strstr.c ├── 7-print_chessboard.c ├── 8-print_diagsums.c ├── README.md ├── _putchar.c └── main.h ├── 0x08-recursion ├── 0-puts_recursion.c ├── 1-print_rev_recursion.c ├── 100-is_palindrome.c ├── 100-wildcmp.c ├── 101-wildcmp.c ├── 2-strlen_recursion.c ├── 3-factorial.c ├── 4-pow_recursion.c ├── 5-sqrt_recursion.c ├── 6-is_prime_number.c ├── README.md ├── _putchar.c └── main.h ├── 0x09-static_libraries ├── .gitignore ├── README.md ├── create_static_lib.sh ├── libmy.a ├── main └── main.h ├── 0x0A-argc_argv ├── 0-whatsmyname.c ├── 1-args.c ├── 100-change.c ├── 2-args.c ├── 3-mul.c ├── 4-add.c ├── README.md └── main.h ├── 0x0B-malloc_free ├── 0-create_array.c ├── 1-strdup.c ├── 100-argstostr.c ├── 101-strtow.c ├── 2-str_concat.c ├── 3-alloc_grid.c ├── 4-free_grid.c ├── README.md └── main.h ├── 0x0C-more_malloc_free ├── 0-malloc_checked.c ├── 1-string_nconcat.c ├── 100-realloc.c ├── 101-mul.c ├── 2-calloc.c ├── 3-array_range.c ├── README.md └── main.h ├── 0x0C-preprocessor ├── 0-object_like_macro.h ├── 1-pi.h ├── 2-main.c ├── 3-function_like_macro.h ├── 4-sum.h └── README.md ├── 0x0D-preprocessor ├── 0-object_like_macro.h ├── 1-pi.h ├── 2-main.c ├── 3-function_like_macro.h ├── 4-sum.h └── README.md ├── 0x0D-structures_typedef ├── 1-init_dog.c ├── 2-print_dog.c ├── 4-new_dog.c ├── 5-free_dog.c ├── README.md └── dog.h ├── 0x0E-structures_typedef ├── 1-init_dog.c ├── 2-print_dog.c ├── 4-new_dog.c ├── 5-free_dog.c ├── README.md └── dog.h └── README.md /0x00-hello_world/0-preprocessor: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -E $CFILE -o c 3 | -------------------------------------------------------------------------------- /0x00-hello_world/1-compiler: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -c $CFILE 3 | -------------------------------------------------------------------------------- /0x00-hello_world/100-intel: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -S -masm=intel $CFILE 3 | -------------------------------------------------------------------------------- /0x00-hello_world/101-quote.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * main - prints exactly "and that piece of art is useful" 6 | * - Dora Korpar, 2015-10-19", 7 | * followed by a new line, to the standard error. 8 | * Return: Always 0 (Success) 9 | */ 10 | int main(void) 11 | { 12 | write(2, "and that piece of art is useful\" - Dora Korpar, 2015-10-19\n", 59); 13 | return (1); 14 | } 15 | -------------------------------------------------------------------------------- /0x00-hello_world/2-assembler: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc $CFILE -S 3 | -------------------------------------------------------------------------------- /0x00-hello_world/3-name: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc $CFILE -o cisfun 3 | -------------------------------------------------------------------------------- /0x00-hello_world/4-puts.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints the size of various types 5 | * Return: 0 if exited properly, non-zero otherwise 6 | */ 7 | int main(void) 8 | { 9 | puts("\"Programming is like building a multilingual puzzle"); 10 | return (0); 11 | } 12 | -------------------------------------------------------------------------------- /0x00-hello_world/5-printf.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints the size of various types 5 | * Return: 0 if exited properly, non-zero otherwise 6 | */ 7 | int main(void) 8 | { 9 | printf("with proper grammar, but the outcome is a piece of art,\n"); 10 | return (0); 11 | } 12 | -------------------------------------------------------------------------------- /0x00-hello_world/6-size.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints the size of various types 5 | * Return: 0 if exited properly, non-zero otherwise 6 | */ 7 | int main(void) 8 | { 9 | printf("Size of a char: %d byte(s)\n", sizeof(char)); 10 | printf("Size of an int: %d byte(s)\n", sizeof(int)); 11 | printf("Size of a long int: %d byte(s)\n", sizeof(long int)); 12 | printf("Size of a long long int: %d byte(s)\n", sizeof(long long int)); 13 | printf("Size of a float: %d byte(s)\n", sizeof(float)); 14 | return (0); 15 | } 16 | 17 | -------------------------------------------------------------------------------- /0x00-hello_world/README.md: -------------------------------------------------------------------------------- 1 | this is a c hello worlds readme 2 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/.gitignore: -------------------------------------------------------------------------------- 1 | */.vscode/ 2 | */.vscode/* -------------------------------------------------------------------------------- /0x01-variables_if_else_while/.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "C_Cpp.errorSquiggles": "Disabled" 3 | } -------------------------------------------------------------------------------- /0x01-variables_if_else_while/.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "tasks": [ 3 | { 4 | "type": "cppbuild", 5 | "label": "C/C++: gcc-9 build active file", 6 | "command": "/usr/bin/gcc-9", 7 | "args": [ 8 | "-fdiagnostics-color=always", 9 | "-g", 10 | "${file}", 11 | "-o", 12 | "${fileDirname}/${fileBasenameNoExtension}" 13 | ], 14 | "options": { 15 | "cwd": "${fileDirname}" 16 | }, 17 | "problemMatcher": [ 18 | "$gcc" 19 | ], 20 | "group": { 21 | "kind": "build", 22 | "isDefault": true 23 | }, 24 | "detail": "Task generated by Debugger." 25 | } 26 | ], 27 | "version": "2.0.0" 28 | } -------------------------------------------------------------------------------- /0x01-variables_if_else_while/0-positive_or_negative.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /** 6 | *main -> assign a random number to the variable n each time it is executed 7 | *and print the last digit of the number stored in the variable n 8 | *Return: Always 0 (Success) 9 | */ 10 | int main(void) 11 | { 12 | int n; 13 | 14 | srand(time(0)); 15 | n = rand() - RAND_MAX / 2; 16 | if (n > 0) 17 | printf("%d is positive\n", n); 18 | if (n == 0) 19 | printf("%d is zero\n", n); 20 | if (n < 0) 21 | { 22 | printf("%d is negative\n", n); 23 | } 24 | return (0); 25 | } 26 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/1-last_digit.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /** 6 | *main -> assign a random number to the variable n each time it is executed 7 | *and print the last digit of the number stored in the variable n 8 | *Return: Always 0 (Success) 9 | */ 10 | int main(void) 11 | { 12 | int n; 13 | int x; 14 | 15 | srand(time(0)); 16 | n = rand() - RAND_MAX / 2; 17 | 18 | x = n % 10; 19 | printf("Last digit of %d is %d ", n, x); 20 | if (x > 5) 21 | { 22 | printf("and is greater than 5"); 23 | } 24 | if (x == 0) 25 | { 26 | printf("and is 0"); 27 | } 28 | if (x < 6 && x != 0) 29 | { 30 | printf("and is less than 6 and not 0"); 31 | } 32 | 33 | printf("\n"); 34 | 35 | return (0); 36 | } 37 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/100-print_comb3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | *main -> assign a random number to the variable n each time it is executed 5 | *and print the last digit of the number stored in the variable n 6 | *Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | int ch; 11 | int n; 12 | for (ch = 48; ch <= 57; ch++) 13 | { 14 | for (n = 49; n <= 57; n++) 15 | { 16 | if (n > ch) 17 | { 18 | putchar(ch); 19 | putchar(n); 20 | if (ch != 56 || n != 57) 21 | { 22 | putchar(44); 23 | putchar(32); 24 | } 25 | } 26 | } 27 | } 28 | 29 | putchar(10); /* this is an ascii code for new line*/ 30 | 31 | return (0); 32 | } 33 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/101-print_comb4: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/saminegash1/alx-low_level_programming/de8b604aa103d44a37596e90a340cc5c0af91e8a/0x01-variables_if_else_while/101-print_comb4 -------------------------------------------------------------------------------- /0x01-variables_if_else_while/101-print_comb4.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints all possible different combinations of three digits 5 | * Return: Always 0 (Success) 6 | */ 7 | int main(void) 8 | { 9 | int n, m, l; 10 | 11 | for (n = 48; n < 58; n++) 12 | { 13 | for (m = 49; m < 58; m++) 14 | { 15 | for (l = 50; l < 58; l++) 16 | { 17 | if (l > m && m > n) 18 | { 19 | putchar(n); 20 | putchar(m); 21 | putchar(l); 22 | if (n != 55 || m != 56) 23 | { 24 | putchar(','); 25 | putchar(' '); 26 | } 27 | } 28 | } 29 | } 30 | } 31 | putchar('\n'); 32 | return (0); 33 | } 34 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/102-print_comb5.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | *main -> assign a random number to the variable n each time it is executed 5 | *and print the last digit of the number stored in the variable n 6 | *Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | int i, j; 11 | 12 | for (i = 0; i < 100; i++) 13 | { 14 | for (j = 0; j < 100; j++) 15 | { 16 | if (i < j) 17 | { 18 | putchar((i / 10) + 48); 19 | putchar((i % 10) + 48); 20 | putchar(' '); 21 | putchar((j / 10) + 48); 22 | putchar((j % 10) + 48); 23 | if (i != 98 || j != 99) 24 | { 25 | putchar(','); 26 | putchar(' '); 27 | } 28 | } 29 | } 30 | } 31 | putchar('\n'); 32 | return (0); 33 | } 34 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/2-print_alphabet.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | *main -> assign a random number to the variable n each time it is executed 5 | *and print the last digit of the number stored in the variable n 6 | *Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | int ch; 11 | 12 | for (ch = 97; ch <= 122; ch++) 13 | { 14 | putchar(ch); 15 | } 16 | putchar(10); /* this is an ascii code for new line*/ 17 | 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/3-print_alphabets.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | *main -> assign a random number to the variable n each time it is executed 5 | *and print the last digit of the number stored in the variable n 6 | *Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | int ch; 11 | 12 | for (ch = 97; ch <= 122; ch++) 13 | { 14 | putchar(ch); 15 | } 16 | for (ch = 65; ch <= 90; ch++) 17 | { 18 | putchar(ch); 19 | } 20 | putchar(10); /* this is an ascii code for new line*/ 21 | 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/4-print_alphabt.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | *main -> assign a random number to the variable n each time it is executed 5 | *and print the last digit of the number stored in the variable n 6 | *Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | char ch; 11 | for (ch = 'a'; ch <= 'z'; ch++) 12 | { 13 | if (ch != 'q' && ch != 'e') 14 | { 15 | putchar(ch); 16 | } 17 | } 18 | 19 | putchar(10); /* this is an ascii code for new line*/ 20 | 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/5-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | *main -> assign a random number to the variable n each time it is executed 5 | *and print the last digit of the number stored in the variable n 6 | *Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | int n; 11 | 12 | for (n = 0; n < 10; n++) 13 | { 14 | printf("%d", n); 15 | } 16 | printf("\n"); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/6-print_numberz.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | *main -> assign a random number to the variable n each time it is executed 5 | *and print the last digit of the number stored in the variable n 6 | *Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | int ch; 11 | for (ch = 48; ch <= 57; ch++) 12 | { 13 | 14 | putchar(ch); 15 | } 16 | 17 | putchar(10); /* this is an ascii code for new line*/ 18 | 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/7-print_tebahpla.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | *main -> assign a random number to the variable n each time it is executed 5 | *and print the last digit of the number stored in the variable n 6 | *Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | char ch; 11 | 12 | for (ch = 'z'; ch >= 'a'; ch--) 13 | { 14 | 15 | putchar(ch); 16 | } 17 | 18 | putchar(10); /* this is an ascii code for new line*/ 19 | 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/8-print_base16.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | *main -> assign a random number to the variable n each time it is executed 5 | *and print the last digit of the number stored in the variable n 6 | *Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | char ch; 11 | int n; 12 | 13 | for (n = 48; n <= 57; n++) 14 | { 15 | 16 | putchar(n); 17 | } 18 | for (ch = 'a'; ch <= 'f'; ch++) 19 | { 20 | 21 | putchar(ch); 22 | } 23 | 24 | putchar(10); 25 | 26 | return (0); 27 | } 28 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/9-print_comb.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | *main -> assign a random number to the variable n each time it is executed 5 | *and print the last digit of the number stored in the variable n 6 | *Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | int ch; 11 | for (ch = 48; ch <= 57; ch++) 12 | { 13 | 14 | putchar(ch); 15 | if (ch != 57) 16 | { 17 | putchar(44); 18 | putchar(32); 19 | } 20 | } 21 | 22 | putchar(10); /* this is an ascii code for new line*/ 23 | 24 | return (0); 25 | } 26 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/README.md: -------------------------------------------------------------------------------- 1 | this is assignment number 2 in low leverl programming 2 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/0-putchar.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - prints putchar 5 | * Return: always 0 6 | */ 7 | int main(void) 8 | { 9 | _putchar('_'); 10 | _putchar('p'); 11 | _putchar('u'); 12 | _putchar('t'); 13 | _putchar('c'); 14 | _putchar('h'); 15 | _putchar('a'); 16 | _putchar('r'); 17 | _putchar('\n'); 18 | 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/1-alphabet.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_alphabet -> prints the lowercase alphabets 5 | */ 6 | 7 | void print_alphabet(void) 8 | { 9 | int j; 10 | 11 | for (j = 'a'; j <= 'z'; j++) 12 | { 13 | _putchar(j); 14 | } 15 | _putchar('\n'); 16 | } 17 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/10-add.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * add -> adds two number 5 | * @x : param1 6 | * @y : param2 7 | * Return: x + y 8 | */ 9 | 10 | int add(int x, int y) 11 | { 12 | return (x + y); 13 | } 14 | 15 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/100-times_table.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_times_table - prints the n times table, starting with 0 5 | * @n: number of the times table 6 | */ 7 | void print_times_table(int n) 8 | { 9 | int i, j, k; 10 | 11 | if (n >= 0 && n <= 15) 12 | { 13 | for (i = 0; i <= n; i++) 14 | { 15 | for (j = 0; j <= n; j++) 16 | { 17 | k = j * i; 18 | if (j == 0) 19 | { 20 | _putchar(k + '0'); 21 | } else if (k < 10 && j != 0) 22 | { 23 | _putchar(','); 24 | _putchar(' '); 25 | _putchar(' '); 26 | _putchar(' '); 27 | _putchar(k + '0'); 28 | } else if (k >= 10 && k < 100) 29 | { 30 | _putchar(','); 31 | _putchar(' '); 32 | _putchar(' '); 33 | _putchar((k / 10) + '0'); 34 | _putchar((k % 10) + '0'); 35 | } else if (k >= 100) 36 | { 37 | _putchar(','); 38 | _putchar(' '); 39 | _putchar((k / 100) + '0'); 40 | _putchar(((k / 10) % 10) + '0'); 41 | _putchar((k % 10) + '0'); 42 | } 43 | } 44 | _putchar('\n'); 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/101-natural.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - computes and prints the sum of all the multiples 5 | * of 3 or 5 below 1024 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | unsigned long int sum3, sum5, sum; 11 | int i; 12 | 13 | sum3 = 0; 14 | sum5 = 0; 15 | sum = 0; 16 | 17 | for (i = 0; i < 1024; ++i) 18 | { 19 | if ((i % 3) == 0) 20 | { 21 | sum3 = sum3 + i; 22 | } else if ((i % 5) == 0) 23 | { 24 | sum5 = sum5 + i; 25 | } 26 | } 27 | sum = sum3 + sum5; 28 | printf("%lu\n", sum); 29 | return (0); 30 | } 31 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/102-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints the first 50 Fibonacci numbers, starting with 1 and 2 5 | * followed by a new line 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | long int i, j, k, next; 11 | 12 | j = 1; 13 | 14 | k = 2; 15 | 16 | for (i = 1; i <= 50; ++i) 17 | { 18 | if (j != 20365011074) 19 | { 20 | printf("%ld, ", j); 21 | } else 22 | { 23 | printf("%ld\n", j); 24 | } 25 | next = j + k; 26 | j = k; 27 | k = next; 28 | } 29 | 30 | return (0); 31 | } 32 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/103-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - finds and prints the sum of the even-valued terms 5 | * followed by a new line 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | int i; 11 | unsigned long int j, k, next, sum; 12 | 13 | j = 1; 14 | k = 2; 15 | sum = 0; 16 | 17 | for (i = 1; i <= 33; ++i) 18 | { 19 | if (j < 4000000 && (j % 2) == 0) 20 | { 21 | sum = sum + j; 22 | } 23 | next = j + k; 24 | j = k; 25 | k = next; 26 | } 27 | 28 | printf("%lu\n", sum); 29 | 30 | return (0); 31 | } 32 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/104-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - finds and prints the first 98 Fibonacci numbers, 5 | * starting with 1 and 2 6 | * followed by a new line 7 | * Return: ALways 0 (Success) 8 | */ 9 | int main(void) 10 | { 11 | unsigned long int i, j, k, j1, j2, k1, k2; 12 | 13 | j = 1; 14 | k = 2; 15 | 16 | printf("%lu", j); 17 | 18 | for (i = 1; i < 91; i++) 19 | { 20 | printf(", %lu", k); 21 | k = k + j; 22 | j = k - j; 23 | } 24 | 25 | j1 = j / 1000000000; 26 | j2 = j % 1000000000; 27 | k1 = k / 1000000000; 28 | k2 = k % 1000000000; 29 | 30 | for (i = 92; i < 99; ++i) 31 | { 32 | printf(", %lu", k1 + (k2 / 1000000000)); 33 | printf("%lu", k2 % 1000000000); 34 | k1 = k1 + j1; 35 | j1 = k1 - j1; 36 | k2 = k2 + j2; 37 | j2 = k2 - j2; 38 | } 39 | 40 | printf("\n"); 41 | 42 | return (0); 43 | } 44 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/11-print_to_98.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | 5 | /** 6 | * print_to_98 - prints to 98 7 | * @n: the starting point 8 | */ 9 | void print_to_98(int n) 10 | { 11 | if (n <= 98) 12 | { 13 | while (n <= 98) 14 | { 15 | if (n == 98) 16 | printf("%d\n", n); 17 | else 18 | printf("%d, ", n); 19 | n = n + 1; 20 | } 21 | } else if (n > 98) 22 | { 23 | while (n >= 98) 24 | { 25 | if (n == 98) 26 | printf("%d\n", n); 27 | else 28 | printf("%d, ", n); 29 | n = n - 1; 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/2-print_alphabet_x10.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_alphabet_x10 -> prints the lowercase alphabets 5 | */ 6 | 7 | void print_alphabet_x10(void) 8 | { 9 | int j; 10 | int x; 11 | 12 | for (x = 0; x < 10; x++) 13 | { 14 | for (j = 'a'; j <= 'z'; j++) 15 | { 16 | _putchar(j); 17 | } 18 | _putchar('\n'); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/3-islower.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _islower -> checks if the character is lowercase 5 | * @c: a charcter argument 6 | * Return: returns 1 and 0 depending on condition 7 | */ 8 | int _islower(int c) 9 | { 10 | return (c >= 'a' && c <= 'z'); 11 | } 12 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/4-isalpha.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isalpha - checks for alphabetical letters 5 | * @c: a charter to be checke on 6 | * Return: returns 0 or 1 depending on condition 7 | */ 8 | int _isalpha(int c) 9 | { 10 | return ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')); 11 | } 12 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/5-sign.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_sign -> prints sign based on condition 5 | * @n: argument passed 6 | * Return: 1, 0 -1 7 | */ 8 | int print_sign(int n) 9 | { 10 | if (n > 0) 11 | { 12 | _putchar('+'); 13 | return (1); 14 | } 15 | else if (n == 0) 16 | { 17 | _putchar('0'); 18 | return (0); 19 | } 20 | else 21 | { 22 | _putchar('-'); 23 | return (-1); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/6-abs.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _abs -> return absolute value of a number 5 | * @n: parameter 6 | * Return: absolute value of a number 7 | */ 8 | int _abs(int n) 9 | { 10 | if (n < 0) 11 | n = -n; 12 | return (n); 13 | } 14 | 15 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/7-print_last_digit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_last_digit - prints the last digit of a number 5 | * @n: the int to extract the last digit from 6 | * Return: value of the last digit 7 | */ 8 | int print_last_digit(int n) 9 | { 10 | int a; 11 | 12 | if (n < 0) 13 | n = -n; 14 | 15 | a = n % 10; 16 | 17 | if (a < 0) 18 | a = -a; 19 | 20 | _putchar(a + '0'); 21 | 22 | return (a); 23 | } 24 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/8-24_hours.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * jack_bauer -> prints 24 hours 4 | */ 5 | void jack_bauer(void) 6 | { 7 | int i, j; 8 | 9 | for (i = 0; i < 24; i++) 10 | { 11 | for (j = 0; j < 60; j++) 12 | { 13 | if (i < 10) 14 | { 15 | _putchar('0'); 16 | _putchar(i + '0'); 17 | } 18 | else if (i >= 10) 19 | { 20 | _putchar((i / 10) + '0'); 21 | _putchar((i % 10) + '0'); 22 | } 23 | if (j < 10) 24 | { 25 | _putchar(':'); 26 | _putchar('0'); 27 | _putchar(j + '0'); 28 | } 29 | else if (j >= 10) 30 | { 31 | _putchar(':'); 32 | _putchar((j / 10) + '0'); 33 | _putchar((j % 10) + '0'); 34 | } 35 | _putchar('\n'); 36 | } 37 | 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/9-times_table.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | *times_table -> time table 5 | */ 6 | void times_table(void) 7 | { 8 | int x, y, k; 9 | 10 | for (x = 0; x < 10; x++) 11 | { 12 | for (y = 0; y < 10; y++) 13 | { 14 | k = x * y; 15 | if (y == 0) 16 | _putchar(k + '0'); 17 | if (y != 0 && k < 10) 18 | { 19 | _putchar(','); 20 | _putchar(' '); 21 | _putchar(' '); 22 | _putchar(k + '0'); 23 | } else if (k >= 10) 24 | { 25 | _putchar(','); 26 | _putchar(' '); 27 | _putchar((k / 10) + '0'); 28 | _putchar((k % 10) + '0'); 29 | } 30 | } 31 | _putchar('\n'); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/README.md: -------------------------------------------------------------------------------- 1 | THIS IS ONE README FOR THE C FUNCTIONSAND NESTED LOOPS 2 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _putchar - writes the character c to stdout 5 | * @c: The character to print 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | int _putchar(char c) 11 | { 12 | return (write(1, &c, 1)); 13 | } 14 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/main.h: -------------------------------------------------------------------------------- 1 | int _putchar(char c); 2 | void print_alphabet(void); 3 | void print_alphabet_x10(void); 4 | int _islower(int c); 5 | int _isalpha(int c); 6 | int print_sign(int n); 7 | int _abs(int); 8 | int print_last_digit(int); 9 | void jack_bauer(void); 10 | void times_table(void); 11 | int add(int, int); 12 | void print_to_98(int n); 13 | void print_times_table(int n); 14 | -------------------------------------------------------------------------------- /0x03-debugging/0-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - tests function that prints if integer is positive or negative 5 | * Return: 0 6 | */ 7 | 8 | int main(void) 9 | { 10 | int i; 11 | 12 | i = 0; 13 | positive_or_negative(i); 14 | 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x03-debugging/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - causes an infinite loop 5 | * Return: 0 6 | */ 7 | 8 | int main(void) 9 | { 10 | int i; 11 | 12 | printf("Infinite loop incoming :(\n"); 13 | 14 | i = 0; 15 | /** 16 | * while (i < 10) 17 | * { 18 | * putchar(i); 19 | *} 20 | */ 21 | printf("Infinite loop avoided! \\o/\n"); 22 | 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x03-debugging/2-largest_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * largest_number - returns the largest of 3 numbers 5 | * @a: first integer 6 | * @b: second integer 7 | * @c: third integer 8 | * Return: largest number 9 | */ 10 | 11 | int largest_number(int a, int b, int c) 12 | { 13 | int largest; 14 | 15 | if (a >= b && a >= c) 16 | { 17 | largest = a; 18 | } 19 | else if (b >= a && b >= c) 20 | { 21 | largest = b; 22 | } 23 | else 24 | { 25 | largest = c; 26 | } 27 | 28 | return (largest); 29 | } 30 | -------------------------------------------------------------------------------- /0x03-debugging/3-print_remaining_days.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_remaining_days - takes a date and prints how many days are 5 | * left in the year, taking leap years into account 6 | * @month: month in number format 7 | * @day: day of month 8 | * @year: year 9 | * Return: void 10 | */ 11 | 12 | void print_remaining_days(int month, int day, int year) 13 | { 14 | if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) 15 | { 16 | if (month >= 3 && day >= 60) 17 | { 18 | day++; 19 | } 20 | 21 | printf("Day of the year: %d\n", day); 22 | printf("Remaining days: %d\n", 366 - day); 23 | } 24 | else 25 | { 26 | if (month == 2 && day == 60) 27 | { 28 | printf("Invalid date: %02d/%02d/%04d\n", month, day - 31, year); 29 | } 30 | else 31 | { 32 | printf("Day of the year: %d\n", day); 33 | printf("Remaining days: %d\n", 365 - day); 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /0x03-debugging/README.md: -------------------------------------------------------------------------------- 1 | This is debugging class 2 | -------------------------------------------------------------------------------- /0x03-debugging/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | #include 5 | 6 | void positive_or_negative(int i); 7 | int largest_number(int, int, int); 8 | void print_remaining_days(int, int, int); 9 | int convert_day(int month, int day); 10 | 11 | #endif /* MAIN_H */ 12 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/0-isupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isupper -> this is a function to check upper case alphas 5 | * @c: an integer paramer 6 | * Return: something but success 7 | */ 8 | int _isupper(int c) 9 | { 10 | return (c >= 'A' && c <= 'Z'); 11 | } 12 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/1-isdigit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isdigit - funtion to check for a digit 5 | * @c: an integer argument 6 | * Return: success 7 | */ 8 | int _isdigit(int c) 9 | { 10 | return (c >= '0' && c <= '9'); 11 | } 12 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/10-print_triangle.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_triangle - prints a triangle, followed by a new line 5 | * @size: size of the triangle 6 | */ 7 | void print_triangle(int size) 8 | { 9 | if (size <= 0) 10 | { 11 | _putchar('\n'); 12 | } else 13 | { 14 | int i, j; 15 | 16 | for (i = 1; i <= size; i++) 17 | { 18 | for (j = i; j < size; j++) 19 | { 20 | _putchar(' '); 21 | } 22 | 23 | for (j = 1; j <= i; j++) 24 | { 25 | _putchar('#'); 26 | } 27 | 28 | _putchar('\n'); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/100-prime_factor.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * main - finds and prints the largest prime factor of the number 612852475143 6 | * followed by a new line 7 | * Return: Always 0 (Success) 8 | */ 9 | int main(void) 10 | { 11 | long int n; 12 | long int max; 13 | long int i; 14 | 15 | n = 612852475143; 16 | max = -1; 17 | 18 | while (n % 2 == 0) 19 | { 20 | max = 2; 21 | n /= 2; 22 | } 23 | 24 | for (i = 3; i <= sqrt(n); i = i + 2) 25 | { 26 | while (n % i == 0) 27 | { 28 | max = i; 29 | n = n / i; 30 | } 31 | } 32 | 33 | if (n > 2) 34 | max = n; 35 | 36 | printf("%ld\n", max); 37 | 38 | return (0); 39 | } 40 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/101-print_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_number - prints an integer 5 | * @n: integer to be printed 6 | */ 7 | void print_number(int n) 8 | { 9 | unsigned int n1; 10 | 11 | if (n < 0) 12 | { 13 | n1 = -n; 14 | _putchar('-'); 15 | } else 16 | { 17 | n1 = n; 18 | } 19 | 20 | if (n1 / 10) 21 | { 22 | print_number(n1 / 10); 23 | } 24 | 25 | _putchar((n1 % 10) + '0'); 26 | } 27 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/2-mul.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * mul -> multiplying 2 numbers 5 | * @a: interger param1 6 | * @b: integer param 2 7 | * Return: success and intiger 8 | */ 9 | 10 | int mul(int a, int b) 11 | { 12 | return (a * b); 13 | } 14 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/3-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_numbers -> print numbers 5 | */ 6 | 7 | void print_numbers(void) 8 | { 9 | int x; 10 | 11 | for (x = 0; x < 10; x++) 12 | _putchar(x + '0'); 13 | _putchar('\n'); 14 | } 15 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/4-print_most_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_most_numbers -> this prints most numbers 5 | */ 6 | void print_most_numbers(void) 7 | { 8 | int x; 9 | 10 | for (x = 0; x < 10; x++) 11 | { 12 | if (x != 2 && x != 4) 13 | _putchar(x + '0'); 14 | } 15 | _putchar('\n'); 16 | } 17 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/5-more_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * more_numbers -> printing more numbers 5 | */ 6 | void more_numbers(void) 7 | { 8 | int x, y; 9 | 10 | for (x = 0; x < 10; x++) 11 | { 12 | for (y = 0; y < 15; y++) 13 | { 14 | if (y >= 10) 15 | _putchar((y / 10) + '0'); 16 | _putchar((y % 10) + '0'); 17 | } 18 | _putchar('\n'); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/6-print_line.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_line -> printing line 5 | * @n: integer params 6 | */ 7 | void print_line(int n) 8 | { 9 | int x; 10 | 11 | if (n <= 0) 12 | _putchar('\n'); 13 | else 14 | { 15 | for (x = 0; x < n; x++) 16 | _putchar(95); 17 | _putchar('\n'); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/7-print_diagonal.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_diagonal -> printing line 5 | * @n: integer params 6 | */ 7 | void print_diagonal(int n) 8 | { 9 | int x, i; 10 | 11 | if (n <= 0) 12 | _putchar('\n'); 13 | else 14 | { 15 | for (x = 0; x < n; x++) 16 | { 17 | for (i = 0; i < x; i++) 18 | _putchar(' '); 19 | _putchar(92); 20 | _putchar('\n'); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/8-print_square.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_square -> printing line 5 | * @size: integer params 6 | */ 7 | void print_square(int size) 8 | { 9 | int x, y; 10 | 11 | if (size <= 0) 12 | _putchar('\n'); 13 | else 14 | { 15 | for (x = 0; x < size; x++) 16 | { 17 | for (y = 0; y < size; y++) 18 | { 19 | _putchar(35); 20 | } 21 | _putchar('\n'); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/9-fizz_buzz.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - prints the numbers from 1 to 100, followed by a new line 6 | * but for multiples of three prints Fizz instead of the number 7 | * and for the multiples of five prints Buzz 8 | * Return: Always 0 (Success) 9 | */ 10 | int main(void) 11 | { 12 | int i; 13 | 14 | for (i = 1; i <= 100; i++) 15 | { 16 | if (i % 3 == 0 && i % 5 != 0) 17 | { 18 | printf(" Fizz"); 19 | } else if (i % 5 == 0 && i % 3 != 0) 20 | { 21 | printf(" Buzz"); 22 | } else if (i % 3 == 0 && i % 5 == 0) 23 | { 24 | printf(" FizzBuzz"); 25 | } else if (i == 1) 26 | { 27 | printf("%d", i); 28 | } else 29 | { 30 | printf(" %d", i); 31 | } 32 | } 33 | printf("\n"); 34 | 35 | return (0); 36 | } 37 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/README.md: -------------------------------------------------------------------------------- 1 | This is a readme file for this project 2 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _putchar - writes the character c to stdout 5 | * @c: The character to print 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | int _putchar(char c) 11 | { 12 | return (write(1, &c, 1)); 13 | } 14 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | int _putchar(char c); 4 | int _isupper(int c); 5 | int _isdigit(int c); 6 | int mul(int a, int b); 7 | void print_numbers(void); 8 | void print_most_numbers(void); 9 | void more_numbers(void); 10 | void print_line(int n); 11 | void print_diagonal(int n); 12 | void print_square(int size); 13 | void print_triangle(int size); 14 | void print_number(int n); 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/0-reset_to_98.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * reset_to_98 -> a function to update the reference of a pointer 5 | * @n: pointer to n to be updated 6 | */ 7 | void reset_to_98(int *n) 8 | { 9 | *n = 98; 10 | } 11 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/1-swap.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * swap_int -> given two integers swap the values they are holding 5 | * @a: parameter 1 6 | * @b: parameter 2 7 | */ 8 | void swap_int(int *a, int *b) 9 | { 10 | int tmp; 11 | 12 | tmp = *a; 13 | *a = *b; 14 | *b = tmp; 15 | } 16 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/100-atoi.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _atoi - Convert a string to an integer. 5 | * @s: The pointer to convert 6 | * 7 | * Return: A integer 8 | */ 9 | int _atoi(char *s) 10 | { 11 | int c = 0; 12 | unsigned int ni = 0; 13 | int min = 1; 14 | int isi = 0; 15 | 16 | while (s[c]) 17 | { 18 | if (s[c] == 45) 19 | { 20 | min *= -1; 21 | } 22 | 23 | while (s[c] >= 48 && s[c] <= 57) 24 | { 25 | isi = 1; 26 | ni = (ni * 10) + (s[c] - '0'); 27 | c++; 28 | } 29 | 30 | if (isi == 1) 31 | { 32 | break; 33 | } 34 | 35 | c++; 36 | } 37 | 38 | ni *= min; 39 | return (ni); 40 | } 41 | 42 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/2-strlen.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * _strlen -> function to get the length of a string 5 | * @s: string pointer to passed to this function 6 | * Return: returns length of the string 7 | */ 8 | int _strlen(char *s) 9 | { 10 | int len; 11 | 12 | for(; *s != '\0'; s++) 13 | { 14 | len += 1; 15 | } 16 | return (len); 17 | } 18 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/3-puts.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _puts -> this is a function that puts 5 | * @str: a param to _puts function 6 | */ 7 | void _puts(char *str) 8 | { 9 | for (; *str != '\0'; str++) 10 | { 11 | _putchar(*str); 12 | } 13 | _putchar('\n'); 14 | } 15 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/4-print_rev.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_rev -> printing a string in reverse 5 | * @s: the string to be printed in rev 6 | */ 7 | 8 | void print_rev(char *s) 9 | { 10 | int i, n; 11 | 12 | n = 0; 13 | while (s[n] != '\0') 14 | n++; 15 | 16 | for (i = n - 1; i >= 0; i--) 17 | { 18 | _putchar(s[i]); 19 | } 20 | _putchar('\n'); 21 | } 22 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/5-rev_string.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * rev_string -> reversing a string 5 | * @s: parameter s 6 | */ 7 | void rev_string(char *s) 8 | { 9 | char tmp; 10 | int i, length1, length2; 11 | 12 | length1 = 0; 13 | length2 = 0; 14 | 15 | while (s[length1] != '\0') 16 | length1++; 17 | 18 | length2 = length1 - 1; 19 | for (i = 0; i < length1 / 2; i++) 20 | { 21 | tmp = s[i]; 22 | s[i] = s[length2]; 23 | s[length2] = tmp; 24 | length2 -= 1; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/6-puts2.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * puts2 -> puts2 function 6 | * @str: str parameter 7 | */ 8 | void puts2(char *str) 9 | { 10 | int len, i; 11 | 12 | len = strlen(str); 13 | for (i = 0; i < len; i += 2) 14 | _putchar(str[i]); 15 | _putchar('\n'); 16 | } 17 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/7-puts_half.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * puts_half -> puts half 6 | * @str: string param 7 | */ 8 | void puts_half(char *str) 9 | { 10 | int x, y, i; 11 | 12 | x = strlen(str); 13 | if (x % 2 == 1) 14 | y = x / 2 + 1; 15 | else 16 | y = x / 2; 17 | for (i = y; i < x; i++) 18 | _putchar(str[i]); 19 | _putchar('\n'); 20 | } 21 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/8-print_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * print_array - Prints n elements of an array of integers 6 | * @a: Array of integers 7 | * @n: Number of elements of the array to be printed 8 | * 9 | * Return: void 10 | */ 11 | void print_array(int *a, int n) 12 | { 13 | int j; 14 | 15 | for (j = 0; j < n; j++) 16 | { 17 | printf("%d", a[j]); 18 | 19 | if (j != (n - 1)) 20 | { 21 | printf(", "); 22 | } 23 | } 24 | 25 | printf("\n"); 26 | } 27 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/9-strcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strcpy - Copy a string 5 | * @dest: Destination value 6 | * @src: Source value 7 | * 8 | * Return: the pointer to dest 9 | */ 10 | char *_strcpy(char *dest, char *src) 11 | { 12 | int i; 13 | 14 | for (i = 0; src[i] != '\0'; i++) 15 | { 16 | dest[i] = src[i]; 17 | } 18 | 19 | dest[i++] = '\0'; 20 | 21 | return (dest); 22 | } 23 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | ## Pointers, Arrays and Strings 2 | **98 Battery st:** Write a function that takes a pointer to an int as parameter and updates the value it points to to 98. 3 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/_putchar.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | /** 5 | * _putchar - writes the character c to stdout 6 | * @c: The character to print 7 | * 8 | * Return: On success 1. 9 | * On error, -1 is returned, and errno is set appropriately. 10 | */ 11 | int _putchar(char c) 12 | { 13 | return (write(1, &c, 1)); 14 | } 15 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | int _putchar(char c); 4 | void reset_to_98(int *n); 5 | void swap_int(int *a, int *b); 6 | int _strlen(char *s); 7 | void _puts(char *str); 8 | void rev_string(char *s); 9 | void puts2(char *str); 10 | void puts_half(char *str); 11 | void print_array(int *a, int n); 12 | char *_strcpy(char *dest, char *src); 13 | int _atoi(char *s); 14 | #endif 15 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/0-strcat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | 4 | /** 5 | * _strcat -> this is a funcion strcat 6 | * @dest: first param 7 | * @src: second param 8 | * Return: a string 9 | */ 10 | char *_strcat(char *dest, char *src) 11 | { 12 | int len = 0, i; 13 | 14 | while (dest[len]) 15 | len++; 16 | 17 | for (i = 0; src[i] != 0; i++) 18 | { 19 | dest[len] = src[i]; 20 | len += 1; 21 | } 22 | dest[len] = '\0'; 23 | return (dest); 24 | } 25 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/1-strncat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _strncat -> function to append some charx 6 | * @dest: first param 7 | * @src: second param 8 | * @n: 3rd param 9 | * Return: string 10 | */ 11 | char *_strncat(char *dest, char *src, int n) 12 | { 13 | strncat(dest, src, n); 14 | return (dest); 15 | } 16 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/2-strncpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * _strncpy -> for copying purpose 5 | * @dest: param1 6 | * @src: param2 7 | * @n: param3 8 | * Return: string 9 | */ 10 | char *_strncpy(char *dest, char *src, int n) 11 | { 12 | strncpy(dest, src, n); 13 | return (dest); 14 | } 15 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/3-strcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * _strcmp -> for comparing purpose 5 | * @s1: param1 6 | * @s2: param2 7 | * Return: integer 8 | */ 9 | int _strcmp(char *s1, char *s2) 10 | { 11 | int result; 12 | 13 | result = strcmp(s1, s2); 14 | return (result); 15 | } 16 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/4-rev_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * reverse_array -> reversing an array 5 | * @a: array a 6 | * @n: an element of an array 7 | */ 8 | void reverse_array(int *a, int n) 9 | { 10 | int *p, i, aux, k; 11 | 12 | p = a; 13 | for (i = 0; i < n; i++) 14 | p++; 15 | for (k = 0; k < i / 2; k++) 16 | { 17 | aux = a[k]; 18 | a[k] = *p; 19 | *p = aux; 20 | p--; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/5-string_toupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * string_toupper -> converting any string to upper case 5 | * @x: string param 6 | * Return: string 7 | */ 8 | char *string_toupper(char *x) 9 | { 10 | int i = 0; 11 | 12 | while (x[i]) 13 | { 14 | if (x[i] >= 97 && x[i] <= 122) 15 | x[i] = x[i] - 32; 16 | i++; 17 | } 18 | return (x); 19 | } 20 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/6-cap_string.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * cap_string -> capitalization function 5 | * @x: string param 6 | * Return: capitalized version of the string 7 | */ 8 | char *cap_string(char *x) 9 | { 10 | char spc[] = {32, 9, '\n', ',', ';', '.', '!', '?', '"', '(', ')', '{', '}'}; 11 | int len = 13; 12 | int a = 0, i; 13 | 14 | while (x[a]) 15 | { 16 | i = 0; 17 | while (i < len) 18 | { 19 | if ((a == 0 || x[a - 1] == spc[i]) && (x[a] >= 97 && x[a] <= 122)) 20 | x[a] = x[a] - 32; 21 | i++; 22 | } 23 | a++; 24 | } 25 | return (x); 26 | } 27 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/7-leet.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * leet -> a leet function 5 | * @x: param x 6 | * Return: a string 7 | */ 8 | char *leet(char *x) 9 | { 10 | int a = 0, b, l = 5; 11 | char tr[5] = {'A', 'E', 'O', 'T', 'L'}; 12 | char trw[5] = {'4', '3', '0', '7', '1'}; 13 | 14 | while (x[a]) 15 | { 16 | b = 0; 17 | 18 | while (b < l) 19 | { 20 | if (x[a] == tr[b] || x[a] - 32 == tr[b]) 21 | x[a] = trw[b]; 22 | b++; 23 | } 24 | a++; 25 | } 26 | return (x); 27 | } 28 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | this is a 2nd pointer, array and strings assignment 2 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _putchar - writes the character c to stdout 5 | * @c: The character to print 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | int _putchar(char c) 11 | { 12 | return (write(1, &c, 1)); 13 | } 14 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | int _putchar(char c); 4 | char *_strcat(char *dest, char *src); 5 | char *_strncat(char *dest, char *src, int n); 6 | char *_strncpy(char *dest, char *src, int n); 7 | int _strcmp(char *s1, char *s2); 8 | void reverse_array(int *a, int n); 9 | char *string_toupper(char *); 10 | char *cap_string(char *); 11 | char *leet(char *); 12 | #endif 13 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/0-memset.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _memset -> this memory set function 6 | * @s: string 7 | * @b: a charcter 8 | * @n: an integer 9 | * Return: a string 10 | */ 11 | char *_memset(char *s, char b, unsigned int n) 12 | { 13 | unsigned int a; 14 | 15 | for (a = 0; a < n; a++) 16 | s[a] = b; 17 | 18 | return (s); 19 | } 20 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/1-memcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _memcpy -> memory copy 5 | * @dest: is destination memory 6 | * @src: is source memory 7 | * @n: number of bytes to be copied 8 | * Return: string copied from source 9 | */ 10 | char *_memcpy(char *dest, char *src, unsigned int n) 11 | { 12 | unsigned int a; 13 | 14 | for (a = 0; a < n; a++) 15 | dest[a] = src[a]; 16 | return (dest); 17 | } 18 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/100-set_string.c: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * set_string - Sets the value of a pointer to char 4 | * @s: the value to modify 5 | * @to: the value to assign 6 | * 7 | * Return: Nothing. 8 | */ 9 | void set_string(char **s, char *to) 10 | { 11 | *s = to; 12 | } 13 | 14 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/2-strchr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strchr -> string character 5 | * @s: string given 6 | * @c: another char 7 | * Return: a string 8 | */ 9 | char *_strchr(char *s, char c) 10 | { 11 | int a; 12 | 13 | while (1) 14 | { 15 | a = *s++; 16 | if (a == c) 17 | { 18 | return (s - 1); 19 | } 20 | if (a == 0) 21 | { 22 | return (NULL); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/3-strspn.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strspn - search a string for a set of bytes 5 | * @s: source string 6 | * @accept: accepted string 7 | * 8 | * Return: number of bytes in the init segment 9 | */ 10 | unsigned int _strspn(char *s, char *accept) 11 | { 12 | unsigned int a = 0, b, t = 0; 13 | 14 | while (accept[a]) 15 | { 16 | b = 0; 17 | 18 | while (s[b] != 32) 19 | { 20 | if (accept[a] == s[b]) 21 | { 22 | t++; 23 | } 24 | 25 | b++; 26 | } 27 | 28 | a++; 29 | } 30 | 31 | return (t); 32 | } 33 | 34 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/4-strpbrk.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strpbrk - search a string for any of a set of bytes 5 | * @s: source string 6 | * @accept: accepted characters 7 | * 8 | * Return: the string since the first found accepted character 9 | */ 10 | char *_strpbrk(char *s, char *accept) 11 | { 12 | int a = 0, b; 13 | 14 | while (s[a]) 15 | { 16 | b = 0; 17 | 18 | while (accept[b]) 19 | { 20 | if (s[a] == accept[b]) 21 | { 22 | s += a; 23 | return (s); 24 | } 25 | 26 | b++; 27 | } 28 | 29 | a++; 30 | } 31 | 32 | return ('\0'); 33 | } 34 | 35 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/5-strstr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strstr - locate a substring 5 | * @haystack: the string to search 6 | * @needle: the string to find 7 | * 8 | * Return: char value 9 | */ 10 | char *_strstr(char *haystack, char *needle) 11 | { 12 | int a = 0, b = 0; 13 | 14 | while (haystack[a]) 15 | { 16 | while (needle[b]) 17 | { 18 | if (haystack[a + b] != needle[b]) 19 | { 20 | break; 21 | } 22 | 23 | b++; 24 | } 25 | 26 | if (needle[b] == '\0') 27 | { 28 | return (haystack + a); 29 | } 30 | 31 | a++; 32 | } 33 | 34 | return ('\0'); 35 | } 36 | 37 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/7-print_chessboard.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_chessboard - Print the chessboard 5 | * @a: array of pieces 6 | * 7 | * Return: Nothing. 8 | */ 9 | void print_chessboard(char (*a)[8]) 10 | { 11 | int b, c; 12 | 13 | for (b = 0; b < 8; b++) 14 | { 15 | for (c = 0; c < 8; c++) 16 | { 17 | _putchar(a[b][c]); 18 | } 19 | 20 | _putchar('\n'); 21 | } 22 | } 23 | 24 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/8-print_diagsums.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * print_diagsums - Print the sum of two diagonals of square matrix 6 | * 7 | * @a: the matrix 8 | * @size: the size 9 | * 10 | * Return: Nothing. 11 | */ 12 | void print_diagsums(int *a, int size) 13 | { 14 | int b, sum1 = 0, sum2 = 0; 15 | 16 | for (b = 0; b < size; b++) 17 | { 18 | sum1 += a[(size + 1) * b]; 19 | sum2 += a[(size - 1) * (b + 1)]; 20 | } 21 | 22 | printf("%d, %d\n", sum1, sum2); 23 | } 24 | 25 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | # this even more pointers arrays and strings section 2 | -------------------------------------------------------------------------------- /0x07-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 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | int _putchar(char c); 4 | char *_memset(char *s, char b, unsigned int n); 5 | char *_memcpy(char *dest, char *src, unsigned int n); 6 | char *_strchr(char *s, char c); 7 | unsigned int _strspn(char *s, char *accept); 8 | char *_strpbrk(char *s, char *accept); 9 | char *_strstr(char *haystack, char *needle); 10 | void print_chessboard(char (*a)[8]); 11 | void set_string(char **s, char *to); 12 | void print_diagsums(int *a, int size); 13 | 14 | #endif 15 | -------------------------------------------------------------------------------- /0x08-recursion/0-puts_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _puts_recursion -> this is a function to print string recursivly 5 | * @s: string paramenter 6 | */ 7 | void _puts_recursion(char *s) 8 | { 9 | if (*s == '\0') 10 | { 11 | _putchar('\n'); 12 | return; 13 | } 14 | _putchar(*s); 15 | s++; 16 | _puts_recursion(s); 17 | } 18 | -------------------------------------------------------------------------------- /0x08-recursion/1-print_rev_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _print_rev_recursion -> printing string in reverse using recursion 5 | * @s: string s 6 | */ 7 | void _print_rev_recursion(char *s) 8 | { 9 | if (*s == '\0') 10 | return; 11 | s++; 12 | _print_rev_recursion(s); 13 | s--; 14 | _putchar(*s); 15 | } 16 | -------------------------------------------------------------------------------- /0x08-recursion/100-is_palindrome.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | int check_pal(char *s, int i, int len); 4 | int _strlen_recursion(char *s); 5 | 6 | /** 7 | * is_palindrome - checks if a string is a palindrome 8 | * @s: string to reverse 9 | * 10 | * Return: 1 if it is, 0 it's not 11 | */ 12 | int is_palindrome(char *s) 13 | { 14 | if (*s == 0) 15 | return (1); 16 | return (check_pal(s, 0, _strlen_recursion(s))); 17 | } 18 | 19 | /** 20 | * _strlen_recursion - returns the length of a string 21 | * @s: string to calculate the length of 22 | * 23 | * Return: length of the string 24 | */ 25 | int _strlen_recursion(char *s) 26 | { 27 | if (*s == '\0') 28 | return (0); 29 | return (1 + _strlen_recursion(s + 1)); 30 | } 31 | 32 | /** 33 | * check_pal - checks the characters recursively for palindrome 34 | * @s: string to check 35 | * @i: iterator 36 | * @len: length of the string 37 | * 38 | * Return: 1 if palindrome, 0 if not 39 | */ 40 | int check_pal(char *s, int i, int len) 41 | { 42 | if (*(s + i) != *(s + len - 1)) 43 | return (0); 44 | if (i >= len) 45 | return (1); 46 | return (check_pal(s, i + 1, len - 1)); 47 | } 48 | -------------------------------------------------------------------------------- /0x08-recursion/100-wildcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | int bandersnatch(char *s1, char *s2); 4 | char *move(char *s2); 5 | 6 | /** 7 | * wildcmp - compares two strings recursively, 8 | * checking for wildcards expansion 9 | * @s1: first string to compare 10 | * @s2: second string to compare 11 | * 12 | * Return: 1 if the strings can be considered identical 13 | * otherwise 0 14 | */ 15 | int wildcmp(char *s1, char *s2) 16 | { 17 | /** 18 | * this is going to be a sum of return values 19 | */ 20 | int sum = 0; 21 | 22 | /** 23 | * if we reach the end of s1 and the char in s2 is a * 24 | * and if the next chars of s2 are *, return 1 25 | */ 26 | if (*s1 == '\0' && *s2 == '*' && !*move(s2)) 27 | return (1); 28 | 29 | /** 30 | * if the chars are equal in both strings, 31 | * if we reached the end of s1, return 1 32 | * else increment s1 and s2 by 1 33 | */ 34 | if (*s1 == *s2) 35 | { 36 | if (*s1 == '\0') 37 | return (1); 38 | return (wildcmp(s1 + 1, s2 + 1)); 39 | } 40 | /** 41 | * if we reached the end of both strings, 42 | * return 0 43 | */ 44 | if (*s1 == '\0' || *s2 == '\0') 45 | return (0); 46 | 47 | /** 48 | * if the char in s2 is a * 49 | * finds the address of the first char after the * 50 | * if we reached the end of s2, return 1 51 | * if the chars are equal, add the return values 52 | * of wildcmp() to sum 53 | * add the return value of bandersnatch() to sum 54 | * convert non-zero to 1, keeps 0 at 0, return 55 | */ 56 | if (*s2 == '*') 57 | { 58 | s2 = move(s2); 59 | if (*s2 == '\0') 60 | return (1); 61 | if (*s1 == *s2) 62 | sum += wildcmp(s1 + 1, s2 + 1); 63 | sum += bandersnatch(s1 + 1, s2); 64 | return (!!sum); 65 | } 66 | return (0); 67 | } 68 | 69 | /** 70 | * bandersnatch - checks recursively for all the paths when the 71 | * characters are equal 72 | * @s1: first string 73 | * @s2: second string 74 | * 75 | * Return: return value of wildcmp() or of itself 76 | */ 77 | int bandersnatch(char *s1, char *s2) 78 | { 79 | /** 80 | * if we reached the end of s1, return 0 81 | * if chars are equal, return the return value of wildcmp() 82 | * increment s1 by 1, not s2 83 | */ 84 | if (*s1 == '\0') 85 | return (0); 86 | if (*s1 == *s2) 87 | return (wildcmp(s1, s2)); 88 | return (bandersnatch(s1 + 1, s2)); 89 | } 90 | 91 | /** 92 | * *move - moves the current char past the * 93 | * @s2: string to iterate over 94 | * 95 | * Return: the address of the character after the * 96 | */ 97 | char *move(char *s2) 98 | { 99 | /** 100 | * if the current char is a * 101 | * increment s2 by 1 102 | * else return the address of 103 | * the first char past all * 104 | */ 105 | if (*s2 == '*') 106 | return (move(s2 + 1)); 107 | else 108 | return (s2); 109 | } 110 | -------------------------------------------------------------------------------- /0x08-recursion/101-wildcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | int bandersnatch(char *s1, char *s2); 4 | char *move(char *s2); 5 | 6 | /** 7 | * wildcmp - compares two strings recursively, 8 | * checking for wildcards expansion 9 | * @s1: first string to compare 10 | * @s2: second string to compare 11 | * 12 | * Return: 1 if the strings can be considered identical 13 | * otherwise 0 14 | */ 15 | int wildcmp(char *s1, char *s2) 16 | { 17 | /** 18 | * this is going to be a sum of return values 19 | */ 20 | int sum = 0; 21 | 22 | /** 23 | * if we reach the end of s1 and the char in s2 is a * 24 | * and if the next chars of s2 are *, return 1 25 | */ 26 | if (*s1 == '\0' && *s2 == '*' && !*move(s2)) 27 | return (1); 28 | 29 | /** 30 | * if the chars are equal in both strings, 31 | * if we reached the end of s1, return 1 32 | * else increment s1 and s2 by 1 33 | */ 34 | if (*s1 == *s2) 35 | { 36 | if (*s1 == '\0') 37 | return (1); 38 | return (wildcmp(s1 + 1, s2 + 1)); 39 | } 40 | /** 41 | * if we reached the end of both strings, 42 | * return 0 43 | */ 44 | if (*s1 == '\0' || *s2 == '\0') 45 | return (0); 46 | 47 | /** 48 | * if the char in s2 is a * 49 | * finds the address of the first char after the * 50 | * if we reached the end of s2, return 1 51 | * if the chars are equal, add the return values 52 | * of wildcmp() to sum 53 | * add the return value of bandersnatch() to sum 54 | * convert non-zero to 1, keeps 0 at 0, return 55 | */ 56 | if (*s2 == '*') 57 | { 58 | s2 = move(s2); 59 | if (*s2 == '\0') 60 | return (1); 61 | if (*s1 == *s2) 62 | sum += wildcmp(s1 + 1, s2 + 1); 63 | sum += bandersnatch(s1 + 1, s2); 64 | return (!!sum); 65 | } 66 | return (0); 67 | } 68 | 69 | /** 70 | * bandersnatch - checks recursively for all the paths when the 71 | * characters are equal 72 | * @s1: first string 73 | * @s2: second string 74 | * 75 | * Return: return value of wildcmp() or of itself 76 | */ 77 | int bandersnatch(char *s1, char *s2) 78 | { 79 | /** 80 | * if we reached the end of s1, return 0 81 | * if chars are equal, return the return value of wildcmp() 82 | * increment s1 by 1, not s2 83 | */ 84 | if (*s1 == '\0') 85 | return (0); 86 | if (*s1 == *s2) 87 | return (wildcmp(s1, s2)); 88 | return (bandersnatch(s1 + 1, s2)); 89 | } 90 | 91 | /** 92 | * *move - moves the current char past the * 93 | * @s2: string to iterate over 94 | * 95 | * Return: the address of the character after the * 96 | */ 97 | char *move(char *s2) 98 | { 99 | /** 100 | * if the current char is a * 101 | * increment s2 by 1 102 | * else return the address of 103 | * the first char past all * 104 | */ 105 | if (*s2 == '*') 106 | return (move(s2 + 1)); 107 | else 108 | return (s2); 109 | } 110 | -------------------------------------------------------------------------------- /0x08-recursion/2-strlen_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strlen_recursion -> function to get length of string 5 | * @s: string param 6 | * Return: length of string 7 | */ 8 | int _strlen_recursion(char *s) 9 | { 10 | if (*s == '\0') 11 | return (0); 12 | s++; 13 | return (1 + _strlen_recursion(s)); 14 | } 15 | -------------------------------------------------------------------------------- /0x08-recursion/3-factorial.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * factorial -> function factorial 5 | * @n: number 6 | * Return: an integer 7 | */ 8 | int factorial(int n) 9 | { 10 | if (n < 0) 11 | return (-1); 12 | if (n <= 1) 13 | return (1); 14 | return (n * factorial(n - 1)); 15 | } 16 | -------------------------------------------------------------------------------- /0x08-recursion/4-pow_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _pow_recursion -> power using recursion 5 | * @x: x ^ y 6 | * @y: the power number 7 | * Return: power result 8 | */ 9 | int _pow_recursion(int x, int y) 10 | { 11 | if (y < 0) 12 | return (-1); 13 | if (y == 0) 14 | return (1); 15 | return (x * _pow_recursion(x, y - 1)); 16 | } 17 | -------------------------------------------------------------------------------- /0x08-recursion/5-sqrt_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | int _sqrt(int, int); 4 | 5 | /** 6 | * _sqrt_recursion -> sqrt using recursion 7 | * @n: parameter to be sqrt 8 | * Return: square root of a number 9 | */ 10 | int _sqrt_recursion(int n) 11 | { 12 | return (_sqrt(n, 1)); 13 | } 14 | 15 | /** 16 | * _sqrt -> recursive square root 17 | * @n: number 18 | * @i: iterator 19 | * Return: a number 20 | */ 21 | int _sqrt(int n, int i) 22 | { 23 | int square = i * i; 24 | 25 | if (square > n) 26 | return (-1); 27 | if (square == n) 28 | return (i); 29 | return (_sqrt(n, i + 1)); 30 | } 31 | -------------------------------------------------------------------------------- /0x08-recursion/6-is_prime_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | int check_prime(int, int); 4 | /** 5 | * is_prime_number -> prime identifier 6 | * @n: is number 7 | * Return: integer 8 | */ 9 | int is_prime_number(int n) 10 | { 11 | return (check_prime(n, 1)); 12 | } 13 | 14 | 15 | /** 16 | * check_prime -> it check prime numbers 17 | * @n: number 18 | * @i: iterator 19 | * Return: a 1 or 0 20 | */ 21 | int check_prime(int n, int i) 22 | { 23 | if (n <= 1) 24 | return (0); 25 | if (n % i == 0 && i > 1) 26 | return (0); 27 | if ((n / i) < i) 28 | return (1); 29 | return (check_prime(n, i + 1)); 30 | } 31 | 32 | -------------------------------------------------------------------------------- /0x08-recursion/README.md: -------------------------------------------------------------------------------- 1 | # Recursion Problem solutions 2 | -------------------------------------------------------------------------------- /0x08-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 | -------------------------------------------------------------------------------- /0x08-recursion/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | int _putchar(char c); 4 | void _puts_recursion(char *s); 5 | void _print_rev_recursion(char *s); 6 | int _strlen_recursion(char *s); 7 | int factorial(int n); 8 | int _pow_recursion(int x, int y); 9 | int is_prime_number(int n); 10 | int wildcmp(char *s1, char *s2); 11 | #endif 12 | -------------------------------------------------------------------------------- /0x09-static_libraries/.gitignore: -------------------------------------------------------------------------------- 1 | *.o 2 | *.c 3 | -------------------------------------------------------------------------------- /0x09-static_libraries/README.md: -------------------------------------------------------------------------------- 1 | # this all about static libraries 2 | -------------------------------------------------------------------------------- /0x09-static_libraries/create_static_lib.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -Wall -Wextra -Werror -pedantic -c *.c 3 | ar rc liball.a *.o 4 | -------------------------------------------------------------------------------- /0x09-static_libraries/libmy.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/saminegash1/alx-low_level_programming/de8b604aa103d44a37596e90a340cc5c0af91e8a/0x09-static_libraries/libmy.a -------------------------------------------------------------------------------- /0x09-static_libraries/main: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/saminegash1/alx-low_level_programming/de8b604aa103d44a37596e90a340cc5c0af91e8a/0x09-static_libraries/main -------------------------------------------------------------------------------- /0x09-static_libraries/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | int _putchar(char c); 4 | int _islower(int c); 5 | int _isalpha(int c); 6 | int _abs(int n); 7 | int _isupper(int c); 8 | int _isdigit(int c); 9 | int _strlen(char *s); 10 | void _puts(char *s); 11 | char *_strcpy(char *dest, char *src); 12 | int _atoi(char *s); 13 | char *_strcat(char *dest, char *src); 14 | char *_strncat(char *dest, char *src, int n); 15 | char *_strncpy(char *dest, char *src, int n); 16 | int _strcmp(char *s1, char *s2); 17 | char *_memset(char *s, char b, unsigned int n); 18 | char *_memcpy(char *dest, char *src, unsigned int n); 19 | char *_strchr(char *s, char c); 20 | unsigned int _strspn(char *s, char *accept); 21 | char *_strpbrk(char *s, char *accept); 22 | char *_strstr(char *haystack, char *needle); 23 | #endif 24 | -------------------------------------------------------------------------------- /0x0A-argc_argv/0-whatsmyname.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main -> this is a function to print its name 5 | * @argc: argc parameter 6 | * @argv: an array of a command listed 7 | * Return: 0 for success 8 | */ 9 | int main(int argc __attribute__((unused)), char *argv[]) 10 | { 11 | printf("%s\n", *argv); 12 | return (0); 13 | } 14 | -------------------------------------------------------------------------------- /0x0A-argc_argv/1-args.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main -> this is a function to print its name 5 | * @argc: argc parameter 6 | * @argv: an array of a command listed 7 | * Return: 0 for success 8 | */ 9 | int main(int argc, char *argv[] __attribute__((unused))) 10 | { 11 | printf("%d\n", argc - 1); 12 | return (0); 13 | } 14 | -------------------------------------------------------------------------------- /0x0A-argc_argv/100-change.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * main -> this is a function to print its name 6 | * @argc: argc parameter 7 | * @argv: an array of a command listed 8 | * Return: 0 for success 9 | */ 10 | int main(int argc, char *argv[]) 11 | { 12 | int num, j, result = 0; 13 | int coins[] = {25, 10, 5, 2, 1}; 14 | 15 | if (argc != 2) 16 | { 17 | printf("%s\n", "Error"); 18 | return (1); 19 | } 20 | 21 | num = atoi(argv[1]); 22 | if (num < 0) 23 | { 24 | printf("0\n"); 25 | return (0); 26 | } 27 | for (j = 0; j < 5 && num >= 0; j++) 28 | { 29 | while (num >= coins[j]) 30 | { 31 | num -= coins[j]; 32 | result++; 33 | } 34 | } 35 | printf("%d\n", result); 36 | return (0); 37 | } 38 | -------------------------------------------------------------------------------- /0x0A-argc_argv/2-args.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main -> this is a function to print its name 5 | * @argc: argc parameter 6 | * @argv: an array of a command listed 7 | * Return: 0 for success 8 | */ 9 | int main(int argc, char *argv[]) 10 | { 11 | int i; 12 | 13 | for (i = 0; i < argc; i++) 14 | { 15 | printf("%s\n", argv[i]); 16 | } 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x0A-argc_argv/3-mul.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * main -> this is a function to print its name 6 | * @argc: argc parameter 7 | * @argv: an array of a command listed 8 | * Return: 0 for success 9 | */ 10 | int main(int argc, char *argv[]) 11 | { 12 | int result, num1, num2; 13 | 14 | if (argc != 3) 15 | { 16 | printf("%s\n", "Error"); 17 | return (1); 18 | } 19 | num1 = atoi(argv[1]); 20 | num2 = atoi(argv[2]); 21 | result = num1 * num2; 22 | 23 | printf("%d\n", result); 24 | return (0); 25 | } 26 | -------------------------------------------------------------------------------- /0x0A-argc_argv/4-add.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * main -> this is a function to print its name 6 | * @argc: argc parameter 7 | * @argv: an array of a command listed 8 | * Return: 0 for success 9 | */ 10 | int main(int argc, char *argv[]) 11 | { 12 | int result = 0, num, i, j, k; 13 | 14 | for (i = 1; i < argc; i++) 15 | { 16 | for (j = 0; argv[i][j] != '\0'; j++) 17 | { 18 | if (argv[i][j] > '9' || argv[i][j] < '0') 19 | { 20 | printf("%s\n", "Error"); 21 | return (1); 22 | } 23 | 24 | } 25 | } 26 | 27 | for (k = 1; k < argc; k++) 28 | { 29 | num = atoi(argv[k]); 30 | result += num; 31 | } 32 | printf("%d\n", result); 33 | return (0); 34 | } 35 | -------------------------------------------------------------------------------- /0x0A-argc_argv/README.md: -------------------------------------------------------------------------------- 1 | # this is all about argc and argv 2 | -------------------------------------------------------------------------------- /0x0A-argc_argv/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | int _putchar(char c); 4 | #endif 5 | -------------------------------------------------------------------------------- /0x0B-malloc_free/0-create_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * create_array -> creating array 5 | * @size: size of array to be created 6 | * @c: character an array is initialized with 7 | * Return: a pointer to an array 8 | */ 9 | char *create_array(unsigned int size, char c) 10 | { 11 | unsigned int i; 12 | char *s; 13 | 14 | if (size == 0) 15 | return (NULL); 16 | s = (char *)malloc(size * sizeof(char)); 17 | if (s == NULL) 18 | return (NULL); 19 | for (i = 0; i < size; i++) 20 | s[i] = c; 21 | 22 | return (s); 23 | } 24 | -------------------------------------------------------------------------------- /0x0B-malloc_free/1-strdup.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strdup -> string duplicator function 5 | * @str: string to be dupliated 6 | * Return: a string pointer 7 | */ 8 | char *_strdup(char *str) 9 | { 10 | int i = 1, j = 0; 11 | char *s; 12 | 13 | if (str == NULL) 14 | return (NULL); 15 | while (str[i]) 16 | i++; 17 | s = (char *)malloc(i * sizeof(char) + 1); 18 | if (s == NULL) 19 | return (NULL); 20 | while (j < i) 21 | { 22 | s[j] = str[j]; 23 | j++; 24 | } 25 | s[j] = '\0'; 26 | return (s); 27 | } 28 | -------------------------------------------------------------------------------- /0x0B-malloc_free/100-argstostr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * argstostr - Concatenates all arguments of the program into a string; 5 | * arguments are separated by a new line in the string. 6 | * @ac: The number of arguments passed to the program. 7 | * @av: An array of pointers to the arguments. 8 | * 9 | * Return: If ac == 0, av == NULL, or the function fails - NULL. 10 | * Otherwise - a pointer to the new string. 11 | */ 12 | char *argstostr(int ac, char **av) 13 | { 14 | char *str; 15 | int arg, byte, index, size = ac; 16 | 17 | if (ac == 0 || av == NULL) 18 | return (NULL); 19 | 20 | for (arg = 0; arg < ac; arg++) 21 | { 22 | for (byte = 0; av[arg][byte]; byte++) 23 | size++; 24 | } 25 | 26 | str = malloc(sizeof(char) * size + 1); 27 | 28 | if (str == NULL) 29 | return (NULL); 30 | 31 | index = 0; 32 | 33 | for (arg = 0; arg < ac; arg++) 34 | { 35 | for (byte = 0; av[arg][byte]; byte++) 36 | str[index++] = av[arg][byte]; 37 | 38 | str[index++] = '\n'; 39 | } 40 | 41 | str[size] = '\0'; 42 | 43 | return (str); 44 | } 45 | -------------------------------------------------------------------------------- /0x0B-malloc_free/101-strtow.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | int word_len(char *str); 4 | int count_words(char *str); 5 | char **strtow(char *str); 6 | 7 | /** 8 | * word_len - Locates the index marking the end of the 9 | * first word contained within a string. 10 | * @str: The string to be searched. 11 | * 12 | * Return: The index marking the end of the initial word pointed to by str. 13 | */ 14 | int word_len(char *str) 15 | { 16 | int index = 0, len = 0; 17 | 18 | while (*(str + index) && *(str + index) != ' ') 19 | { 20 | len++; 21 | index++; 22 | } 23 | 24 | return (len); 25 | } 26 | 27 | /** 28 | * count_words - Counts the number of words contained within a string. 29 | * @str: The string to be searched. 30 | * 31 | * Return: The number of words contained within str. 32 | */ 33 | int count_words(char *str) 34 | { 35 | int index = 0, words = 0, len = 0; 36 | 37 | for (index = 0; *(str + index); index++) 38 | len++; 39 | 40 | for (index = 0; index < len; index++) 41 | { 42 | if (*(str + index) != ' ') 43 | { 44 | words++; 45 | index += word_len(str + index); 46 | } 47 | } 48 | 49 | return (words); 50 | } 51 | 52 | /** 53 | * strtow - Splits a string into words. 54 | * @str: The string to be split. 55 | * 56 | * Return: If str = NULL, str = "", or the function fails - NULL. 57 | * Otherwise - a pointer to an array of strings (words). 58 | */ 59 | char **strtow(char *str) 60 | { 61 | char **strings; 62 | int index = 0, words, w, letters, l; 63 | 64 | if (str == NULL || str[0] == '\0') 65 | return (NULL); 66 | 67 | words = count_words(str); 68 | if (words == 0) 69 | return (NULL); 70 | 71 | strings = malloc(sizeof(char *) * (words + 1)); 72 | if (strings == NULL) 73 | return (NULL); 74 | 75 | for (w = 0; w < words; w++) 76 | { 77 | while (str[index] == ' ') 78 | index++; 79 | 80 | letters = word_len(str + index); 81 | 82 | strings[w] = malloc(sizeof(char) * (letters + 1)); 83 | 84 | if (strings[w] == NULL) 85 | { 86 | for (; w >= 0; w--) 87 | free(strings[w]); 88 | 89 | free(strings); 90 | return (NULL); 91 | } 92 | 93 | for (l = 0; l < letters; l++) 94 | strings[w][l] = str[index++]; 95 | 96 | strings[w][l] = '\0'; 97 | } 98 | strings[w] = NULL; 99 | 100 | return (strings); 101 | } 102 | -------------------------------------------------------------------------------- /0x0B-malloc_free/2-str_concat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * str_concat -> string concatinating function 5 | * @s1: string 1 6 | * @s2: string 2 7 | * Return: string 1 + string 2 8 | */ 9 | char *str_concat(char *s1, char *s2) 10 | { 11 | int i = 0, j = 0, l = 0, k = 0; 12 | char *s; 13 | 14 | if (s1 == NULL) 15 | s1 = ""; 16 | if (s2 == NULL) 17 | s2 = ""; 18 | while (s1[i]) 19 | i++; 20 | while (s2[j]) 21 | j++; 22 | 23 | l = i + j; 24 | s = (char *)malloc(l * sizeof(char) + 1); 25 | if (s == NULL) 26 | return (NULL); 27 | j = 0; 28 | while (k < l) 29 | { 30 | if (k < i) 31 | s[k] = s1[k]; 32 | if (k >= i) 33 | { 34 | s[k] = s2[j]; 35 | j++; 36 | } 37 | k++; 38 | } 39 | s[k] = '\0'; 40 | return (s); 41 | } 42 | -------------------------------------------------------------------------------- /0x0B-malloc_free/3-alloc_grid.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * alloc_grid -> allocatingn 2d array 5 | * @width: width of an array 6 | * @height: height of an array 7 | * Return: a pointer to a allocated grid 8 | */ 9 | int **alloc_grid(int width, int height) 10 | { 11 | int i, j, k, l; 12 | int **a; 13 | 14 | if (width <= 0 || height <= 0) 15 | return (NULL); 16 | a = malloc(height * sizeof(int *)); 17 | if (a == NULL) 18 | { 19 | free(a); 20 | return (NULL); 21 | } 22 | for (i = 0; i < height; i++) 23 | { 24 | a[i] = malloc(width * sizeof(int)); 25 | if (a[i] == NULL) 26 | { 27 | for (j = i; j >= 0; j--) 28 | { 29 | free(a[j]); 30 | } 31 | free(a); 32 | return (NULL); 33 | } 34 | } 35 | 36 | for (k = 0; k < height; k++) 37 | { 38 | for (l = 0; l < width; l++) 39 | a[k][l] = 0; 40 | } 41 | return (a); 42 | } 43 | 44 | 45 | -------------------------------------------------------------------------------- /0x0B-malloc_free/4-free_grid.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * free_grid -> removing memory allocation 5 | * @grid: grid to be freed 6 | * @height: height of matrix 7 | * Return: nothing 8 | */ 9 | void free_grid(int **grid, int height) 10 | { 11 | int i; 12 | 13 | for (i = 0; i < height; i++) 14 | free(grid[i]); 15 | free(grid); 16 | } 17 | -------------------------------------------------------------------------------- /0x0B-malloc_free/README.md: -------------------------------------------------------------------------------- 1 | # This is all about dynamic memory allocation 2 | 3 | -------------------------------------------------------------------------------- /0x0B-malloc_free/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | #include 4 | #include 5 | int _putchar(char c); 6 | char *create_array(unsigned int size, char c); 7 | char *str_concat(char *s1, char *s2); 8 | int **alloc_grid(int width, int height); 9 | void free_grid(int **grid, int height); 10 | #endif 11 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/0-malloc_checked.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * malloc_checked - Allocates memory using malloc. 6 | * @b: The number of bytes to be allocated. 7 | * 8 | * Return: A pointer to the allocated memory. 9 | */ 10 | void *malloc_checked(unsigned int b) 11 | { 12 | void *mem = malloc(b); 13 | 14 | if (mem == NULL) 15 | exit(98); 16 | 17 | return (mem); 18 | } 19 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/1-string_nconcat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * string_nconcat - Concatenates two strings using at 6 | * most an inputted number of bytes. 7 | * @s1: The first string. 8 | * @s2: The second string. 9 | * @n: The maximum number of bytes of s2 to concatenate to s1. 10 | * 11 | * Return: If the function fails - NULL. 12 | * Otherwise - a pointer to the concatenated space in memory. 13 | */ 14 | char *string_nconcat(char *s1, char *s2, unsigned int n) 15 | { 16 | char *concat; 17 | unsigned int len = n, index; 18 | 19 | if (s1 == NULL) 20 | s1 = ""; 21 | 22 | if (s2 == NULL) 23 | s2 = ""; 24 | 25 | for (index = 0; s1[index]; index++) 26 | len++; 27 | 28 | concat = malloc(sizeof(char) * (len + 1)); 29 | 30 | if (concat == NULL) 31 | return (NULL); 32 | 33 | len = 0; 34 | 35 | for (index = 0; s1[index]; index++) 36 | concat[len++] = s1[index]; 37 | 38 | for (index = 0; s2[index] && index < n; index++) 39 | concat[len++] = s2[index]; 40 | 41 | concat[len] = '\0'; 42 | 43 | return (concat); 44 | } 45 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/100-realloc.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _realloc - Reallocates a memory block using malloc and free. 6 | * @ptr: A pointer to the memory previously allocated. 7 | * @old_size: The size in bytes of the allocated space for ptr. 8 | * @new_size: The size in bytes for the new memory block. 9 | * 10 | * Return: If new_size == old_size - ptr. 11 | * If new_size == 0 and ptr is not NULL - NULL. 12 | * Otherwise - a pointer to the reallocated memory block. 13 | */ 14 | void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size) 15 | { 16 | void *mem; 17 | char *ptr_copy, *filler; 18 | unsigned int index; 19 | 20 | if (new_size == old_size) 21 | return (ptr); 22 | 23 | if (ptr == NULL) 24 | { 25 | mem = malloc(new_size); 26 | 27 | if (mem == NULL) 28 | return (NULL); 29 | 30 | return (mem); 31 | } 32 | 33 | if (new_size == 0 && ptr != NULL) 34 | { 35 | free(ptr); 36 | return (NULL); 37 | } 38 | 39 | ptr_copy = ptr; 40 | mem = malloc(sizeof(*ptr_copy) * new_size); 41 | 42 | if (mem == NULL) 43 | { 44 | free(ptr); 45 | return (NULL); 46 | } 47 | 48 | filler = mem; 49 | 50 | for (index = 0; index < old_size && index < new_size; index++) 51 | filler[index] = *ptr_copy++; 52 | 53 | free(ptr); 54 | return (mem); 55 | } 56 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/101-mul.c: -------------------------------------------------------------------------------- 1 | 2 | #include "main.h" 3 | #include 4 | #include 5 | 6 | int find_len(char *str); 7 | char *create_xarray(int size); 8 | char *iterate_zeroes(char *str); 9 | void get_prod(char *prod, char *mult, int digit, int zeroes); 10 | void add_nums(char *final_prod, char *next_prod, int next_len); 11 | 12 | /** 13 | * find_len - Finds the length of a string. 14 | * @str: The string to be measured. 15 | * 16 | * Return: The length of the string. 17 | */ 18 | int find_len(char *str) 19 | { 20 | int len = 0; 21 | 22 | while (*str++) 23 | len++; 24 | 25 | return (len); 26 | } 27 | 28 | /** 29 | * create_xarray - Creates an array of chars and initializes it with 30 | * the character 'x'. Adds a terminating null byte. 31 | * @size: The size of the array to be initialized. 32 | * 33 | * Description: If there is insufficient space, the 34 | * function exits with a status of 98. 35 | * Return: A pointer to the array. 36 | */ 37 | char *create_xarray(int size) 38 | { 39 | char *array; 40 | int index; 41 | 42 | array = malloc(sizeof(char) * size); 43 | 44 | if (array == NULL) 45 | exit(98); 46 | 47 | for (index = 0; index < (size - 1); index++) 48 | array[index] = 'x'; 49 | 50 | array[index] = '\0'; 51 | 52 | return (array); 53 | } 54 | 55 | /** 56 | * iterate_zeroes - Iterates through a string of numbers containing 57 | * leading zeroes until it hits a non-zero number. 58 | * @str: The string of numbers to be iterate through. 59 | * 60 | * Return: A pointer to the next non-zero element. 61 | */ 62 | char *iterate_zeroes(char *str) 63 | { 64 | while (*str && *str == '0') 65 | str++; 66 | 67 | return (str); 68 | } 69 | 70 | /** 71 | * get_digit - Converts a digit character to a corresponding int. 72 | * @c: The character to be converted. 73 | * 74 | * Description: If c is a non-digit, the function 75 | * exits with a status of 98. 76 | * Return: The converted int. 77 | */ 78 | int get_digit(char c) 79 | { 80 | int digit = c - '0'; 81 | 82 | if (digit < 0 || digit > 9) 83 | { 84 | printf("Error\n"); 85 | exit(98); 86 | } 87 | 88 | return (digit); 89 | } 90 | 91 | /** 92 | * get_prod - Multiplies a string of numbers by a single digit. 93 | * @prod: The buffer to store the result. 94 | * @mult: The string of numbers. 95 | * @digit: The single digit. 96 | * @zeroes: The necessary number of leading zeroes. 97 | * 98 | * Description: If mult contains a non-digit, the function 99 | * exits with a status value of 98. 100 | */ 101 | void get_prod(char *prod, char *mult, int digit, int zeroes) 102 | { 103 | int mult_len, num, tens = 0; 104 | 105 | mult_len = find_len(mult) - 1; 106 | mult += mult_len; 107 | 108 | while (*prod) 109 | { 110 | *prod = 'x'; 111 | prod++; 112 | } 113 | 114 | prod--; 115 | 116 | while (zeroes--) 117 | { 118 | *prod = '0'; 119 | prod--; 120 | } 121 | 122 | for (; mult_len >= 0; mult_len--, mult--, prod--) 123 | { 124 | if (*mult < '0' || *mult > '9') 125 | { 126 | printf("Error\n"); 127 | exit(98); 128 | } 129 | 130 | num = (*mult - '0') * digit; 131 | num += tens; 132 | *prod = (num % 10) + '0'; 133 | tens = num / 10; 134 | } 135 | 136 | if (tens) 137 | *prod = (tens % 10) + '0'; 138 | } 139 | 140 | /** 141 | * add_nums - Adds the numbers stored in two strings. 142 | * @final_prod: The buffer storing the running final product. 143 | * @next_prod: The next product to be added. 144 | * @next_len: The length of next_prod. 145 | */ 146 | void add_nums(char *final_prod, char *next_prod, int next_len) 147 | { 148 | int num, tens = 0; 149 | 150 | while (*(final_prod + 1)) 151 | final_prod++; 152 | 153 | while (*(next_prod + 1)) 154 | next_prod++; 155 | 156 | for (; *final_prod != 'x'; final_prod--) 157 | { 158 | num = (*final_prod - '0') + (*next_prod - '0'); 159 | num += tens; 160 | *final_prod = (num % 10) + '0'; 161 | tens = num / 10; 162 | 163 | next_prod--; 164 | next_len--; 165 | } 166 | 167 | for (; next_len >= 0 && *next_prod != 'x'; next_len--) 168 | { 169 | num = (*next_prod - '0'); 170 | num += tens; 171 | *final_prod = (num % 10) + '0'; 172 | tens = num / 10; 173 | 174 | final_prod--; 175 | next_prod--; 176 | } 177 | 178 | if (tens) 179 | *final_prod = (tens % 10) + '0'; 180 | } 181 | 182 | /** 183 | * main - Multiplies two positive numbers. 184 | * @argv: The number of arguments passed to the program. 185 | * @argc: An array of pointers to the arguments. 186 | * 187 | * Description: If the number of arguments is incorrect or one number 188 | * contains non-digits, the function exits with a status of 98. 189 | * Return: Always 0. 190 | */ 191 | int main(int argc, char *argv[]) 192 | { 193 | char *final_prod, *next_prod; 194 | int size, index, digit, zeroes = 0; 195 | 196 | if (argc != 3) 197 | { 198 | printf("Error\n"); 199 | exit(98); 200 | } 201 | 202 | if (*(argv[1]) == '0') 203 | argv[1] = iterate_zeroes(argv[1]); 204 | if (*(argv[2]) == '0') 205 | argv[2] = iterate_zeroes(argv[2]); 206 | if (*(argv[1]) == '\0' || *(argv[2]) == '\0') 207 | { 208 | printf("0\n"); 209 | return (0); 210 | } 211 | 212 | size = find_len(argv[1]) + find_len(argv[2]); 213 | final_prod = create_xarray(size + 1); 214 | next_prod = create_xarray(size + 1); 215 | 216 | for (index = find_len(argv[2]) - 1; index >= 0; index--) 217 | { 218 | digit = get_digit(*(argv[2] + index)); 219 | get_prod(next_prod, argv[1], digit, zeroes++); 220 | add_nums(final_prod, next_prod, size - 1); 221 | } 222 | for (index = 0; final_prod[index]; index++) 223 | { 224 | if (final_prod[index] != 'x') 225 | putchar(final_prod[index]); 226 | } 227 | putchar('\n'); 228 | 229 | free(next_prod); 230 | free(final_prod); 231 | 232 | return (0); 233 | } 234 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/2-calloc.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _calloc - Allocates memory for an array of a certain number 6 | * of elements each of an inputted byte size. 7 | * @nmemb: The number of elements. 8 | * @size: The byte size of each array element. 9 | * 10 | * Return: If nmemb = 0, size = 0, or the function fails - NULL. 11 | * Otherwise - a pointer to the allocated memory. 12 | */ 13 | void *_calloc(unsigned int nmemb, unsigned int size) 14 | { 15 | void *mem; 16 | char *filler; 17 | unsigned int index; 18 | 19 | if (nmemb == 0 || size == 0) 20 | return (NULL); 21 | 22 | mem = malloc(size * nmemb); 23 | 24 | if (mem == NULL) 25 | return (NULL); 26 | 27 | filler = mem; 28 | 29 | for (index = 0; index < (size * nmemb); index++) 30 | filler[index] = '\0'; 31 | 32 | return (mem); 33 | } 34 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/3-array_range.c: -------------------------------------------------------------------------------- 1 | 2 | #include "main.h" 3 | #include 4 | 5 | /** 6 | * array_range - Creates an array of integers ordered 7 | * from min to max, inclusive. 8 | * @min: The first value of the array. 9 | * @max: The last value of the array. 10 | * 11 | * Return: If min > max or the function fails - NULL. 12 | * Otherwise - a pointer to the newly created array. 13 | */ 14 | int *array_range(int min, int max) 15 | { 16 | int *array, index, size; 17 | 18 | if (min > max) 19 | return (NULL); 20 | 21 | size = max - min + 1; 22 | 23 | array = malloc(sizeof(int) * size); 24 | 25 | if (array == NULL) 26 | return (NULL); 27 | 28 | for (index = 0; index < size; index++) 29 | array[index] = min++; 30 | 31 | return (array); 32 | } 33 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/README.md: -------------------------------------------------------------------------------- 1 | # C - More malloc, free 2 | 3 | In this project, I learned about using `exit`, `calloc`, and `realloc` in C. 4 | 5 | ## Tests :heavy_check_mark: 6 | 7 | * [tests](./tests): Folder of test files. Provided by Holberton School. 8 | 9 | ## Header File :file_folder: 10 | 11 | * [holberton.h](./holberton.h): Header file containing prototypes for all functions written in 12 | the project. 13 | 14 | | File | Prototype | 15 | | -------------------- | -------------------------------------------------------------------------- | 16 | | `0-malloc_checked.c` | `void *malloc_checked(unsigned int b);` | 17 | | `1-string_nconcat.c` | `char *string_nconcat(char *s1, char *s2, unsigned int n);` | 18 | | `2-calloc.c` | `char *string_nconcat(char *s1, char *s2, unsigned int n);` | 19 | | `3-array_range.c` | `int *array_range(int min, int max);` | 20 | | `100-realloc.c` | `void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size);` | 21 | 22 | ## Tasks :page_with_curl: 23 | 24 | * **0. Trust no one** 25 | * [0-malloc_checked.c](./0-malloc_checked.c): C function that returns a 26 | pointer to a newly-allocated space in memory using `malloc`. 27 | * If `malloc` fails, the function causes normal process termination with a status value 28 | of `98`. 29 | 30 | * **1. string_nconcat** 31 | * [1-string_nconcat.c](./1-string_nconcat.c): C function that returns a pointer to a 32 | newly-allocated space in memory containing the concatenation of two strings. 33 | * The returned pointer contains `s1` followed by the first `n` bytes 34 | of `s2`, null-terminated. 35 | * If `n` is greater than or equal to the length of `s2`, the entire string `s2` is used. 36 | * If `NULL` is passed, the function treats the parameter as an empty string. 37 | * If the function fails - returns `NULL`. 38 | 39 | * **2. _calloc** 40 | * [2-calloc.c](./2-calloc.c): C function that returns a pointer to a newly-allocated space 41 | in memory for an array, using `malloc`. 42 | * Allocates memory for an array of `nmemb` elements of `size` bytes each. 43 | * The memory is set to zero. 44 | * If `nmemb` = 0, `size` = `0`, or the function fail - returns `NULL`. 45 | 46 | * **3. array_range** 47 | * [3-array_range.c](./3-array_range.c): C function that returns a pointer to a 48 | newly-allocated space in memory containing an array of integers. 49 | * The array contains all the values from parameters `min` to `max`, inclusive, 50 | ordered from `min` to `max`. 51 | * If `min > max` or the function fails - returns `NULL`. 52 | 53 | * **4. _realloc** 54 | * [100-realloc.c](./100-realloc.c): C function that reallocates a memory block using 55 | `malloc` and `free`. 56 | * The parameter `ptr` is a pointer to the memory previously allocated with 57 | a call to `malloc: malloc(old_size)`. 58 | * The paramter `old_size` is the size, in bytes, of the allocated space for `ptr`. 59 | * The paramter `new_size` is the new size, in bytes, of the new memory block. 60 | * The contens of `ptr` are copied to the newly-allocated space in the range from the 61 | start of `ptr` up to the minimum of `old_size` and `new_size`. 62 | * If `new_size` > `old_size`, the "added" memory is not initialized. 63 | * If `new_size` == `old_size`, the function returns `ptr`. 64 | * If `ptr` is `NULL`, the call is equivalent to `malloc(new_size)` for all values of 65 | `old_size` and `new_size`. 66 | * If `new_size` = 0 and `ptr` is not `NULL`, the call is equivalent to 67 | `free(ptr)` and the function returns `NULL`. 68 | 69 | * **5. We must accept finite disappointment, but never lose infinite hope** 70 | * [101-mul.c](./101-mul.c): C program that multiplies two positive numbers. 71 | * Usage: `mul num1 num2`. 72 | * The function assumes `num1` and `num2` are passed in base 10. 73 | * Prints the result followed by a new line. 74 | * If the number of arguments is incorrect or either of `num1` or `num2` 75 | contains non-digits, the function prints `Error` followed by a new line and 76 | exits with a status of `98`. 77 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | /** 5 | * File: MAIN.h 6 | */ 7 | 8 | int _putchar(char c); 9 | void *malloc_checked(unsigned int b); 10 | char *string_nconcat(char *s1, char *s2, unsigned int n); 11 | void *_calloc(unsigned int nmemb, unsigned int size); 12 | int *array_range(int min, int max); 13 | void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size); 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /0x0C-preprocessor/0-object_like_macro.h: -------------------------------------------------------------------------------- 1 | #ifndef OBJECT_LIKE_MACRO_H 2 | #define OBJECT_LIKE_MACRO_H 3 | 4 | #define SIZE 1024 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /0x0C-preprocessor/1-pi.h: -------------------------------------------------------------------------------- 1 | #ifndef PI_H 2 | #define PI_H 3 | 4 | #define PI 3.14159265359 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /0x0C-preprocessor/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Prints the name of the file of the program 5 | * was compiled from, followed by a new line. 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | printf("%s\n", __FILE__); 11 | 12 | return (0); 13 | } 14 | -------------------------------------------------------------------------------- /0x0C-preprocessor/3-function_like_macro.h: -------------------------------------------------------------------------------- 1 | #ifndef FUNCTION_LIKE_MACRO_H 2 | #define FUNCTION_LIKE_MACRO_H 3 | 4 | #define ABS(x) ((x) < 0 ? (-x) : (x)) 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /0x0C-preprocessor/4-sum.h: -------------------------------------------------------------------------------- 1 | #ifndef SUM_H 2 | #define SUM_H 3 | 4 | #define SUM(x, y) ((x) + (y)) 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /0x0C-preprocessor/README.md: -------------------------------------------------------------------------------- 1 | preprocessor, 2 | -------------------------------------------------------------------------------- /0x0D-preprocessor/0-object_like_macro.h: -------------------------------------------------------------------------------- 1 | #ifndef OBJECT_LIKE_MACRO_H 2 | #define OBJECT_LIKE_MACRO_H 3 | 4 | #define SIZE 1024 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /0x0D-preprocessor/1-pi.h: -------------------------------------------------------------------------------- 1 | #ifndef PI_H 2 | #define PI_H 3 | 4 | #define PI 3.14159265359 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /0x0D-preprocessor/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Prints the name of the file of the program 5 | * was compiled from, followed by a new line. 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | printf("%s\n", __FILE__); 11 | 12 | return (0); 13 | } 14 | -------------------------------------------------------------------------------- /0x0D-preprocessor/3-function_like_macro.h: -------------------------------------------------------------------------------- 1 | #ifndef FUNCTION_LIKE_MACRO_H 2 | #define FUNCTION_LIKE_MACRO_H 3 | 4 | #define ABS(x) ((x) < 0 ? (-x) : (x)) 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /0x0D-preprocessor/4-sum.h: -------------------------------------------------------------------------------- 1 | #ifndef SUM_H 2 | #define SUM_H 3 | 4 | #define SUM(x, y) ((x) + (y)) 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /0x0D-preprocessor/README.md: -------------------------------------------------------------------------------- 1 | preprocessor, 2 | -------------------------------------------------------------------------------- /0x0D-structures_typedef/1-init_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | 4 | /** 5 | * init_dog - Initializes a variable of type struct dog. 6 | * @d: The dog to be initialized. 7 | * @name: The name of the dog. 8 | * @age: The age of the dog. 9 | * @owner: The owner of the dog. 10 | */ 11 | void init_dog(struct dog *d, char *name, float age, char *owner) 12 | { 13 | if (d != NULL) 14 | { 15 | d->name = name; 16 | d->age = age; 17 | d->owner = owner; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x0D-structures_typedef/2-print_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * print_dog - Prints a struct dog. 7 | * @d: The struct dog to be printed. 8 | */ 9 | void print_dog(struct dog *d) 10 | { 11 | if (d == NULL) 12 | return; 13 | 14 | if (d->name == NULL) 15 | printf("Name: (nil)\n"); 16 | else 17 | printf("Name: %s\n", d->name); 18 | 19 | if (d->age < 0) 20 | printf("Age: (nil)\n"); 21 | else 22 | printf("Age: %f\n", d->age); 23 | 24 | if (d->owner == NULL) 25 | printf("Owner: (nil)\n"); 26 | else 27 | printf("Owner: %s\n", d->owner); 28 | } 29 | -------------------------------------------------------------------------------- /0x0D-structures_typedef/4-new_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | 4 | int _strlen(char *str); 5 | char *_strcopy(char *dest, char *src); 6 | dog_t *new_dog(char *name, float age, char *owner); 7 | 8 | /** 9 | * _strlen - Finds the length of a string. 10 | * @str: The string to be measured. 11 | * 12 | * Return: The length of the string. 13 | */ 14 | int _strlen(char *str) 15 | { 16 | int len = 0; 17 | 18 | while (*str++) 19 | len++; 20 | 21 | return (len); 22 | } 23 | 24 | /** 25 | * _strcopy - Copies a string pointed to by src, including the 26 | * terminating null byte, to a buffer pointed to by dest. 27 | * @dest: The buffer storing the string copy. 28 | * @src: The source string. 29 | * 30 | * Return: The pointer to dest. 31 | */ 32 | char *_strcopy(char *dest, char *src) 33 | { 34 | int index = 0; 35 | 36 | for (index = 0; src[index]; index++) 37 | dest[index] = src[index]; 38 | 39 | dest[index] = '\0'; 40 | 41 | return (dest); 42 | } 43 | 44 | /** 45 | * new_dog - Creates a new dog. 46 | * @name: The name of the dog. 47 | * @age: The age of the dog. 48 | * @owner: The owner of the dog. 49 | * 50 | * Return: The new struct dog. 51 | */ 52 | dog_t *new_dog(char *name, float age, char *owner) 53 | { 54 | dog_t *doggo; 55 | 56 | if (name == NULL || age < 0 || owner == NULL) 57 | return (NULL); 58 | 59 | doggo = malloc(sizeof(dog_t)); 60 | if (doggo == NULL) 61 | return (NULL); 62 | 63 | doggo->name = malloc(sizeof(char) * (_strlen(name) + 1)); 64 | if (doggo->name == NULL) 65 | { 66 | free(doggo); 67 | return (NULL); 68 | } 69 | 70 | doggo->owner = malloc(sizeof(char) * (_strlen(owner) + 1)); 71 | if (doggo->owner == NULL) 72 | { 73 | free(doggo->name); 74 | free(doggo); 75 | return (NULL); 76 | } 77 | 78 | doggo->name = _strcopy(doggo->name, name); 79 | doggo->age = age; 80 | doggo->owner = _strcopy(doggo->owner, owner); 81 | 82 | return (doggo); 83 | } 84 | -------------------------------------------------------------------------------- /0x0D-structures_typedef/5-free_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | 4 | /** 5 | * free_dog - Frees dogs. 6 | * @d: The dog to be freed. 7 | */ 8 | void free_dog(dog_t *d) 9 | { 10 | if (d == NULL) 11 | return; 12 | 13 | free(d->owner); 14 | free(d->name); 15 | free(d); 16 | } 17 | -------------------------------------------------------------------------------- /0x0D-structures_typedef/README.md: -------------------------------------------------------------------------------- 1 | structrue and type def 2 | -------------------------------------------------------------------------------- /0x0D-structures_typedef/dog.h: -------------------------------------------------------------------------------- 1 | #ifndef DOG_H 2 | #define DOG_H 3 | /** 4 | * struct dog - A new type describing a dog. 5 | * @name: The name of the dog. 6 | * @age: The age of the dog. 7 | * @owner: The owner of the dog. 8 | */ 9 | struct dog 10 | { 11 | char *name; 12 | float age; 13 | char *owner; 14 | }; 15 | 16 | /** 17 | * dog_t - Typedef for struct dog 18 | */ 19 | typedef struct dog dog_t; 20 | 21 | void init_dog(struct dog *d, char *name, float age, char *owner); 22 | void print_dog(struct dog *d); 23 | dog_t *new_dog(char *name, float age, char *owner); 24 | void free_dog(dog_t *d); 25 | 26 | #endif 27 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/1-init_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | 4 | /** 5 | * init_dog - Initializes a variable of type struct dog. 6 | * @d: The dog to be initialized. 7 | * @name: The name of the dog. 8 | * @age: The age of the dog. 9 | * @owner: The owner of the dog. 10 | */ 11 | void init_dog(struct dog *d, char *name, float age, char *owner) 12 | { 13 | if (d != NULL) 14 | { 15 | d->name = name; 16 | d->age = age; 17 | d->owner = owner; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/2-print_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * print_dog - Prints a struct dog. 7 | * @d: The struct dog to be printed. 8 | */ 9 | void print_dog(struct dog *d) 10 | { 11 | if (d == NULL) 12 | return; 13 | 14 | if (d->name == NULL) 15 | printf("Name: (nil)\n"); 16 | else 17 | printf("Name: %s\n", d->name); 18 | 19 | if (d->age < 0) 20 | printf("Age: (nil)\n"); 21 | else 22 | printf("Age: %f\n", d->age); 23 | 24 | if (d->owner == NULL) 25 | printf("Owner: (nil)\n"); 26 | else 27 | printf("Owner: %s\n", d->owner); 28 | } 29 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/4-new_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | 4 | int _strlen(char *str); 5 | char *_strcopy(char *dest, char *src); 6 | dog_t *new_dog(char *name, float age, char *owner); 7 | 8 | /** 9 | * _strlen - Finds the length of a string. 10 | * @str: The string to be measured. 11 | * 12 | * Return: The length of the string. 13 | */ 14 | int _strlen(char *str) 15 | { 16 | int len = 0; 17 | 18 | while (*str++) 19 | len++; 20 | 21 | return (len); 22 | } 23 | 24 | /** 25 | * _strcopy - Copies a string pointed to by src, including the 26 | * terminating null byte, to a buffer pointed to by dest. 27 | * @dest: The buffer storing the string copy. 28 | * @src: The source string. 29 | * 30 | * Return: The pointer to dest. 31 | */ 32 | char *_strcopy(char *dest, char *src) 33 | { 34 | int index = 0; 35 | 36 | for (index = 0; src[index]; index++) 37 | dest[index] = src[index]; 38 | 39 | dest[index] = '\0'; 40 | 41 | return (dest); 42 | } 43 | 44 | /** 45 | * new_dog - Creates a new dog. 46 | * @name: The name of the dog. 47 | * @age: The age of the dog. 48 | * @owner: The owner of the dog. 49 | * 50 | * Return: The new struct dog. 51 | */ 52 | dog_t *new_dog(char *name, float age, char *owner) 53 | { 54 | dog_t *doggo; 55 | 56 | if (name == NULL || age < 0 || owner == NULL) 57 | return (NULL); 58 | 59 | doggo = malloc(sizeof(dog_t)); 60 | if (doggo == NULL) 61 | return (NULL); 62 | 63 | doggo->name = malloc(sizeof(char) * (_strlen(name) + 1)); 64 | if (doggo->name == NULL) 65 | { 66 | free(doggo); 67 | return (NULL); 68 | } 69 | 70 | doggo->owner = malloc(sizeof(char) * (_strlen(owner) + 1)); 71 | if (doggo->owner == NULL) 72 | { 73 | free(doggo->name); 74 | free(doggo); 75 | return (NULL); 76 | } 77 | 78 | doggo->name = _strcopy(doggo->name, name); 79 | doggo->age = age; 80 | doggo->owner = _strcopy(doggo->owner, owner); 81 | 82 | return (doggo); 83 | } 84 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/5-free_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | 4 | /** 5 | * free_dog - Frees dogs. 6 | * @d: The dog to be freed. 7 | */ 8 | void free_dog(dog_t *d) 9 | { 10 | if (d == NULL) 11 | return; 12 | 13 | free(d->owner); 14 | free(d->name); 15 | free(d); 16 | } 17 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/README.md: -------------------------------------------------------------------------------- 1 | structrue and type def 2 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/dog.h: -------------------------------------------------------------------------------- 1 | #ifndef DOG_H 2 | #define DOG_H 3 | /** 4 | * struct dog - A new type describing a dog. 5 | * @name: The name of the dog. 6 | * @age: The age of the dog. 7 | * @owner: The owner of the dog. 8 | */ 9 | struct dog 10 | { 11 | char *name; 12 | float age; 13 | char *owner; 14 | }; 15 | 16 | /** 17 | * dog_t - Typedef for struct dog 18 | */ 19 | typedef struct dog dog_t; 20 | 21 | void init_dog(struct dog *d, char *name, float age, char *owner); 22 | void print_dog(struct dog *d); 23 | dog_t *new_dog(char *name, float age, char *owner); 24 | void free_dog(dog_t *d); 25 | 26 | #endif 27 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | this is an alx low level programming assignmens readme file 2 | --------------------------------------------------------------------------------