├── 0x00-hello_world ├── 0-preprocessor ├── 1-compiler ├── 100-intel ├── 101-quote.c ├── 2-assembler ├── 3-name ├── 4-puts.c ├── 5-printf.c ├── 6-size.c └── README.md ├── 0x01-variables_if_else_while ├── 0-positive_or_negative.c ├── 1-last_digit.c ├── 100-print_comb3.c ├── 101-print_comb4.c ├── 102-print_comb5.c ├── 2-print_alphabet.c ├── 3-print_alphabets.c ├── 4-print_alphabt.c ├── 5-print_numbers.c ├── 6-print_numberz.c ├── 7-print_tebahpla.c ├── 8-print_base16.c ├── 9-print_comb.c └── README.md ├── 0x02-functions_nested_loops ├── 0-putchar.c ├── 1-alphabet.c ├── 10-add.c ├── 100-times_table.c ├── 101-natural.c ├── 102-fibonacci.c ├── 103-fibonacci.c ├── 104-fibonacci.c ├── 11-print_to_98.c ├── 2-print_alphabet_x10.c ├── 3-islower.c ├── 4-isalpha.c ├── 5-sign.c ├── 6-abs.c ├── 7-print_last_digit.c ├── 8-24_hours.c ├── 9-times_table.c ├── README.md └── main.h ├── 0x03-debugging ├── 0-main.c ├── 1-main.c ├── 2-largest_number.c ├── 3-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 └── main.h ├── 0x05-pointers_arrays_strings ├── 0-reset_to_98.c ├── 1-swap.c ├── 100-atoi.c ├── 101-keygen.c ├── 2-strlen.c ├── 3-puts.c ├── 4-print_rev.c ├── 5-rev_string.c ├── 6-puts2.c ├── 7-puts_half.c ├── 8-print_array.c ├── 9-strcpy.c ├── README.md └── main.h ├── 0x06-pointers_arrays_strings ├── 0-strcat.c ├── 1-strncat.c ├── 100-rot13.c ├── 101-print_number.c ├── 102-magic.c ├── 103-infinite_add.c ├── 104-print_buffer.c ├── 2-strncpy.c ├── 3-strcmp.c ├── 4-rev_array.c ├── 5-string_toupper.c ├── 6-cap_string.c ├── 7-leet.c ├── README.md └── main.h ├── 0x07-pointers_arrays_strings ├── 0-memset.c ├── 1-memcpy.c ├── 100-set_string.c ├── 101-crackme_password ├── 2-strchr.c ├── 3-strspn.c ├── 4-strpbrk.c ├── 5-strstr.c ├── 7-print_chessboard.c ├── 8-print_diagsums.c ├── README.md └── main.h ├── 0x08-recursion ├── 0-puts_recursion.c ├── 1-print_rev_recursion.c ├── 100-is_palindrome.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 └── main.h ├── 0x09-static_libraries ├── README.md ├── create_static_lib.sh ├── libmy.a └── 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 ├── 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 ├── 0x0D-preprocessor ├── 0-object_like_macro.h ├── 1-pi.h ├── 2-main.c ├── 3-function_like_macro.h ├── 4-sum.h └── README.md ├── 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 | 3 | /** 4 | * main - Prints a text 5 | * 6 | * Return: Always 1 (Success) 7 | */ 8 | int main(void) 9 | { 10 | write(2, "and that piece of art is useful\" - Dora Korpar, 2015-10-19\n", 59); 11 | return (1); 12 | } 13 | -------------------------------------------------------------------------------- /0x00-hello_world/2-assembler: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -S $CFILE 3 | -------------------------------------------------------------------------------- /0x00-hello_world/3-name: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -o cisfun $CFILE 3 | -------------------------------------------------------------------------------- /0x00-hello_world/4-puts.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Prints a text 5 | * 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | puts("\"Programming is like building a multilingual puzzle"); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x00-hello_world/5-printf.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Prints a text 5 | * 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | printf("with proper grammar, but the outcome is a piece of art,\n"); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x00-hello_world/6-size.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Prints the sizeof many var types 5 | * 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | char a; 11 | int b; 12 | long int c; 13 | long long int d; 14 | float e; 15 | 16 | printf("Size of a char: %d byte(s)\n", sizeof(a)); 17 | printf("Size of an int: %d byte(s)\n", sizeof(b)); 18 | printf("Size of a long int: %d byte(s)\n", sizeof(c)); 19 | printf("Size of a long long int: %d byte(s)\n", sizeof(d)); 20 | printf("Size of a float: %d byte(s)\n", sizeof(e)); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x00-hello_world/README.md: -------------------------------------------------------------------------------- 1 | My readme file for 0x00-hello_world 2 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/0-positive_or_negative.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /** 6 | * main - Prints if number is positive, zero or negative 7 | * 8 | * Return: Always (Success) 9 | */ 10 | int main(void) 11 | { 12 | int n; 13 | 14 | srand(time(0)); 15 | n = rand() - RAND_MAX / 2; 16 | 17 | if (n > 0) 18 | { 19 | printf("%d is positive\n", n); 20 | } 21 | else if (n == 0) 22 | { 23 | printf("%d is zero\n", n); 24 | } 25 | else 26 | { 27 | printf("%d is negative\n", n); 28 | } 29 | 30 | return (0); 31 | } 32 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/1-last_digit.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /** 6 | * main - Prints a text according number 7 | * 8 | * Return: Always (Success) 9 | */ 10 | int main(void) 11 | { 12 | int n, lastd; 13 | 14 | srand(time(0)); 15 | n = rand() - RAND_MAX / 2; 16 | lastd = n % 10; 17 | 18 | if (lastd > 5) 19 | { 20 | printf("Last digit of %d is %d and is greater than 5\n", n, lastd); 21 | } 22 | else if (lastd == 0) 23 | { 24 | printf("Last digit of %d is %d and is 0\n", n, lastd); 25 | } 26 | else if (lastd < 6 && lastd != 0) 27 | { 28 | printf("Last digit of %d is %d and is less than 6 and not 0\n", n, lastd); 29 | } 30 | 31 | return (0); 32 | } 33 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/100-print_comb3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Prints combination of numbers 5 | * 6 | * Return: Always (Success) 7 | */ 8 | int main(void) 9 | { 10 | int c, i; 11 | 12 | for (c = '0'; c <= '9'; c++) 13 | { 14 | for (i = '0'; i <= '9'; i++) 15 | { 16 | if (c < i) 17 | { 18 | putchar(c); 19 | putchar(i); 20 | 21 | if (c != '8' || (c == '8' && i != '9')) 22 | { 23 | putchar(','); 24 | putchar(' '); 25 | } 26 | } 27 | } 28 | } 29 | 30 | putchar('\n'); 31 | 32 | return (0); 33 | } 34 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/101-print_comb4.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Prints 3 combination of numbers 5 | * 6 | * Return: Always (Success) 7 | */ 8 | int main(void) 9 | { 10 | int c, i, k; 11 | 12 | for (c = '0'; c <= '9'; c++) 13 | { 14 | for (i = '0'; i <= '9'; i++) 15 | { 16 | for (k = '0'; k <= '9'; k++) 17 | { 18 | if (c < i && i < k) 19 | { 20 | putchar(c); 21 | putchar(i); 22 | putchar(k); 23 | 24 | if (c != '7') 25 | { 26 | putchar(','); 27 | putchar(' '); 28 | } 29 | } 30 | } 31 | } 32 | } 33 | 34 | putchar('\n'); 35 | 36 | return (0); 37 | } 38 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/102-print_comb5.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Prints 3 combination of numbers 5 | * 6 | * Return: Always (Success) 7 | */ 8 | int main(void) 9 | { 10 | int c, i, k, j; 11 | 12 | for (c = 48; c <= 57; c++) 13 | { 14 | for (i = 48; i <= 57; i++) 15 | { 16 | for (k = 48; k <= 57; k++) 17 | { 18 | for (j = 48; j <= 57; j++) 19 | { 20 | if (((k + j) > (c + i) && k >= c) || c < k) 21 | { 22 | putchar(c); 23 | putchar(i); 24 | putchar(' '); 25 | putchar(k); 26 | putchar(j); 27 | 28 | if (c + i + k + j == 227 && c == 57) 29 | { 30 | break; 31 | } 32 | else 33 | { 34 | putchar(','); 35 | putchar(' '); 36 | } 37 | } 38 | } 39 | } 40 | } 41 | } 42 | 43 | putchar('\n'); 44 | 45 | return (0); 46 | } 47 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/2-print_alphabet.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Prints the alphabetic 5 | * 6 | * Return: Always (Success) 7 | */ 8 | int main(void) 9 | { 10 | char c; 11 | 12 | for (c = 'a'; c <= 'z'; c++) 13 | putchar(c); 14 | 15 | putchar('\n'); 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/3-print_alphabets.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Prints the alphabetic in lower and upper case 5 | * 6 | * Return: Always (Success) 7 | */ 8 | int main(void) 9 | { 10 | char c; 11 | 12 | for (c = 'a'; c <= 'z'; c++) 13 | { 14 | putchar(c); 15 | } 16 | 17 | for (c = 'A'; c <= 'Z'; c++) 18 | { 19 | putchar(c); 20 | } 21 | 22 | putchar('\n'); 23 | 24 | return (0); 25 | } 26 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/4-print_alphabt.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Prints some letters of alphabet 5 | * 6 | * Return: Always (Success) 7 | */ 8 | int main(void) 9 | { 10 | char c; 11 | 12 | for (c = 'a'; c <= 'z'; c++) 13 | { 14 | if (c != 'e' && c != 'q') 15 | { 16 | putchar(c); 17 | } 18 | } 19 | 20 | putchar('\n'); 21 | 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/5-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Prints the numbers since 0 to 9 5 | * 6 | * Return: Always (Success) 7 | */ 8 | int main(void) 9 | { 10 | char n; 11 | 12 | for (n = '0'; n <= '9'; n++) 13 | { 14 | putchar(n); 15 | } 16 | 17 | putchar('\n'); 18 | 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/6-print_numberz.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Prints the numbers since 0 to 9 5 | * 6 | * Return: Always (Success) 7 | */ 8 | int main(void) 9 | { 10 | int n; 11 | 12 | for (n = '0'; n <= '9'; n++) 13 | { 14 | putchar(n); 15 | } 16 | 17 | putchar('\n'); 18 | 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/7-print_tebahpla.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Prints the alphabet at reverse 5 | * 6 | * Return: Always (Success) 7 | */ 8 | int main(void) 9 | { 10 | char c; 11 | 12 | for (c = 'z'; c >= 'a'; c--) 13 | { 14 | putchar(c); 15 | } 16 | 17 | putchar('\n'); 18 | 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/8-print_base16.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Prints a hexadecimal string 5 | * 6 | * Return: Always (Success) 7 | */ 8 | int main(void) 9 | { 10 | char c; 11 | 12 | for (c = '0'; c <= '9'; c++) 13 | { 14 | putchar(c); 15 | } 16 | 17 | for (c = 'a'; c <= 'f'; c++) 18 | { 19 | putchar(c); 20 | } 21 | 22 | putchar('\n'); 23 | 24 | return (0); 25 | } 26 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/9-print_comb.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Prints a serie of numbers with commas 5 | * 6 | * Return: Always (Success); 7 | */ 8 | int main(void) 9 | { 10 | int c; 11 | 12 | for (c = '0'; c <= '9'; c++) 13 | { 14 | putchar(c); 15 | 16 | if (c != '9') 17 | { 18 | putchar(','); 19 | putchar(' '); 20 | } 21 | } 22 | 23 | putchar('\n'); 24 | 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/README.md: -------------------------------------------------------------------------------- 1 | Variables and loops 2 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/0-putchar.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | 4 | /** 5 | * main - Entry point 6 | * 7 | * Return: nothing, return void 8 | */ 9 | 10 | int main(void) 11 | { 12 | _putchar('_'); 13 | _putchar('p'); 14 | _putchar('u'); 15 | _putchar('t'); 16 | _putchar('c'); 17 | _putchar('h'); 18 | _putchar('a'); 19 | _putchar('r'); 20 | _putchar('\n'); 21 | 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/1-alphabet.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_alphabet - print alphabet lowercase 5 | * 6 | * Return: Always 0. 7 | */ 8 | 9 | void print_alphabet(void) 10 | { 11 | char alphabet; 12 | 13 | for (alphabet = 'a'; alphabet <= 'z'; alphabet++) 14 | { 15 | _putchar(alphabet); 16 | } 17 | 18 | _putchar('\n'); 19 | 20 | } 21 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/10-add.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * add -prints add two numbers 6 | *@i: print int i 7 | *@k: print int k 8 | * Return: Always 0. 9 | */ 10 | 11 | int add(int i, int k) 12 | { 13 | return (i + k); 14 | } 15 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/100-times_table.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_times_table - Prints the times table of the input, 5 | * starting with 0. 6 | * @n: The value of the times table to be printed. 7 | */ 8 | void print_times_table(int n) 9 | { 10 | int num, mult, prod; 11 | 12 | if (n >= 0 && n <= 15) 13 | { 14 | for (num = 0; num <= n; num++) 15 | { 16 | _putchar('0'); 17 | 18 | for (mult = 1; mult <= n; mult++) 19 | { 20 | _putchar(','); 21 | _putchar(' '); 22 | 23 | prod = num * mult; 24 | 25 | if (prod <= 99) 26 | _putchar(' '); 27 | if (prod <= 9) 28 | _putchar(' '); 29 | 30 | if (prod >= 100) 31 | { 32 | _putchar((prod / 100) + '0'); 33 | _putchar(((prod / 10)) % 10 + '0'); 34 | } 35 | else if (prod <= 99 && prod >= 10) 36 | { 37 | _putchar((prod / 10) + '0'); 38 | } 39 | _putchar((prod % 10) + '0'); 40 | } 41 | _putchar('\n'); 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/101-natural.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | *main - check for multiples of 3 and 5 4 | * 5 | *Return: 0 always 6 | */ 7 | int main(void) 8 | { 9 | int x = 1024, y, sum = 0; 10 | for (y = 0; y < x; y++) 11 | { 12 | if ((y % 3 == 0) || (y % 5 == 0)) 13 | { 14 | sum = sum + y; 15 | } 16 | } 17 | printf("%d\n", sum); 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/102-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | *main - print first 50 fibonacci 4 | * 5 | *Return: 0 always. 6 | */ 7 | int main(void) 8 | { 9 | long int i, x = 1, y = 2, sum = 0; 10 | for (i = 0; i < 49; i++) 11 | { 12 | printf("%ld, ", x); 13 | sum = x + y; 14 | x = y; 15 | y = sum; 16 | if (i == 48) 17 | printf("%ld\n", x); 18 | } 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/103-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | *main - print first 50 fibonacci 4 | * 5 | *Return: 0 always. 6 | */ 7 | int main(void) 8 | { 9 | long int i, x = 1, y = 2, sum = 0, tSum = 0; 10 | 11 | for (i = 0; i < 49; i++) 12 | { 13 | if ((y % 2 == 0) && (y <= 4000000)) 14 | { 15 | tSum = tSum + y; 16 | } 17 | sum = x + y; 18 | x = y; 19 | y = sum; 20 | 21 | } 22 | printf("%ld\n", tSum); 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/104-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - finds and prints the first 98 Fibonacci numbers, 4 | * starting with 1 and 2 5 | * followed by a new line 6 | * Return: ALways 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | unsigned long int i, j, k, j1, j2, k1, k2; 11 | 12 | j = 1; 13 | k = 2; 14 | 15 | printf("%lu", j); 16 | 17 | for (i = 1; i < 91; i++) 18 | { 19 | printf(", %lu", k); 20 | k = k + j; 21 | j = k - j; 22 | } 23 | 24 | j1 = j / 1000000000; 25 | j2 = j % 1000000000; 26 | k1 = k / 1000000000; 27 | k2 = k % 1000000000; 28 | 29 | for (i = 92; i < 99; ++i) 30 | { 31 | printf(", %lu", k1 + (k2 / 1000000000)); 32 | printf("%lu", k2 % 1000000000); 33 | k1 = k1 + j1; 34 | j1 = k1 - j1; 35 | k2 = k2 + j2; 36 | j2 = k2 - j2; 37 | } 38 | 39 | printf("\n"); 40 | 41 | return (0); 42 | } 43 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/11-print_to_98.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * print_to_98 - print all natural numbers from n to 98. 5 | * @n: the number to start counting from n to 98 6 | * Return: Always 0. 7 | */ 8 | void print_to_98(int n) 9 | { 10 | if (n < 98) 11 | { 12 | for (n = n; n < 98; n++) 13 | printf("%d, ", n); 14 | printf("%d\n", 98); 15 | } 16 | else 17 | { 18 | for (n = n; n > 98; n--) 19 | printf("%d, ", n); 20 | printf("%d\n", 98); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/2-print_alphabet_x10.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_alphabet_x10 - print alphabet 5 | * 6 | * Return: Always 0. 7 | */ 8 | void print_alphabet_x10(void) 9 | { 10 | int alphabet; 11 | int count; 12 | 13 | 14 | count = 0; 15 | while (count < 10) 16 | { 17 | for (alphabet = 'a' ; alphabet <= 'z'; alphabet++) 18 | { 19 | _putchar(alphabet); 20 | } 21 | 22 | count++; 23 | _putchar('\n'); 24 | } 25 | 26 | 27 | } 28 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/3-islower.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _islower - Short description, single line 5 | * @c: contains value to be compared 6 | * Return: Always 0. 7 | */ 8 | int _islower(int c) 9 | 10 | { 11 | 12 | if (c > 'a' && c < 'z') 13 | { 14 | return (1); 15 | } 16 | 17 | else 18 | { 19 | return (0); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/4-isalpha.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isalpha - Return 1 if c is a letter. lowercase or uppercase 5 | * 6 | *@c: The int to print 7 | * Return: Always 0. 8 | */ 9 | 10 | int _isalpha(int c) 11 | 12 | { 13 | 14 | if ((c > 'a' && c < 'z') || (c > 'A' && c < 'Z')) 15 | { 16 | return (1); 17 | } 18 | 19 | else 20 | { 21 | return (0); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/5-sign.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_sign - return 0 letter not lowercase, 1 letter lowercase 5 | * 6 | *@n: the int to print 7 | * Return: Always 0. 8 | */ 9 | int print_sign(int n) 10 | { 11 | if (n > 0) 12 | { 13 | _putchar ('+'); 14 | return (1); 15 | } 16 | 17 | else if (n == 0) 18 | { 19 | _putchar ('0'); 20 | return (0); 21 | } 22 | 23 | else 24 | { 25 | _putchar ('-'); 26 | return (-1); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/6-abs.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _abs - value absolute 6 | * 7 | *@n: The int to print 8 | * Return: Always 0. 9 | */ 10 | 11 | int _abs(int n) 12 | { 13 | 14 | if (n < 0) 15 | { 16 | return (n * (-1)); 17 | } 18 | 19 | else if (n == 0) 20 | { 21 | return (0); 22 | } 23 | 24 | else 25 | { 26 | return (n); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/7-print_last_digit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_last_digit - last digit 5 | * 6 | *@n: The int to print 7 | * Return: Always 0. 8 | */ 9 | 10 | int print_last_digit(int n) 11 | 12 | { 13 | int last_digit; 14 | 15 | if (n < 0) 16 | { 17 | last_digit = (-1 * (n % 10)); 18 | _putchar (last_digit + '0'); 19 | return (last_digit); 20 | } 21 | 22 | else 23 | { 24 | last_digit = (n % 10); 25 | _putchar (last_digit + '0'); 26 | return (last_digit); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/8-24_hours.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * jack_bauer -prints every minute of the day 5 | * 6 | * Return: Always 0. 7 | */ 8 | 9 | void jack_bauer(void) 10 | 11 | { 12 | int a; 13 | int b; 14 | 15 | for (a = 0; a <= 23; a++) 16 | { 17 | for (b = 0; b <= 59; b++) 18 | { 19 | _putchar (a / 10 + '0'); 20 | _putchar (a % 10 + '0'); 21 | _putchar (':'); 22 | _putchar (b / 10 + '0'); 23 | _putchar (b % 10 + '0'); 24 | _putchar ('\n'); 25 | 26 | } 27 | 28 | 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/9-times_table.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * times_table -prints tables 4 | * 5 | * Return: Always 0. 6 | */ 7 | void times_table(void) 8 | { 9 | int a; 10 | int b; 11 | int c; 12 | for (a = 0; a <= 9; a++) 13 | { 14 | for (b = 0; b <= 9; b++) 15 | { 16 | c = a * b; 17 | if ((c / 10) == 0) 18 | { 19 | if (b == 0) 20 | { 21 | _putchar ('0'); 22 | } 23 | if (b != 0) 24 | { 25 | _putchar (' '); 26 | _putchar ((c % 10) + '0'); 27 | } 28 | if (b < 9) 29 | { 30 | _putchar(','); 31 | _putchar (' '); 32 | } 33 | } 34 | else 35 | { 36 | _putchar ((c / 10) + '0'); 37 | _putchar ((c % 10) + '0'); 38 | if (b < 9) 39 | { 40 | _putchar(','); 41 | _putchar (' '); 42 | } 43 | } 44 | } 45 | _putchar ('\n'); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/README.md: -------------------------------------------------------------------------------- 1 | Functions in C 2 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x03-debugging/0-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - tests function prints if integer 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 2 | #include "main.h" 3 | 4 | /** 5 | * print_remaining_days - takes a date and prints how many days are 6 | * left in the year, taking leap years into account 7 | * @month: month in number format 8 | * @day: day of month 9 | * @year: year 10 | * Return: void 11 | */ 12 | 13 | void print_remaining_days(int month, int day, int year) 14 | { 15 | if (year % 4 == 0 || ((year % 400 == 0) && !(year % 100 == 0))) 16 | { 17 | if (month > 2 && day >= 60) 18 | { 19 | day++; 20 | } 21 | 22 | printf("Day of the year: %d\n", day); 23 | printf("Remaining days: %d\n", 366 - day); 24 | } 25 | else 26 | { 27 | if (month == 2 && day == 60) 28 | { 29 | printf("Invalid date: %02d/%02d/%04d\n", month, day - 31, year); 30 | } 31 | else 32 | { 33 | printf("Day of the year: %d\n", day); 34 | printf("Remaining days: %d\n", 365 - day); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /0x03-debugging/README.md: -------------------------------------------------------------------------------- 1 | # Debugging Process 2 | -------------------------------------------------------------------------------- /0x03-debugging/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | #include 5 | 6 | void positive_or_negative(int n); 7 | int largest_number(int a, int b, int c); 8 | int convert_day(int month, int day); 9 | void print_remaining_days(int month, int day, int year); 10 | 11 | #endif /* MAIN_H */ 12 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/0-isupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isupper - checks for uppercase character 5 | * @letter: the character to be tracked 6 | * Return: 1 if c is uppercase, 0 otherwise 7 | */ 8 | int _isupper(int letter) 9 | { 10 | return (letter >= 'A' && letter <= 'Z'); 11 | } 12 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/1-isdigit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isdigit - checks for a digit (0 through 9) 5 | * @num: int to be checked 6 | * Return: 1 if num is a digit, 0 otherwise 7 | */ 8 | int _isdigit(int num) 9 | { 10 | return (num >= '0' && num <= '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: tracked var 6 | */ 7 | 8 | void print_number(int n) 9 | { 10 | unsigned int x = n; 11 | 12 | if (n < 0) 13 | { 14 | _putchar(45); 15 | x = -x; 16 | } 17 | if ((x / 10) > 0) 18 | { 19 | print_number(x / 10); 20 | } 21 | _putchar((x % 10) + 48); 22 | } 23 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/2-mul.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * mul - multiplies two integers 5 | * @a: int to be multiplied to b 6 | * @b: int to be multiplied to a 7 | * Return: the result of the operation 8 | */ 9 | int mul(int a, int b) 10 | { 11 | int c; 12 | 13 | c = a * b; 14 | 15 | return (c); 16 | } 17 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/3-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_numbers - prints the numbers, from 0 to 9 5 | */ 6 | 7 | void print_numbers(void) 8 | { 9 | int x; 10 | 11 | for (x = 48; x < 58; x++) 12 | { 13 | _putchar(x); 14 | } 15 | _putchar(10); 16 | } 17 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/4-print_most_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_most_numbers - prints the numbers without 2 and 4 5 | */ 6 | 7 | void print_most_numbers(void) 8 | { 9 | int x; 10 | 11 | for (x = 48; x < 58; x++) 12 | { 13 | if (x != 50 && x != 52) 14 | { 15 | _putchar(x); 16 | } 17 | } 18 | _putchar(10); 19 | } 20 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/5-more_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * more_numbers - prints 10 times the numbers, from 0 to 14 5 | */ 6 | 7 | void more_numbers(void) 8 | { 9 | int c, x; 10 | 11 | for (c = 0; c < 10; c++) 12 | { 13 | for (x = 0; x <= 14; x++) 14 | { 15 | if (x > 9) 16 | { 17 | _putchar((x / 10) + 48); 18 | } 19 | _putchar((x % 10) + 48); 20 | } 21 | _putchar(10); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/6-print_line.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_line - draws a straight line in the terminal 5 | * @n: number of times the character _ should be printed 6 | */ 7 | void print_line(int n) 8 | { 9 | if (n <= 0) 10 | { 11 | _putchar('\n'); 12 | } else 13 | { 14 | int i; 15 | 16 | for (i = 1; i <= n; i++) 17 | { 18 | _putchar('_'); 19 | } 20 | _putchar('\n'); 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/7-print_diagonal.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_diagonal - draws a diagonal line on the terminal 5 | * @n: number of times the character \ should be printed 6 | */ 7 | void print_diagonal(int n) 8 | { 9 | if (n <= 0) 10 | { 11 | _putchar('\n'); 12 | } else 13 | { 14 | int i, j; 15 | 16 | for (i = 0; i < n; i++) 17 | { 18 | for (j = 0; j < n; j++) 19 | { 20 | if (j == i) 21 | _putchar('\\'); 22 | else if (j < i) 23 | _putchar(' '); 24 | } 25 | _putchar('\n'); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/8-print_square.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_square - prints a square 5 | * @size: the size of the square 6 | */ 7 | 8 | void print_square(int size) 9 | { 10 | int x, y; 11 | 12 | if (size > 0) 13 | { 14 | for (x = 0; x < size; x++) 15 | { 16 | for (y = 0; y < size; y++) 17 | { 18 | _putchar(35); 19 | } 20 | _putchar(10); 21 | } 22 | } 23 | else 24 | { 25 | _putchar(10); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/9-fizz_buzz.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - prints the numbers from 1 to 100, followed by a new line 6 | * but for multiples of three prints Fizz instead of the number 7 | * and for the multiples of five prints Buzz 8 | * Return: Always 0 (Success) 9 | */ 10 | int main(void) 11 | { 12 | int i; 13 | 14 | for (i = 1; i <= 100; i++) 15 | { 16 | if (i % 3 == 0 && i % 5 != 0) 17 | { 18 | printf(" Fizz"); 19 | } else if (i % 5 == 0 && i % 3 != 0) 20 | { 21 | printf(" Buzz"); 22 | } else if (i % 3 == 0 && i % 5 == 0) 23 | { 24 | printf(" FizzBuzz"); 25 | } else if (i == 1) 26 | { 27 | printf("%d", i); 28 | } else 29 | { 30 | printf(" %d", i); 31 | } 32 | } 33 | printf("\n"); 34 | 35 | return (0); 36 | } 37 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/README.md: -------------------------------------------------------------------------------- 1 | # More Functions and Nested Loops 2 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/main.h: -------------------------------------------------------------------------------- 1 | int _putchar(char c); 2 | int _isupper(int c); 3 | int _isdigit(int c); 4 | int mul(int a, int b); 5 | void print_numbers(void); 6 | void print_most_numbers(void); 7 | void more_numbers(void); 8 | void print_line(int n); 9 | void print_diagonal(int n); 10 | void print_square(int size); 11 | void print_triangle(int size); 12 | void print_number(int n); 13 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/0-reset_to_98.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * reset_to_98 - updates the value *n points to. 5 | * @n: the number to modify. 6 | */ 7 | 8 | void reset_to_98(int *n) 9 | { 10 | *n = 98; 11 | } 12 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/1-swap.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * swap_int - swap the value of two integers. 5 | * 6 | * @a: first integer. 7 | * @b: second integer. 8 | * 9 | */ 10 | 11 | void swap_int(int *a, int *b) 12 | { 13 | int c; 14 | 15 | c = 0; 16 | c = *a; 17 | *a = *b; 18 | *b = c; 19 | } 20 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/100-atoi.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _atoi - convert a string into an integer. 5 | * 6 | * @s: the string to use. 7 | * 8 | * Return: integer. 9 | */ 10 | 11 | int _atoi(char *s) 12 | { 13 | int sign = 1, i = 0; 14 | unsigned int res = 0; 15 | 16 | while (!(s[i] <= '9' && s[i] >= '0') && s[i] != '\0') 17 | { 18 | if (s[i] == '-') 19 | sign *= -1; 20 | i++; 21 | } 22 | while (s[i] <= '9' && (s[i] >= '0' && s[i] != '\0')) 23 | { 24 | res = (res * 10) + (s[i] - '0'); 25 | i++; 26 | } 27 | res *= sign; 28 | return (res); 29 | } 30 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/101-keygen.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | /** 7 | * main - print password. 8 | * 9 | * Return: 0. 10 | */ 11 | 12 | int main(void) 13 | { 14 | int ascii = 2772, i = 0, j, random; 15 | char password[100]; 16 | time_t t; 17 | 18 | srand((int) time(&t)); 19 | while (ascii > 126) 20 | { 21 | random = rand() % 126; 22 | password[i] = random; 23 | ascii -= random; 24 | i++; 25 | } 26 | if (ascii > 0) 27 | password[i] = ascii; 28 | else 29 | { 30 | i--; 31 | } 32 | 33 | 34 | for (j = 0; j <= i; j++) 35 | { 36 | printf("%c", password[j]); 37 | } 38 | return (0); 39 | } 40 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/2-strlen.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strlen - calculate the length of a string. 5 | * 6 | * @s: the string to calculate it's length. 7 | * Return: length of a string. 8 | */ 9 | 10 | int _strlen(char *s) 11 | { 12 | int i; 13 | int length = 0; 14 | 15 | for (i = 0; s[i] != '\0'; i++) 16 | { 17 | length++; 18 | } 19 | return (length); 20 | } 21 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/3-puts.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _puts - prints a string followed by a new line.. 5 | * 6 | *@str: the string to print. 7 | * 8 | */ 9 | 10 | void _puts(char *str) 11 | { 12 | int i; 13 | 14 | for (i = 0; str[i] != '\0'; i++) 15 | { 16 | _putchar(str[i]); 17 | } 18 | _putchar('\n'); 19 | } 20 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/4-print_rev.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_rev - print a string in reverse. 5 | * 6 | *@s: the string to print in reverse. 7 | * 8 | */ 9 | 10 | void print_rev(char *s) 11 | { 12 | int i = 0; 13 | int length; 14 | 15 | for (length = 0; s[length] != '\0'; length++) 16 | { 17 | } 18 | 19 | for (i = length - 1; i >= 0; i--) 20 | { 21 | _putchar(s[i]); 22 | } 23 | _putchar('\n'); 24 | } 25 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/5-rev_string.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * rev_string - reverse a string. 5 | * 6 | *@s: the string to be reversed. 7 | */ 8 | 9 | void rev_string(char *s) 10 | { 11 | int length, j, i; 12 | char v1, v2; 13 | 14 | for (length = 0; s[length] != '\0'; length++) 15 | { 16 | } 17 | 18 | j = length - 1; 19 | i = 0; 20 | 21 | while (j > i) 22 | { 23 | v1 = s[i]; 24 | v2 = s[j]; 25 | s[i] = v2; 26 | s[j] = v1; 27 | j--; 28 | i++; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/6-puts2.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * puts2 - prints every other character of a string. 5 | *@str: the string to use. 6 | */ 7 | 8 | void puts2(char *str) 9 | { 10 | int i = 0; 11 | 12 | while (str[i] != '\0') 13 | { 14 | if (i % 2 == 0) 15 | { 16 | _putchar(str[i]); 17 | } 18 | i++; 19 | } 20 | _putchar('\n'); 21 | } 22 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/7-puts_half.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * puts_half - prints the second half of a string. 5 | *@str: string to use. 6 | */ 7 | 8 | void puts_half(char *str) 9 | { 10 | int length, n, i; 11 | 12 | for (length = 0; str[length] != '\0'; length++) 13 | { 14 | } 15 | n = (length - 1) / 2; 16 | for (i = n + 1; str[i] != '\0'; i++) 17 | { 18 | _putchar(str[i]); 19 | } 20 | _putchar('\n'); 21 | } 22 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/8-print_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * print_array - print an array. 6 | * @a: the array to print. 7 | * @n: array's length 8 | */ 9 | 10 | void print_array(int *a, int n) 11 | { 12 | int i; 13 | 14 | for (i = 0; i < n; i++) 15 | { 16 | printf("%d", a[i]); 17 | if (i < n - 1) 18 | { 19 | printf(", "); 20 | } 21 | } 22 | printf("\n"); 23 | } 24 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/9-strcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * *_strcpy - copies the string pointed to by src, including \0. 5 | *@src: pointer. 6 | *@dest: pointer. 7 | *Return: the pointer to dest. 8 | */ 9 | 10 | char *_strcpy(char *dest, char *src) 11 | { 12 | int i, length; 13 | 14 | for (length = 0; src[length] != '\0'; length++) 15 | { 16 | } 17 | 18 | for (i = 0; i <= length ; i++) 19 | { 20 | dest[i] = src[i]; 21 | } 22 | return (dest); 23 | } 24 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | # Pointers, Arrays and Strings in C 2 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | #ifndef main 2 | 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 print_rev(char *s); 9 | void rev_string(char *s); 10 | void puts2(char *str); 11 | void puts_half(char *str); 12 | void print_array(int *a, int n); 13 | char *_strcpy(char *dest, char *src); 14 | int _atoi(char *s); 15 | 16 | #define main 17 | #endif 18 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/0-strcat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | *_strcat - concatinate 2 strings. 5 | *@dest: first string. 6 | *@src: second string. 7 | *Return: string. 8 | */ 9 | 10 | char *_strcat(char *dest, char *src) 11 | { 12 | int i = 0, j = 0; 13 | 14 | while (dest[i] != '\0') 15 | i++; 16 | 17 | while (src[j] != '\0') 18 | { 19 | dest[i] = src[j]; 20 | i++; 21 | j++; 22 | } 23 | 24 | dest[i] = '\0'; 25 | 26 | return (dest); 27 | } 28 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/1-strncat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | *_strncat - concatinate 2 strings. 5 | *@dest: first string. 6 | *@src: second string. 7 | *@n: the number of bytes to use from src. 8 | *Return: string. 9 | */ 10 | 11 | char *_strncat(char *dest, char *src, int n) 12 | { 13 | int i = 0, k = 0; 14 | 15 | while (dest[i] != '\0') 16 | i++; 17 | 18 | while (src[k] != '\0' && n > k) 19 | { 20 | dest[i] = src[k]; 21 | k++; 22 | i++; 23 | } 24 | if (n > 0) 25 | { 26 | dest[i] = '\0'; 27 | } 28 | 29 | return (dest); 30 | } 31 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/100-rot13.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | *rot13 - encoding a string using rot13. 5 | *@s: the string to be encoded to rot13. 6 | *Return: the string s encoded to rot13. 7 | */ 8 | 9 | char *rot13(char *s) 10 | { 11 | int i = 0, j = 0; 12 | char string_rot13[] = "NnOoPpQqRrSsTtUuVvWwXxYyZzAaBbCcDdEeFfGgHhIiJjKkLlMm"; 13 | char string_alpha[] = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz"; 14 | 15 | while (s[i] != '\0') 16 | { 17 | do { 18 | if (s[i] == string_alpha[j]) 19 | { 20 | s[i] = string_rot13[j]; 21 | break; 22 | } 23 | j++; 24 | } while (string_alpha[j] != '\0'); 25 | j = 0; 26 | i++; 27 | } 28 | 29 | return (s); 30 | } 31 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/101-print_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | *print_number - print a number using _putchar. 5 | *@n: the number to be printed. 6 | */ 7 | 8 | void print_number(int n) 9 | { 10 | unsigned int i = 1; 11 | 12 | if (n < 0) 13 | { 14 | _putchar('-'); 15 | n *= -1; 16 | } 17 | if (n == 0) 18 | _putchar('0'); 19 | else 20 | { 21 | while ((n / i) >= 10) 22 | i *= 10; 23 | 24 | while (i > 0) 25 | { 26 | _putchar((n / i) + '0'); 27 | n %= i; 28 | i /= 10; 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/102-magic.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | int n; 6 | int a[5]; 7 | int *p; 8 | 9 | a[2] = 1024; 10 | p = &n; 11 | /* 12 | * write your line of code here... 13 | * Remember: 14 | * - you are not allowed to use a 15 | * - you are not allowed to modify p 16 | * - only one statement 17 | * - you are not allowed to code anything else than this line of code 18 | */ 19 | *(p + 5) = 98; 20 | /* ...so that this prints 98\n */ 21 | printf("a[2] = %d\n", a[2]); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/103-infinite_add.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | *infinite_add - add two numbers. 5 | * 6 | *@n1: first number. 7 | *@n2: second number. 8 | *@r: result. 9 | *@size_r: result size. 10 | *Return: the addition of n1 and n2. 11 | */ 12 | 13 | char *infinite_add(char *n1, char *n2, char *r, int size_r) 14 | { 15 | int add = 0, len1, len2, i, j; 16 | 17 | for (len1 = 0; n1[len1]; len1++) 18 | ; 19 | for (len2 = 0; n2[len2]; len2++) 20 | ; 21 | if (len1 > size_r || len2 > size_r) 22 | return (0); 23 | len1--; 24 | len2--; 25 | size_r--; 26 | for (i = 0; i < size_r; i++, len1--, len2--) 27 | { 28 | if (len1 >= 0) 29 | add += n1[len1] - '0'; 30 | if (len2 >= 0) 31 | add += n2[len2] - '0'; 32 | if (len1 < 0 && len2 < 0 && add == 0) 33 | break; 34 | r[i] = add % 10 + '0'; 35 | add /= 10; 36 | } 37 | r[i] = '\0'; 38 | if (len1 >= 0 || len2 >= 0 || add) 39 | return (0); 40 | for (i--, j = 0; i > j; i--, j++) 41 | { 42 | add = r[i]; 43 | r[i] = r[j]; 44 | r[j] = add; 45 | } 46 | return (r); 47 | } 48 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/104-print_buffer.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | *print_buffer - Print a buffer 10 bytes per line. 6 | *@b: Buffer address. 7 | *@size: Number of characters to be printed. 8 | */ 9 | void print_buffer(char *b, int size) 10 | { 11 | int j, k, l; 12 | 13 | if (size <= 0) 14 | printf("\n"); 15 | else 16 | { 17 | for (j = 0; j < size; j += 10) 18 | { 19 | printf("%.8x:", j); 20 | for (k = j; k < j + 10; k++) 21 | { 22 | if (k % 2 == 0) 23 | printf(" "); 24 | if (k < size) 25 | printf("%.2x", *(b + k)); 26 | else 27 | printf(" "); 28 | } 29 | printf(" "); 30 | for (l = j; l < j + 10; l++) 31 | { 32 | if (l >= size) 33 | break; 34 | if (*(b + l) < 32 || *(b + l) > 126) 35 | printf("%c", '.'); 36 | else 37 | printf("%c", *(b + l)); 38 | } 39 | printf("\n"); 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/2-strncpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | *_strncpy - copy src into dest. 5 | *@dest: first string. 6 | *@src: second string. 7 | *@n: the number of bytes to use from src. 8 | *Return: string. 9 | */ 10 | 11 | char *_strncpy(char *dest, char *src, int n) 12 | { 13 | int i = 0, k = 0; 14 | 15 | while (n > k) 16 | { 17 | if (src[k] == '\0') 18 | { 19 | for (; k < n; k++) 20 | { 21 | dest[i] = '\0'; 22 | i++; 23 | } 24 | } 25 | else 26 | { 27 | dest[i] = src[k]; 28 | k++; 29 | i++; 30 | } 31 | } 32 | 33 | return (dest); 34 | } 35 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/3-strcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strcmp - compare two strings. 5 | *@s1: string 1. 6 | *@s2: string 2. 7 | *Return: int. 8 | */ 9 | 10 | int _strcmp(char *s1, char *s2) 11 | { 12 | int i = 0, cmp = 0; 13 | 14 | while (s1[i] != '\0' && s2[i] != '\0' && cmp == 0) 15 | { 16 | cmp = s1[i] - s2[i]; 17 | i++; 18 | } 19 | 20 | return (cmp); 21 | } 22 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/4-rev_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | *reverse_array - reverse an array. 5 | *@a: array. 6 | *@n: array's length. 7 | */ 8 | 9 | void reverse_array(int *a, int n) 10 | { 11 | int i, j, tmp; 12 | 13 | j = n - 1; 14 | for (i = 0; i < n / 2; i++) 15 | { 16 | tmp = a[i]; 17 | a[i] = a[j]; 18 | a[j] = tmp; 19 | j--; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/5-string_toupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | *string_toupper - changes all lowercase letters of a string to uppercase. 5 | *@s: string to modify. 6 | * 7 | *Return: s modified. 8 | */ 9 | 10 | char *string_toupper(char *s) 11 | { 12 | int i = 0; 13 | 14 | while (s[i] != '\0') 15 | { 16 | if (s[i] > 96 && s[i] < 123) 17 | { 18 | s[i] -= 32; 19 | } 20 | i++; 21 | } 22 | 23 | return (s); 24 | } 25 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/6-cap_string.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | *cap_string - capitalizes all words of a string. 5 | *@s: string to use. 6 | * 7 | *Return: string. 8 | */ 9 | 10 | char *cap_string(char *s) 11 | { 12 | int i = 1, j, check; 13 | char a[] = {',', ';', '.', '!', '?', '"', '(', ')', '{', '}', '\n', '\t', ' '}; 14 | 15 | if (s[0] > 96 && s[0] < 123) 16 | s[0] -= 32; 17 | 18 | while (s[i] != '\0') 19 | { 20 | if (s[i] > 96 && s[i] < 123) 21 | { 22 | j = 0; 23 | check = 0; 24 | while (check == 0 && j < 13) 25 | { 26 | if (s[i - 1] == a[j]) 27 | { 28 | check = 1; 29 | } 30 | j++; 31 | } 32 | if (check == 1) 33 | { 34 | s[i] -= 32; 35 | } 36 | } 37 | i++; 38 | } 39 | return (s); 40 | } 41 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/7-leet.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | *leet - encodes a string into 1337. 5 | *@s: string to encode. 6 | *Return: the encoded string. 7 | */ 8 | 9 | char *leet(char *s) 10 | { 11 | int i = 0, j = 0; 12 | char array_leet[] = {'4', '3', '1', '0', '7'}; 13 | char array_up[] = {'A', 'E', 'L', 'O', 'T'}; 14 | char array_low[] = {'a', 'e', 'l', 'o', 't'}; 15 | 16 | while (s[i] != '\0') 17 | { 18 | for (j = 0; j < 5; j++) 19 | { 20 | if (s[i] == array_low[j] || s[i] == array_up[j]) 21 | s[i] = array_leet[j]; 22 | } 23 | i++; 24 | } 25 | 26 | return (s); 27 | } 28 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | # More pointers, arrays and strings 2 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | char *_strcat(char *dest, char *src); 2 | int _putchar(char c); 3 | char *_strncat(char *dest, char *src, int n); 4 | char *_strncpy(char *dest, char *src, int n); 5 | int _strcmp(char *s1, char *s2); 6 | void reverse_array(int *a, int n); 7 | char *string_toupper(char *); 8 | char *cap_string(char *s); 9 | char *leet(char *); 10 | char *rot13(char *); 11 | void print_number(int n); 12 | char *infinite_add(char *n1, char *n2, char *r, int size_r); 13 | void print_buffer(char *b, int size); 14 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/0-memset.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _memset - fills string with constant char b upto n bytes 5 | * @s: input pointer to string 6 | * @b: constant char 7 | * @n: number of bytes 8 | * Return: pointer to s string 9 | */ 10 | 11 | char *_memset(char *s, char b, unsigned int n) 12 | { 13 | unsigned int i = 0; 14 | 15 | while (i < n) 16 | { 17 | s[i] = b; 18 | i++; 19 | } 20 | return (s); 21 | } 22 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/1-memcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _memcpy - input 5 | * @dest: input pointer to string destination 6 | * @src: input pointer to source string 7 | * @n: number of bytes 8 | * Return: pointer to destination string 9 | */ 10 | 11 | char *_memcpy(char *dest, char *src, unsigned int n) 12 | { 13 | unsigned int i = 0; 14 | 15 | while (i < n) 16 | { 17 | *(dest + i) = *(src + i); 18 | i++; 19 | } 20 | return (dest); 21 | } 22 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/100-set_string.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * set_string - a function that sets the value of a pointer to a char. 5 | * @s: double pointer to a string 6 | * @to: pointer to the string to set s to 7 | * Return: Description of the returned value 8 | */ 9 | void set_string(char **s, char *to) 10 | { 11 | *s = to; 12 | } 13 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/101-crackme_password: -------------------------------------------------------------------------------- 1 | abc123 2 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/2-strchr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strchr - locates a character in a string 5 | * @s: string input 6 | * @c: character to find 7 | * Return: pointer to first occurence of c character 8 | */ 9 | 10 | char *_strchr(char *s, char c) 11 | { 12 | unsigned int i; 13 | 14 | for (i = 0; s[i] != '\0'; i++) 15 | if (s[i] == c) 16 | break; 17 | return (s[i] == c ? (s + i) : '\0'); 18 | } 19 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/3-strspn.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strspn - gets the length of a prefix substring 5 | * @s: input string to search for substring 6 | * @accept: characters that prefix substring must include 7 | * Return: length of prefix substring 8 | */ 9 | 10 | unsigned int _strspn(char *s, char *accept) 11 | { 12 | unsigned int i, j, a_len = 0, len = 0; 13 | 14 | while (accept[a_len] != '\0') 15 | a_len++; 16 | for (i = 0; s[i] != '\0'; i++) 17 | for (j = 0; j < a_len; j++) 18 | if (s[i] == accept[j]) 19 | len++, j = a_len; 20 | else 21 | if (j == a_len - 1) 22 | goto exit; 23 | exit: return (len); 24 | } 25 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/4-strpbrk.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strpbrk - finds first matching char in string 5 | * @s: input string to search for matching char 6 | * @accept: characters that could be matched 7 | * Return: pointer to matching char 8 | */ 9 | 10 | char *_strpbrk(char *s, char *accept) 11 | { 12 | unsigned int i, j; 13 | 14 | for (i = 0; s[i] != '\0'; i++) 15 | for (j = 0; accept[j] != '\0'; j++) 16 | if (s[i] == accept[j]) 17 | goto exit; 18 | exit: return (s[i] != '\0' ? s + i : '\0'); 19 | } 20 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/5-strstr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strstr - returns pointer to first char of matching substring 5 | * @haystack: string to find substring in 6 | * @needle: substring to find match of 7 | * Return: pointer to first char of matching substring 8 | */ 9 | 10 | char *_strstr(char *haystack, char *needle) 11 | { 12 | int k; 13 | 14 | while (*haystack != '\0') 15 | { 16 | k = 0; 17 | while (*haystack == *needle && *haystack != '\0' && *needle != '\0') 18 | haystack++, needle++, k++; 19 | if (*needle == '\0') 20 | return (haystack - k); 21 | haystack -= (k - 1), needle -= k; 22 | } 23 | return ('\0'); 24 | } 25 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/7-print_chessboard.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_chessboard - a function that prints a chessboard 5 | * @a: an array input to print 6 | * Return: Nothing 7 | */ 8 | void print_chessboard(char (*a)[8]) 9 | { 10 | int i = 0, j; 11 | 12 | for (; i < 8; i++) 13 | { 14 | for (j = 0; j < 8; j++) 15 | _putchar(a[i][j]); 16 | _putchar('\n'); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/8-print_diagsums.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * print_diagsums - prints sum of #'s in diagnols of square 6 | * @a: input square array 7 | * @size: size of one dimension in array 8 | * Return: void 9 | */ 10 | 11 | void print_diagsums(int *a, int size) 12 | { 13 | int i, j, sum1 = 0, sum2 = 0; 14 | 15 | for (j = 0, i = size - 1; j < (size * size); j += size + 1, i += size - 1) 16 | sum1 += a[j], sum2 += a[i]; 17 | printf("%d, %d\n", sum1, sum2); 18 | } 19 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | # More Functions on Pointers, Arrays and Strings. 2 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | int _putchar(char c); 2 | char *_memset(char *s, char b, unsigned int n); 3 | char *_memcpy(char *dest, char *src, unsigned int n); 4 | char *_strchr(char *s, char c); 5 | unsigned int _strspn(char *s, char *accept); 6 | char *_strpbrk(char *s, char *accept); 7 | char *_strstr(char *haystack, char *needle); 8 | void print_chessboard(char (*a)[8]); 9 | void print_diagsums(int *a, int size); 10 | void set_string(char **s, char *to); 11 | -------------------------------------------------------------------------------- /0x08-recursion/0-puts_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _puts_recursion - Prints a string followed by a new line 5 | * @s: string 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | 11 | void _puts_recursion(char *s) 12 | { 13 | if (*s == 0) 14 | { 15 | _putchar('\n'); 16 | return; 17 | } 18 | _putchar(*s); 19 | _puts_recursion(s + 1); 20 | } 21 | -------------------------------------------------------------------------------- /0x08-recursion/1-print_rev_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _print_rev_recursion - Prints a string in reverse 5 | * @s: string 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | void _print_rev_recursion(char *s) 11 | { 12 | if (*s != '\0') 13 | { 14 | _print_rev_recursion(s + 1); 15 | _putchar(*s); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /0x08-recursion/100-is_palindrome.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * palind2 - obtains length of a 5 | * @a: string 6 | * @l: integer to count length 7 | * 8 | * Return: On success 1. 9 | * On error, -1 is returned, and errno is set appropriately. 10 | */ 11 | int palind2(char *a, int l) 12 | { 13 | if (*a == 0) 14 | return (l - 1); 15 | return (palind2(a + 1, l + 1)); 16 | } 17 | /** 18 | * palind3 - compares string vs string reverse 19 | * @a: string 20 | * @l: length 21 | * 22 | * Return: On success 1. 23 | * On error, -1 is returned, and errno is set appropriately. 24 | */ 25 | 26 | int palind3(char *a, int l) 27 | { 28 | if (*a != *(a + l)) 29 | return (0); 30 | else if (*a == 0) 31 | return (1); 32 | return (palind3(a + 1, l - 2)); 33 | } 34 | /** 35 | * is_palindrome - checks if a string is a palindrome 36 | * @s: string to evaluate 37 | * 38 | * Return: On success 1. 39 | * On error, -1 is returned, and errno is set appropriately. 40 | */ 41 | int is_palindrome(char *s) 42 | { 43 | int l; 44 | 45 | l = palind2(s, 0); 46 | return (palind3(s, l)); 47 | } 48 | -------------------------------------------------------------------------------- /0x08-recursion/101-wildcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * wildcmp - compares two strings and returns 1 if the strings 5 | * can be considered identical, otherwise return 0. 6 | * @s1: string to compare to 7 | * @s2: string with wild character 8 | * 9 | * Return: On success 1. 10 | * On error, -1 is returned, and errno is set appropriately. 11 | */ 12 | int wildcmp(char *s1, char *s2) 13 | { 14 | if (*s1 == '\0' && *s2 == '\0') 15 | return (1); 16 | 17 | if (*s1 == *s2) 18 | return (wildcmp(s1 + 1, s2 + 1)); 19 | 20 | if (*s2 == '*') 21 | { 22 | if (*s2 == '*' && *(s2 + 1) != '\0' && *s1 == '\0') 23 | return (0); 24 | if (wildcmp(s1, s2 + 1) || wildcmp(s1 + 1, s2)) 25 | return (1); 26 | } 27 | 28 | return (0); 29 | } 30 | -------------------------------------------------------------------------------- /0x08-recursion/2-strlen_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strlen_recursion - Returns Length of String 5 | * @s: string 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | 11 | int _strlen_recursion(char *s) 12 | { 13 | if (*s != '\0') 14 | { 15 | return (1 + _strlen_recursion(s + 1)); 16 | } 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x08-recursion/3-factorial.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * factorial - gets factorial of n 5 | * @n: integer 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | int factorial(int n) 11 | { 12 | if (n < 0) 13 | return (-1); 14 | if (n == 0) 15 | return (1); 16 | return (n * factorial(n - 1)); 17 | } 18 | -------------------------------------------------------------------------------- /0x08-recursion/4-pow_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _pow_recursion - raises x to the power of y 5 | * @x: Number Integer 6 | * @y: Power 7 | * 8 | * Return: On success 1. 9 | * On error, -1 is returned, and errno is set appropriately. 10 | */ 11 | int _pow_recursion(int x, int y) 12 | { 13 | if (y < 0) 14 | return (-1); 15 | if (y == 0) 16 | return (1); 17 | return (x * _pow_recursion(x, y - 1)); 18 | } 19 | -------------------------------------------------------------------------------- /0x08-recursion/5-sqrt_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * sqrt2 - Makes possible to evaluate from 1 to n 5 | * @a: same number as n 6 | * @b: number that iterates from 1 to n 7 | * 8 | * Return: On success 1. 9 | * On error, -1 is returned, and errno is set appropriately. 10 | */ 11 | int sqrt2(int a, int b) 12 | { 13 | if (b * b == a) 14 | return (b); 15 | else if (b * b > a) 16 | return (-1); 17 | return (sqrt2(a, b + 1)); 18 | } 19 | /** 20 | * _sqrt_recursion - returns the natural square root of n 21 | * @n: Number Integer 22 | * 23 | * Return: On success 1. 24 | * On error, -1 is returned, and errno is set appropriately. 25 | */ 26 | int _sqrt_recursion(int n) 27 | { 28 | return (sqrt2(n, 1)); 29 | } 30 | -------------------------------------------------------------------------------- /0x08-recursion/6-is_prime_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * prime2 - Makes possible to evaluate from 1 to n 5 | * @a: same number as n 6 | * @b: number that iterates from 1 to n 7 | * 8 | * Return: On success 1. 9 | * On error, -1 is returned, and errno is set appropriately. 10 | */ 11 | int prime2(int a, int b) 12 | { 13 | if (a == b) 14 | return (1); 15 | else if (a % b == 0) 16 | return (0); 17 | return (prime2(a, b + 1)); 18 | } 19 | /** 20 | * is_prime_number - checks if a number is prime 21 | * @n: Number Integer 22 | * 23 | * Return: On success 1. 24 | * On error, -1 is returned, and errno is set appropriately. 25 | */ 26 | int is_prime_number(int n) 27 | { 28 | if (n <= 1) 29 | return (0); 30 | return (prime2(n, 2)); 31 | } 32 | -------------------------------------------------------------------------------- /0x08-recursion/README.md: -------------------------------------------------------------------------------- 1 | # Recursion in C Programming 2 | -------------------------------------------------------------------------------- /0x08-recursion/main.h: -------------------------------------------------------------------------------- 1 | int _putchar(char c); 2 | void _puts_recursion(char *s); 3 | void _print_rev_recursion(char *s); 4 | int _strlen_recursion(char *s); 5 | int factorial(int n); 6 | int _pow_recursion(int x, int y); 7 | int _sqrt_recursion(int n); 8 | int is_prime_number(int n); 9 | int is_palindrome(char *s); 10 | int wildcmp(char *s1, char *s2); 11 | -------------------------------------------------------------------------------- /0x09-static_libraries/README.md: -------------------------------------------------------------------------------- 1 | # Introduction to StaticLibraries in C 2 | -------------------------------------------------------------------------------- /0x09-static_libraries/create_static_lib.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -Wall -pedantic -Werror -Wextra -c *.c 3 | ar -rc liball.a *.o 4 | ranlib liball.a 5 | -------------------------------------------------------------------------------- /0x09-static_libraries/libmy.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/larymak/alx-low_level_programming/2cffc534ddda1190149fcb9eb2e5df07e7414447/0x09-static_libraries/libmy.a -------------------------------------------------------------------------------- /0x09-static_libraries/main.h: -------------------------------------------------------------------------------- 1 | #ifndef _FUNCTIONS_H 2 | #define _FUNCTIONS_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 | #include 3 | 4 | /** 5 | * main - Entry point 6 | * 7 | * @argc: Counts the number of parameters that go into main 8 | * @argv: Pointer of array of pointers containing strings entering main 9 | * Return: Always 0 (Success) 10 | */ 11 | 12 | int main(int argc, char **argv) 13 | { 14 | if (argc > 0) 15 | printf("%s\n", argv[0]); 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x0A-argc_argv/1-args.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * main - Entry point 6 | * 7 | * @argc: Counts the number of parameters that go into main 8 | * @argv: Pointer of array of pointers containing strings entering main 9 | * Return: Always 0 (Success) 10 | */ 11 | int main(int argc, char **argv) 12 | { 13 | (void) argv; 14 | printf("%i\n", argc - 1); 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x0A-argc_argv/100-change.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | 6 | /** 7 | * _isnumber - checks if string is a number 8 | * @s: string 9 | * 10 | * Return: On success 1. 11 | * If not a number, 0 is returned. 12 | */ 13 | int _isnumber(char *s) 14 | { 15 | int i, check, d; 16 | 17 | i = 0, d = 0, check = 1; 18 | if (*s == '-') 19 | i++; 20 | for (; *(s + i) != 0; i++) 21 | { 22 | d = isdigit(*(s + i)); 23 | if (d == 0) 24 | { 25 | check = 0; 26 | break; 27 | } 28 | } 29 | return (check); 30 | } 31 | /** 32 | * main - Entry point 33 | * 34 | * @argc: Counts the number of parameters that go into main 35 | * @argv: Pointer of array of pointers containing strings entering main 36 | * Return: Always 0 (Success) 37 | */ 38 | int main(int argc, char **argv) 39 | { 40 | int j, ex, coins, cents, d; 41 | int c[5] = {25, 10, 5, 2, 1}; 42 | 43 | ex = 1, j = 0, coins = 0; 44 | if (argc == 2) 45 | { 46 | if (_isnumber(argv[1])) 47 | { 48 | ex = 0, cents = atoi(argv[1]); 49 | if (cents >= 0) 50 | { 51 | while (cents != 0) 52 | { 53 | d = cents / c[j]; 54 | if (d == 0) 55 | { 56 | j++; 57 | } 58 | else 59 | { 60 | coins += d; 61 | cents -= (d * c[j]); 62 | } 63 | } 64 | } 65 | } 66 | } 67 | if (ex == 0) 68 | printf("%i\n", coins); 69 | else 70 | printf("%s\n", "Error"); 71 | return (ex); 72 | } 73 | -------------------------------------------------------------------------------- /0x0A-argc_argv/2-args.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * main - Entry point 6 | * 7 | * @argc: Counts the number of parameters that go into main 8 | * @argv: Pointer of array of pointers containing strings entering main 9 | * Return: Always 0 (Success) 10 | */ 11 | int main(int argc, char **argv) 12 | { 13 | int i; 14 | 15 | if (argc > 0) 16 | { 17 | for (i = 0; i < argc; i++) 18 | { 19 | printf("%s\n", argv[i]); 20 | } 21 | } 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x0A-argc_argv/3-mul.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * main - Entry point 6 | * 7 | * @argc: Counts the number of parameters that go into main 8 | * @argv: Pointer of array of pointers containing strings entering main 9 | * Return: Always 0 (Success) 10 | */ 11 | int main(int argc, char **argv) 12 | { 13 | int n, ex; 14 | 15 | ex = 0; 16 | if (argc != 3) 17 | { 18 | printf("%s\n", "Error"); 19 | ex = 1; 20 | } 21 | else 22 | { 23 | n = atoi(argv[1]) * atoi(argv[2]); 24 | printf("%i\n", n); 25 | } 26 | return (ex); 27 | } 28 | -------------------------------------------------------------------------------- /0x0A-argc_argv/4-add.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /** 6 | * _isnumber - checks if string is a number 7 | * @s: string 8 | * 9 | * Return: On success 1. 10 | * If not a number, 0 is returned. 11 | */ 12 | 13 | int _isnumber(char *s) 14 | { 15 | int i, check, d; 16 | 17 | i = 0, d = 0, check = 1; 18 | if (*s == '-') 19 | i++; 20 | for (; *(s + i) != 0; i++) 21 | { 22 | d = isdigit(*(s + i)); 23 | if (d == 0) 24 | { 25 | check = 0; 26 | break; 27 | } 28 | } 29 | return (check); 30 | } 31 | /** 32 | * main - Entry point 33 | * 34 | * @argc: Counts the number of parameters that go into main 35 | * @argv: Pointer of array of pointers containing strings entering main 36 | * Return: Always 0 (Success) 37 | */ 38 | int main(int argc, char **argv) 39 | { 40 | int i, n, ex; 41 | 42 | ex = 0, n = 0; 43 | if (argc > 1) 44 | { 45 | for (i = 1; i < argc; i++) 46 | { 47 | if (_isnumber(argv[i])) 48 | n += atoi(argv[i]); 49 | else 50 | ex = 1; 51 | } 52 | } 53 | if (ex == 0) 54 | printf("%i\n", n); 55 | else 56 | printf("%s\n", "Error"); 57 | return (ex); 58 | } 59 | -------------------------------------------------------------------------------- /0x0A-argc_argv/README.md: -------------------------------------------------------------------------------- 1 | # 0x0A. C - argc, argv 2 | -------------------------------------------------------------------------------- /0x0B-malloc_free/0-create_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * create_array - creates an array of chars, and initializes it with 7 | * a specific char 8 | * @size: size of array 9 | * @c: character to initialize with 10 | * 11 | * Return: Pointer 12 | */ 13 | char *create_array(unsigned int size, char c) 14 | { 15 | unsigned int i; 16 | char *s; 17 | 18 | if (size <= 0) 19 | return (0); 20 | 21 | s = malloc(sizeof(char) * size); 22 | 23 | if (s == 0) 24 | return (0); 25 | 26 | for (i = 0; i < size; i++) 27 | *(s + i) = c; 28 | 29 | *(s + i) = '\0'; 30 | 31 | return (s); 32 | } 33 | -------------------------------------------------------------------------------- /0x0B-malloc_free/1-strdup.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | *_strdup - allocate a string. 6 | *@str: string. 7 | *Return: pointer to string if success. 8 | */ 9 | 10 | char *_strdup(char *str) 11 | { 12 | int i; 13 | char *str1; 14 | 15 | if (str == NULL) 16 | return (NULL); 17 | 18 | for (i = 0; str[i]; i++) 19 | ; 20 | i++; 21 | str1 = malloc(sizeof(char) * i); 22 | 23 | if (str1 == NULL) 24 | return (NULL); 25 | 26 | for (i = 0; str[i] != '\0'; i++) 27 | str1[i] = str[i]; 28 | str1[i] = '\0'; 29 | return (str1); 30 | } 31 | -------------------------------------------------------------------------------- /0x0B-malloc_free/100-argstostr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * argstostr - concatenates all the arguments of your program 6 | * @ac: argument count in main 7 | * @av: arguments passed to main 8 | * 9 | * Return: Pointer 10 | */ 11 | 12 | char *argstostr(int ac, char **av) 13 | { 14 | char *s; 15 | int l, lt, i, j, k; 16 | 17 | if (ac == 0 || av == NULL) 18 | return (0); 19 | l = 0, k = 0; 20 | for (i = 0; i < ac; i++) 21 | { 22 | lt = 0; 23 | while (av[i][lt]) 24 | lt++; 25 | l += lt + 1; 26 | } 27 | s = malloc((l + 1) * sizeof(char)); 28 | 29 | if (s == 0) 30 | return (0); 31 | 32 | for (j = 0; j < ac; j++) 33 | { 34 | lt = 0; 35 | while (av[j][lt]) 36 | { 37 | *(s + k) = av[j][lt]; 38 | k++; 39 | lt++; 40 | } 41 | *(s + k) = '\n'; 42 | k++; 43 | } 44 | *(s + k) = '\0'; 45 | 46 | return (s); 47 | } 48 | -------------------------------------------------------------------------------- /0x0B-malloc_free/101-strtow.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | /** 5 | * _wcount - counts number of words 6 | * @sw: string 7 | * 8 | * Return: int 9 | */ 10 | int _wcount(char *sw) 11 | { 12 | int l, wc; 13 | 14 | l = 0, wc = 0; 15 | if (*(sw + l) == ' ') 16 | l++; 17 | while (*(sw + l)) 18 | { 19 | if (*(sw + l) == ' ' && *(sw + l - 1) != ' ') 20 | wc++; 21 | if (*(sw + l) != ' ' && *(sw + l + 1) == 0) 22 | wc++; 23 | l++; 24 | } 25 | return (wc); 26 | } 27 | /** 28 | * _trspace - Moves adress to remove trailig whitespaces 29 | * @st: string 30 | * 31 | * Return: Pointer 32 | */ 33 | char *_trspace(char *st) 34 | { 35 | while (*st == ' ') 36 | st++; 37 | return (st); 38 | } 39 | /** 40 | * strtow - splits a string into words 41 | * @str: string 42 | * 43 | * Return: Double Pointer 44 | */ 45 | char **strtow(char *str) 46 | { 47 | char **s, *ts; 48 | int l, l2, wc, i, j, fr, k; 49 | 50 | if (str == NULL || *str == 0) 51 | return (0); 52 | fr = 0; 53 | wc = _wcount(str); 54 | if (wc == 0) 55 | return (0); 56 | s = malloc((wc + 1) * sizeof(char *)); 57 | if (s == 0) 58 | return (0); 59 | ts = _trspace(str); 60 | for (i = 0; i < wc; i++) 61 | { 62 | l = 0; 63 | while (*(ts + l) != ' ' && *(ts + l) != 0) 64 | l++; 65 | s[i] = malloc((l + 1) * sizeof(char)); 66 | if (s[i] == 0) 67 | { 68 | fr = 1; 69 | break; 70 | } 71 | for (j = 0, l2 = 0; l2 < l; l2++, j++) 72 | s[i][j] = *(ts + l2); 73 | s[i][j] = '\0'; 74 | ts = _trspace(ts + l); 75 | } 76 | s[i] = NULL; 77 | if (fr == 1) 78 | { 79 | for (k = 0; k <= i; k++) 80 | free(s[k]); 81 | free(s); 82 | } 83 | return (s); 84 | } 85 | -------------------------------------------------------------------------------- /0x0B-malloc_free/2-str_concat.c: -------------------------------------------------------------------------------- 1 | # include "main.h" 2 | # include 3 | 4 | /** 5 | *str_concat - concat 2 strings. 6 | *@s1: first string. 7 | *@s2: second string. 8 | *Return: pointer to string. 9 | */ 10 | 11 | char *str_concat(char *s1, char *s2) 12 | { 13 | char *s; 14 | int i = 0, j = 0, k = 0; 15 | 16 | if (s1 != NULL) 17 | for (; s1[i]; i++) 18 | ; 19 | if (s2 != NULL) 20 | for (; s2[j]; j++) 21 | ; 22 | 23 | s = malloc(sizeof(char) * (i + j + 1)); 24 | if (s == NULL) 25 | return (NULL); 26 | 27 | while (k < i) 28 | { 29 | s[k] = s1[k]; 30 | k++; 31 | } 32 | 33 | while (k < i + j) 34 | { 35 | s[k] = s2[k - i]; 36 | k++; 37 | } 38 | s[k] = '\0'; 39 | return (s); 40 | } 41 | -------------------------------------------------------------------------------- /0x0B-malloc_free/3-alloc_grid.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * alloc_grid - returns a pointer to a 2 dimensional array of integers 7 | * @width: columns 8 | * @height: rows 9 | * 10 | * Return: Double Pointer 11 | */ 12 | 13 | int **alloc_grid(int width, int height) 14 | { 15 | int **s; 16 | int i, j, k, fr; 17 | 18 | fr = 0; 19 | if (width <= 0 || height <= 0) 20 | return (0); 21 | 22 | s = malloc(height * sizeof(int *)); 23 | if (s == 0) 24 | return (0); 25 | for (i = 0; i < height; i++) 26 | { 27 | *(s + i) = malloc(width * sizeof(int)); 28 | if (*(s + i) == 0) 29 | { 30 | fr = 1; 31 | break; 32 | } 33 | for (j = 0; j < width; j++) 34 | { 35 | s[i][j] = 0; 36 | } 37 | } 38 | if (fr == 1) 39 | { 40 | for (k = 0; k <= i; k++) 41 | { 42 | free(*(s + k)); 43 | } 44 | free(s); 45 | } 46 | return (s); 47 | } 48 | -------------------------------------------------------------------------------- /0x0B-malloc_free/4-free_grid.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * free_grid - frees a 2 dimensional grid previously created by 7 | * alloc_grid function. 8 | * @grid: matrix double pointer 9 | * @height: rows (pointer) 10 | * 11 | * Return: Nothing 12 | */ 13 | 14 | void free_grid(int **grid, int height) 15 | { 16 | int i; 17 | 18 | for (i = 0; i < height; i++) 19 | { 20 | free(*(grid + i)); 21 | } 22 | free(grid); 23 | } 24 | -------------------------------------------------------------------------------- /0x0B-malloc_free/README.md: -------------------------------------------------------------------------------- 1 | # Malloc in C Programming 2 | -------------------------------------------------------------------------------- /0x0B-malloc_free/main.h: -------------------------------------------------------------------------------- 1 | int _putchar(char c); 2 | char *create_array(unsigned int size, char c); 3 | char *_strdup(char *str); 4 | char *str_concat(char *s1, char *s2); 5 | int **alloc_grid(int width, int height); 6 | void free_grid(int **grid, int height); 7 | char *argstostr(int ac, char **av); 8 | char **strtow(char *str); 9 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/0-malloc_checked.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | *malloc_checked - allocate memory. 6 | *@b: size of the memory to be allocated. 7 | *Return: pointer; 8 | **/ 9 | 10 | void *malloc_checked(unsigned int b) 11 | { 12 | char *p; 13 | 14 | p = malloc(b); 15 | if (p == NULL) 16 | { 17 | exit(98); 18 | return (NULL); 19 | } 20 | return (p); 21 | } 22 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/1-string_nconcat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * string_nconcat - concatenates two strings 7 | * @s1: string1 8 | * @s2: string2 9 | * @n: n bytes of string 2 10 | * 11 | * Return: Pointer to allocated memory of s1 + nbytes of s2 12 | */ 13 | char *string_nconcat(char *s1, char *s2, unsigned int n) 14 | { 15 | unsigned int l1, l2, i, j; 16 | char *s; 17 | char *nul = ""; 18 | 19 | if (s1 == NULL) 20 | s1 = nul; 21 | if (s2 == NULL) 22 | s2 = nul; 23 | 24 | l1 = 0, l2 = 0; 25 | while (*(s1 + l1)) 26 | l1++; 27 | while (*(s2 + l2)) 28 | l2++; 29 | 30 | if (n < l2) 31 | l2 = n; 32 | 33 | s = malloc(sizeof(char) * (l1 + l2 + 1)); 34 | 35 | if (s == 0) 36 | return (0); 37 | 38 | for (i = 0; i < l1; i++) 39 | *(s + i) = *(s1 + i); 40 | 41 | for (i = 0, j = l1; i < l2; j++, i++) 42 | *(s + j) = *(s2 + i); 43 | 44 | *(s + j) = '\0'; 45 | 46 | return (s); 47 | } 48 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/100-realloc.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | 5 | 6 | /** 7 | * _memcpy - copies memory 8 | * @dest: destination 9 | * @src: source 10 | * @n: size of memory to copy 11 | * 12 | * Return: Returns memory copied 13 | */ 14 | char *_memcpy(char *dest, char *src, unsigned int n) 15 | { 16 | unsigned int i; 17 | 18 | for (i = 0; i < n; i++) 19 | dest[i] = src[i]; 20 | return (dest); 21 | } 22 | 23 | /** 24 | * _realloc - reallocates a memory block using malloc and free 25 | * @ptr: pointer to modify 26 | * @old_size: current size of memory 27 | * @new_size: size memory will now have 28 | * 29 | * Return: Pointer to reallocated memory 30 | */ 31 | void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size) 32 | { 33 | void *ptr2; 34 | 35 | if (old_size == new_size) 36 | return (ptr); 37 | 38 | if (ptr == NULL) 39 | { 40 | ptr2 = malloc(new_size); 41 | if (ptr2 == 0) 42 | return (0); 43 | free(ptr); 44 | return (ptr2); 45 | } 46 | 47 | if (new_size == 0 && ptr != NULL) 48 | { 49 | free(ptr); 50 | return (0); 51 | } 52 | 53 | ptr2 = malloc(new_size); 54 | if (ptr2 == 0) 55 | return (0); 56 | 57 | _memcpy(ptr2, ptr, old_size); 58 | free(ptr); 59 | return (ptr2); 60 | } 61 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/101-mul.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _atoi_digit - convert a char to integer. 6 | * @x: character to convert. 7 | * Return: integer. 8 | **/ 9 | 10 | int _atoi_digit(char x) 11 | { 12 | unsigned int res; 13 | 14 | if (x <= '9' && x >= '0') 15 | res = x - '0'; 16 | return (res); 17 | } 18 | 19 | /** 20 | * _isNumber - Define if a string is a number. 21 | * @argv: Pointer to string. 22 | * Return: success (0). 23 | **/ 24 | int _isNumber(char *argv) 25 | { 26 | int i; 27 | 28 | for (i = 0; argv[i]; i++) 29 | if (argv[i] < 48 || argv[i] > 57) 30 | return (1); 31 | return (0); 32 | } 33 | 34 | /** 35 | *_calloc - allocate array of size * nmemb. 36 | * @nmemb: number of elements. 37 | * @size: size of element. 38 | * Return: pointer to array. 39 | **/ 40 | 41 | void *_calloc(unsigned int nmemb, unsigned int size) 42 | { 43 | char *tab; 44 | unsigned int i; 45 | 46 | tab = malloc(size * nmemb); 47 | 48 | if (tab == NULL) 49 | return (NULL); 50 | 51 | for (i = 0; i < (size * nmemb); i++) 52 | tab[i] = '0'; 53 | 54 | return (tab); 55 | } 56 | 57 | /** 58 | * mul_array - multiply two arrays. 59 | * @a1: first array. 60 | * @len1: length of array a1. 61 | * @a2: char. 62 | * @a3: array for result. 63 | * @lena: length of array a3. 64 | * Return: pointer to array. 65 | **/ 66 | 67 | void *mul_array(char *a1, int len1, char a2, char *a3, int lena) 68 | { 69 | int mul = 0, i, k; 70 | 71 | k = lena; 72 | for (i = len1 - 1; i >= 0 ; i--) 73 | { 74 | mul += (a1[i] - '0') * (a2 - '0') + (a3[k] - '0'); 75 | a3[k] = (mul % 10) + '0'; 76 | mul /= 10; 77 | k--; 78 | } 79 | 80 | while (mul != 0) 81 | { 82 | mul += a3[k] - '0'; 83 | a3[k] = (mul % 10) + '0'; 84 | mul /= 10; 85 | k--; 86 | } 87 | 88 | return (a3); 89 | } 90 | /** 91 | * print_array - print all digits of array. 92 | * @nb: number of elements to print. 93 | * @a: array of elements. 94 | **/ 95 | void print_array(char *a, int nb) 96 | { 97 | int i = 0; 98 | 99 | while (a[i] == '0' && (i + 1) < nb) 100 | { 101 | i++; 102 | } 103 | for (; i < nb; i++) 104 | { 105 | _putchar(a[i]); 106 | } 107 | _putchar('\n'); 108 | } 109 | 110 | /** 111 | *main - print the multiplication of 2 numbers. 112 | *@argc: array length. 113 | *@argv: array. 114 | *Return: 0. 115 | */ 116 | 117 | int main(int argc, char *argv[]) 118 | { 119 | int i, c, len1, len2, lenres; 120 | char E[6] = {'E', 'r', 'r', 'o', 'r', '\n'}; 121 | char *tabres; 122 | 123 | if (argc != 3 || _isNumber(argv[1]) == 1 || _isNumber(argv[2]) == 1) 124 | { 125 | for (i = 0; i < 6; i++) 126 | { 127 | _putchar(E[i]); 128 | } 129 | exit(98); 130 | } 131 | for (len1 = 0; argv[1][len1]; len1++) 132 | ; 133 | for (len2 = 0; argv[2][len2]; len2++) 134 | ; 135 | lenres = len1 + len2; 136 | tabres = _calloc(lenres, sizeof(int)); 137 | if (tabres == NULL) 138 | { 139 | free(tabres); 140 | return (0); 141 | } 142 | for (i = len2 - 1, c = 0; i >= 0; i--) 143 | { 144 | tabres = mul_array(argv[1], len1, argv[2][i], tabres, (lenres - 1 - c)); 145 | c++; 146 | } 147 | print_array(tabres, lenres); 148 | free(tabres); 149 | exit(EXIT_SUCCESS); 150 | return (0); 151 | } 152 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/2-calloc.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | /** 5 | * _memset - initializes n bytes of memory to x 6 | * @ptr: initial adress 7 | * @x: variable to initialize with 8 | * @n: number of bytes to initialize 9 | * 10 | * Return: Return pointer char (so movements are 1 byte) 11 | */ 12 | char *_memset(char *ptr, int x, unsigned int n) 13 | { 14 | unsigned int i; 15 | 16 | for (i = 0; i < n; i++) 17 | ptr[i] = x; 18 | 19 | return (ptr); 20 | } 21 | 22 | /** 23 | * _calloc - allocates memory using malloc and initializes in 0 24 | * @nmemb: number of elements of array to allocate 25 | * @size: size of elements 26 | * 27 | * Return: Pointer to allocated memory or normal process termination 28 | * with a status value of 98 29 | */ 30 | void *_calloc(unsigned int nmemb, unsigned int size) 31 | { 32 | void *p; 33 | 34 | 35 | if (nmemb == 0 || size == 0) 36 | return (0); 37 | 38 | p = malloc(nmemb * size); 39 | if (p == 0) 40 | return (0); 41 | _memset(p, 0, size * nmemb); 42 | 43 | return (p); 44 | } 45 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/3-array_range.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * array_range - creates an array of integers 7 | * The array created should contain all the values 8 | * from min (included) to max (included), ordered from min to max 9 | * @min: minimal value 10 | * @max: maximum value 11 | * 12 | * Return: Pointer to allocated memory of s1 + nbytes of s2 13 | */ 14 | int *array_range(int min, int max) 15 | { 16 | int *p; 17 | int size, i, j; 18 | 19 | if (min > max) 20 | return (0); 21 | 22 | if (max > min) 23 | size = max - min + 1; 24 | else if (max == min) 25 | size = 2; 26 | 27 | p = malloc(sizeof(int) * size); 28 | if (p == 0) 29 | return (0); 30 | 31 | for (i = 0, j = min; j <= max; i++, j++) 32 | p[i] = j; 33 | if (max == min) 34 | p[i] = max; 35 | return (p); 36 | } 37 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/README.md: -------------------------------------------------------------------------------- 1 | # More malloc, free 2 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/main.h: -------------------------------------------------------------------------------- 1 | int _putchar(char c); 2 | void *malloc_checked(unsigned int b); 3 | char *string_nconcat(char *s1, char *s2, unsigned int n); 4 | void *_calloc(unsigned int nmemb, unsigned int size); 5 | int *array_range(int min, int max); 6 | void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size); 7 | -------------------------------------------------------------------------------- /0x0D-preprocessor/0-object_like_macro.h: -------------------------------------------------------------------------------- 1 | #ifndef SIZE 2 | #define SIZE 1024 3 | #endif 4 | -------------------------------------------------------------------------------- /0x0D-preprocessor/1-pi.h: -------------------------------------------------------------------------------- 1 | #ifndef PI 2 | #define PI 3.14159265359 3 | #endif 4 | -------------------------------------------------------------------------------- /0x0D-preprocessor/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Print the the name of the file it was compiled from. 5 | * Return: success (0). 6 | **/ 7 | 8 | int main(void) 9 | { 10 | printf("%s\n", __FILE__); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x0D-preprocessor/3-function_like_macro.h: -------------------------------------------------------------------------------- 1 | #ifndef ABS_H 2 | #define ABS_H 3 | #define ABS(a) (((a) < 0) ? (-(a)) : (a)) 4 | #endif 5 | -------------------------------------------------------------------------------- /0x0D-preprocessor/4-sum.h: -------------------------------------------------------------------------------- 1 | #ifndef SUM_H 2 | #define SUM_H 3 | #define SUM(x, y) (x + y) 4 | #endif 5 | -------------------------------------------------------------------------------- /0x0D-preprocessor/README.md: -------------------------------------------------------------------------------- 1 | # Preprocessor in C 2 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/1-init_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | /** 4 | * init_dog - Initialize a variable of type struct dog. 5 | * @d: Variable to initialize. 6 | * @name: Dog's name. 7 | * @age: Dog's age. 8 | * @owner: Dog's owner. 9 | **/ 10 | void init_dog(struct dog *d, char *name, float age, char *owner) 11 | { 12 | if (d != NULL) 13 | { 14 | d->name = name; 15 | d->age = age; 16 | d->owner = owner; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/2-print_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | /** 4 | * print_dog - prints a struct dog 5 | * @d: Pointer to struct 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | void print_dog(struct dog *d) 11 | { 12 | if (d != 0) 13 | { 14 | if ((*d).name != 0) 15 | printf("Name: %s\n", (*d).name); 16 | else 17 | printf("Name: (nil)\n"); 18 | printf("Age: %f\n", (*d).age); 19 | if ((*d).owner != 0) 20 | printf("Owner: %s\n", (*d).owner); 21 | else 22 | printf("Owner: (nil)\n"); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/4-new_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | /** 4 | * _strdup - returns a pointer to a newly allocated space in memory, which 5 | * contains a copy of the string given as a parameter. 6 | * @str: string to copy 7 | * 8 | * Return: Pointer 9 | */ 10 | char *_strdup(char *str) 11 | { 12 | int l, i; 13 | char *s; 14 | 15 | if (str == NULL) 16 | return (0); 17 | 18 | l = 0; 19 | while (*(str + l)) 20 | l++; 21 | 22 | s = malloc(sizeof(char) * l + 1); 23 | 24 | if (s == 0) 25 | return (0); 26 | 27 | for (i = 0; i <= l; i++) 28 | { 29 | *(s + i) = *(str + i); 30 | } 31 | return (s); 32 | } 33 | /** 34 | * new_dog - creates a new dog 35 | * @name: name of dog 36 | * @age: age of dog 37 | * @owner: owner of dog 38 | * 39 | * Return: On success 1. 40 | * On error, -1 is returned, and errno is set appropriately. 41 | */ 42 | dog_t *new_dog(char *name, float age, char *owner) 43 | { 44 | dog_t *new_dog; 45 | 46 | new_dog = malloc(sizeof(struct dog)); 47 | 48 | if (new_dog == 0 || name == 0 || owner == 0) 49 | return (0); 50 | 51 | new_dog->name = _strdup(name); 52 | if (new_dog->name == 0) 53 | { 54 | free(new_dog); 55 | return (0); 56 | } 57 | new_dog->age = age; 58 | new_dog->owner = _strdup(owner); 59 | if (new_dog->owner == 0) 60 | { 61 | free(new_dog); 62 | free(new_dog->name); 63 | return (0); 64 | } 65 | return (new_dog); 66 | } 67 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/5-free_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | /** 4 | * free_dog - frees memory of structure dogs 5 | * @d: pointer of structure 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | void free_dog(dog_t *d) 11 | { 12 | if (d != 0) 13 | { 14 | free(d->name); 15 | free(d->owner); 16 | free(d); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/README.md: -------------------------------------------------------------------------------- 1 | # Structures & typedef in C 2 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/dog.h: -------------------------------------------------------------------------------- 1 | #ifndef STRUCTS 2 | #define STRUCTS 3 | /** 4 | * struct dog - description for a pet 5 | * @name: pet name 6 | * @age: pet age 7 | * @owner: owner 8 | * 9 | * Description: Longer description 10 | */ 11 | typedef struct dog 12 | { 13 | char *name; 14 | float age; 15 | char *owner; 16 | } dog_t; 17 | #endif 18 | #ifndef _FUNCTIONS_H 19 | #define _FUNCTIONS_H 20 | int _putchar(char c); 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 | #endif 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Alx Low Level programming 2 | --------------------------------------------------------------------------------