├── 0x00-hello_world ├── .6-size.c.swp ├── 0-preprocessor ├── 1-compiler ├── 100-intel ├── 101-quote.c ├── 2-assembler ├── 3-name ├── 4-puts.c ├── 5-printf.c ├── 6-size.c ├── README.md └── size32 ├── 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 ├── .README.md.swp ├── 0-putchar.c ├── 1-alphabet.c ├── 1-main.c ├── 10-add.c ├── 10-main.c ├── 100-main.c ├── 100-times_table.c ├── 101-natural.c ├── 102-fibonacci.c ├── 103-fibonacci.c ├── 104-fibonacci.c ├── 11-main.c ├── 11-print_to_98.c ├── 2-main.c ├── 2-print_alphabet_x10.c ├── 3-islower.c ├── 3-main.c ├── 4-isalpha.c ├── 4-main.c ├── 5-main.c ├── 5-sign.c ├── 6-abs.c ├── 6-main.c ├── 7-main.c ├── 7-print_last_digit.c ├── 8-24_hours.c ├── 8-main.c ├── 9-main.c ├── 9-times_table.c ├── README.md ├── _putchar └── 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 ├── 1-main.c ├── 10-main.c ├── 10-print_triangle.c ├── 100-prime_factor.c ├── 101-main.c ├── 101-print_number.c ├── 2-main.c ├── 2-mul.c ├── 3-main.c ├── 3-print_numbers.c ├── 4-main.c ├── 4-print_most_numbers.c ├── 5-main.c ├── 5-more_numbers.c ├── 6-main.c ├── 6-print_line.c ├── 7-main.c ├── 7-print_diagonal.c ├── 8-print_square.c ├── 9-fizz_buzz.c ├── README.md ├── _putchar └── main.h ├── 0x05-pointers_arrays_strings ├── 0-main.c ├── 0-reset_to_98.c ├── 1-main.c ├── 1-swap.c ├── 100-atoi.c ├── 100-main.c ├── 101-keygen.c ├── 2-main.c ├── 2-strlen.c ├── 3-main.c ├── 3-puts.c ├── 4-main.c ├── 4-print_rev.c ├── 5-main.c ├── 5-rev_string.c ├── 6-main.c ├── 6-puts2.c ├── 7-main.c ├── 7-puts_half.c ├── 8-main.c ├── 8-print_array.c ├── 9-main.c ├── 9-strcpy.c ├── README.md ├── Test │ ├── 1-main.c │ └── main.c ├── _putchar.c └── 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 ├── New-test │ ├── 0-main.c │ ├── 1-main.c │ ├── 100-main.c │ ├── 101-main.c │ ├── 103-main.c │ ├── 104-main.c │ ├── 2-main.c │ ├── 3-main.c │ ├── 4-main.c │ ├── 5-main.c │ ├── 6-main.c │ └── 7-main.c ├── README.md ├── _putchar.c └── main.h ├── 0x07-pointers_arrays_strings ├── 0-main.c ├── 0-memset.c ├── 1-main.c ├── 1-memcpy.c ├── 100-main.c ├── 100-set_string.c ├── 101-crackme_password ├── 2-main.c ├── 2-strchr.c ├── 3-main.c ├── 3-strspn.c ├── 4-main.c ├── 4-strpbrk.c ├── 5-main.c ├── 5-strstr.c ├── 7-main.c ├── 7-print_chessboard.c ├── 8-main.c ├── 8-print_diagsums.c ├── README.md ├── _putchar.c └── 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 ├── _putchar.c ├── main-all │ ├── .swp │ ├── 0-main.c │ ├── 1-main.c │ ├── 100-main.c │ ├── 101-main.c │ ├── 2-main.c │ ├── 3-main.c │ ├── 4-main.c │ ├── 5-main.c │ └── 6-main.c └── main.h ├── 0x09-static_libraries ├── 0-isupper.c ├── 0-isupper.o ├── 0-memset.c ├── 0-memset.o ├── 0-strcat.c ├── 0-strcat.o ├── 1-isdigit.c ├── 1-isdigit.o ├── 1-memcpy.c ├── 1-memcpy.o ├── 1-strncat.c ├── 1-strncat.o ├── 100-atoi.c ├── 100-atoi.o ├── 2-strchr.c ├── 2-strchr.o ├── 2-strlen.c ├── 2-strlen.o ├── 2-strncpy.c ├── 2-strncpy.o ├── 3-islower.c ├── 3-islower.o ├── 3-puts.c ├── 3-puts.o ├── 3-strcmp.c ├── 3-strcmp.o ├── 3-strspn.c ├── 3-strspn.o ├── 4-isalpha.c ├── 4-isalpha.o ├── 4-strpbrk.c ├── 4-strpbrk.o ├── 5-strstr.c ├── 5-strstr.o ├── 6-abs.c ├── 6-abs.o ├── 9-strcpy.c ├── 9-strcpy.o ├── README.md ├── _putchar.c ├── _putchar.o ├── create_static_lib.sh ├── liball.a ├── libmy.a ├── main.c ├── main.h ├── main.o └── quote ├── 0x0A-argc_argv ├── .3-mul.c.swp ├── 0-whatsmyname.c ├── 1-args.c ├── 100-change.c ├── 2-args.c ├── 3-mul.c ├── 4-add.c ├── README.md ├── _putchar └── main.h ├── 0x0B-malloc_free ├── 0-create_array.c ├── 1-strdup.c ├── 100-argstostr.c ├── 101-strtow.c ├── 2-str_concat.c ├── 3-alloc_grid.c ├── 4-free_grid.c ├── README.md ├── _putchar └── 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 ├── _putchar.c └── 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/.6-size.c.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mostafa101296/alx-low_level_programming/31145c309aa2ee439037a77e33b6ef9e6dc2c99f/0x00-hello_world/.6-size.c.swp -------------------------------------------------------------------------------- /0x00-hello_world/0-preprocessor: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -E $CFILE -o c 3 | -------------------------------------------------------------------------------- /0x00-hello_world/1-compiler: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -c $CFILE 3 | -------------------------------------------------------------------------------- /0x00-hello_world/100-intel: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -S -masm=intel $CFILE 3 | -------------------------------------------------------------------------------- /0x00-hello_world/101-quote.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main - Entry 5 | * Return: 1 6 | */ 7 | int main(void) 8 | { 9 | write(1, "and that piece of art is useful\" - Dora Korpar, 2015-10-19\n", 59); 10 | return (1); 11 | } 12 | -------------------------------------------------------------------------------- /0x00-hello_world/2-assembler: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -S $CFILE 3 | -------------------------------------------------------------------------------- /0x00-hello_world/3-name: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc $CFILE -o cisfun 3 | -------------------------------------------------------------------------------- /0x00-hello_world/4-puts.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - A program that prints a line with puts function 4 | * Return: 0 (Success) 5 | */ 6 | int main(void) 7 | { 8 | puts("\"Programming is like building a multilingual puzzle"); 9 | return (0); 10 | } 11 | -------------------------------------------------------------------------------- /0x00-hello_world/5-printf.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * *main - A program that print a line using the printf function 4 | * Return: 0 (Success) 5 | */ 6 | int main(void) 7 | { 8 | printf("with proper grammar, but the outcome is a piece of art,\n"); 9 | return (0); 10 | } 11 | -------------------------------------------------------------------------------- /0x00-hello_world/6-size.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - Entry 4 | * Return: Always 0 5 | */ 6 | int main(void) 7 | { 8 | printf("Size of a char: %lu byte(s)\n", sizeof(char)); 9 | printf("Size of an int: %lu byte(s)\n", sizeof(int)); 10 | printf("Size of a long int: %lu byte(s)\n", sizeof(long int)); 11 | printf("Size of a long long int: %lu byte(s)\n", sizeof(long long int)); 12 | printf("Size of a float: %lu byte(s)\n", sizeof(float)); 13 | return (0); 14 | } 15 | -------------------------------------------------------------------------------- /0x00-hello_world/README.md: -------------------------------------------------------------------------------- 1 | Preprocessor 2 | -------------------------------------------------------------------------------- /0x00-hello_world/size32: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mostafa101296/alx-low_level_programming/31145c309aa2ee439037a77e33b6ef9e6dc2c99f/0x00-hello_world/size32 -------------------------------------------------------------------------------- /0x01-variables_if_else_while/0-positive_or_negative.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | /** 5 | * main - entry point 6 | * Description: prints if a number is positive or negative. 7 | * Return: 0 8 | */ 9 | int main(void) 10 | { 11 | int n; 12 | 13 | srand(time(0)); 14 | n = rand() - RAND_MAX / 2; 15 | if (n > 0) 16 | { 17 | printf("%d is positive\n", n); 18 | } 19 | else if (n == 0) 20 | { 21 | printf("%d is zero\n", n); 22 | } 23 | else if (n < 0) 24 | { 25 | printf("%d is negative\n", n); 26 | } 27 | return (0); 28 | } 29 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/1-last_digit.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | /** 5 | * main - Entry point 6 | * Description: checks on the last digit of the assigned var 7 | * Return: Always 0 (success) 8 | */ 9 | int main(void) 10 | { 11 | int n, m; 12 | 13 | srand(time(0)); 14 | n = rand() - RAND_MAX / 2; 15 | m = n % 10; 16 | if (m > 5) 17 | { 18 | printf("Last digit of %d is %d and is greater than 5\n", n, m); 19 | } 20 | else if (m < 6 && m != 0) 21 | { 22 | printf("Last digit of %d is %d and is less than 6 and not 0\n", n, m); 23 | } 24 | else 25 | { 26 | printf("Last digit of %d is %d and is 0\n", n, m); 27 | } 28 | return (0); 29 | } 30 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/100-print_comb3.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main - Entyr point 5 | * Description: prints two digits combination 6 | * Return: Always 0 (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 | putchar('\n'); 30 | return (0); 31 | } 32 | -------------------------------------------------------------------------------- /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 | putchar('\n'); 34 | return (0); 35 | } 36 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/102-print_comb5.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main - Entry point 5 | * Description: prints all possible combination of 2-digit numbers 6 | * Return: Always 0 (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 | putchar('\n'); 43 | return (0); 44 | } 45 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/2-print_alphabet.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main - Entry point 5 | * Description: prints the alphabet in lowercase\n 6 | * Return: Always 0 (success) 7 | */ 8 | int main(void) 9 | { 10 | int ch; 11 | 12 | for (ch = 'a'; ch <= 'z'; ch++) 13 | { 14 | putchar(ch); 15 | } 16 | putchar('\n'); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/3-print_alphabets.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main - Entry point 5 | * Description: prints alphabet in lowercase then uppercase 6 | * Return: Always 0 (success) 7 | */ 8 | int main(void) 9 | { 10 | int ch; 11 | 12 | for (ch = 'a'; ch <= 'z'; ch++) 13 | { 14 | putchar(ch); 15 | } 16 | for (ch = 'A'; ch <= 'Z'; ch++) 17 | { 18 | putchar(ch); 19 | } 20 | putchar('\n'); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/4-print_alphabt.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main - Entry point 5 | * Description: prints lowercase alphabets 6 | * Return: Always 0 (success) 7 | */ 8 | int main(void) 9 | { 10 | int ch; 11 | 12 | for (ch = 'a'; ch <= 'z'; ch++) 13 | { 14 | if (ch != 'e' && ch != 'q') 15 | { 16 | putchar(ch); 17 | } 18 | else 19 | { 20 | } 21 | } 22 | putchar('\n'); 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/5-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main -Entry point 5 | * Description: prints allsingle digit numbers of 6 | * base 10 from 0 7 | * Return: Always 0 (success) 8 | */ 9 | int main(void) 10 | { 11 | int a; 12 | 13 | for (a = 0; a < 10; a++) 14 | { 15 | printf("%d", a); 16 | } 17 | putchar('\n'); 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/6-print_numberz.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main - Entry point 5 | * Description: prints char below 10 6 | * Return: Always 0 (success) 7 | */ 8 | int main(void) 9 | { 10 | int a; 11 | 12 | for (a = 0; a < 10; a++) 13 | { 14 | putchar(a + '0'); 15 | } 16 | putchar('\n'); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/7-print_tebahpla.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main - Entry point 5 | * Description: prints lowercase alphabets in reverse 6 | * Return: Always 0 (success) 7 | */ 8 | int main(void) 9 | { 10 | int a; 11 | 12 | for (a = 'z'; a >= 'a'; a--) 13 | { 14 | putchar(a); 15 | } 16 | putchar('\n'); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/8-print_base16.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main - Entry point 5 | * Description: Print base 16 numbers 6 | * Return: Always 0 (success) 7 | */ 8 | int main(void) 9 | { 10 | char c; 11 | char d = '0'; 12 | 13 | while (d <= '9') 14 | { 15 | putchar(d); 16 | d++; 17 | } 18 | for (c = 'a'; c <= 'f'; c++) 19 | { 20 | putchar(c); 21 | } 22 | putchar('\n'); 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/9-print_comb.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main - Entry point 5 | * Description: prints numbers 6 | * Return: Always 0 (success) 7 | */ 8 | int main(void) 9 | { 10 | int i; 11 | 12 | for (i = '0'; i <= '9' ; i++) 13 | { 14 | putchar(i); 15 | 16 | if (i != '9') 17 | { 18 | putchar(','); 19 | putchar(' '); 20 | } 21 | } 22 | 23 | putchar('\n'); 24 | return (0); 25 | } 26 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/README.md: -------------------------------------------------------------------------------- 1 | 0-positive_or_negative.c 2 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/.README.md.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mostafa101296/alx-low_level_programming/31145c309aa2ee439037a77e33b6ef9e6dc2c99f/0x02-functions_nested_loops/.README.md.swp -------------------------------------------------------------------------------- /0x02-functions_nested_loops/0-putchar.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *main - print _putchar is written by mbah 4 | *Return: 0 5 | */ 6 | int main(void) 7 | { 8 | _putchar('_'); 9 | _putchar('p'); 10 | _putchar('u'); 11 | _putchar('t'); 12 | _putchar('c'); 13 | _putchar('h'); 14 | _putchar('a'); 15 | _putchar('r'); 16 | _putchar('\n'); 17 | return (0); 18 | } 19 | 20 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/1-alphabet.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * print_alphabet - print all alphabet in lowercase 4 | */ 5 | void print_alphabet(void) 6 | { 7 | char letter = 'a'; 8 | 9 | while (letter <= 'z') 10 | { 11 | _putchar(letter); 12 | letter++; 13 | } 14 | _putchar('\n'); 15 | } 16 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/1-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - check the code 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | print_alphabet(); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/10-add.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * add - adds two integers 4 | * @n1: integer 1 5 | * @n2: integer 2 6 | * Description: give two integers, returns sum 7 | * Return: sum 8 | */ 9 | 10 | int add(int n1, int n2) 11 | { 12 | int sum; 13 | 14 | sum = n1 + n2; 15 | 16 | return (sum); 17 | } 18 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/10-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int n; 12 | 13 | n = add(89, 9); 14 | printf("%d\n", n); 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/100-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - check the code. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | print_times_table(3); 11 | _putchar('\n'); 12 | print_times_table(5); 13 | _putchar('\n'); 14 | print_times_table(98); 15 | _putchar('\n'); 16 | print_times_table(12); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/100-times_table.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_times_table - Prints a multiplication table up to param 5 | * @n: The number to be treated 6 | * 7 | * Return: Number matrix 8 | */ 9 | void print_times_table(int n) 10 | { 11 | int x, y, z; 12 | 13 | if (n >= 0 && n <= 14) 14 | { 15 | for (x = 0; x <= n; x++) 16 | { 17 | for (y = 0; y <= n; y++) 18 | { 19 | z = x * y; 20 | if (z > 99) 21 | { 22 | _putchar(','); 23 | _putchar(32); 24 | _putchar((z / 100) + '0'); 25 | _putchar(((z / 10) % 10) + '0'); 26 | _putchar((z % 10) + '0'); 27 | } 28 | else if (z > 9) 29 | { 30 | _putchar(','); 31 | _putchar(32); 32 | _putchar(32); 33 | _putchar(((z / 10) % 10) + '0'); 34 | _putchar((z % 10) + '0'); 35 | } 36 | else 37 | { 38 | if (y != 0) 39 | { 40 | _putchar(','); 41 | _putchar(32); 42 | _putchar(32); 43 | _putchar(32); 44 | } 45 | _putchar(z + '0'); 46 | } 47 | } 48 | _putchar('\n'); 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/101-natural.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Prints the sum of all multiples of 3 or 5 up to 102 5 | * Return: Always (Success) 6 | */ 7 | int main(void) 8 | { 9 | int i, z = 0; 10 | 11 | while (i < 1024) 12 | { 13 | if ((i % 3 == 0) || (i % 5 == 0)) 14 | { 15 | z += i; 16 | } 17 | 18 | i++; 19 | } 20 | 21 | printf("%d\n", z); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/102-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Prints the first 52 fibonacci numbers 5 | * 6 | * Return: Nothing! 7 | */ 8 | int main(void) 9 | { 10 | int i = 0; 11 | long j = 1, k = 2; 12 | 13 | while (i < 50) 14 | { 15 | if (i == 0) 16 | printf("%ld", j); 17 | else if (i == 1) 18 | printf(", %ld", k); 19 | else 20 | { 21 | k += j; 22 | j = k - j; 23 | printf(", %ld", k); 24 | } 25 | 26 | ++i; 27 | } 28 | 29 | printf("\n"); 30 | return (0); 31 | } 32 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/103-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - main block 4 | * Description: computes and prints even number < 4,000,000 5 | * 5 below 1024 (excluded), followed by a new line 6 | * Return: 0 7 | */ 8 | int main(void) 9 | { 10 | int a = 0, b = 1, next = 0; 11 | int sum = 0; 12 | 13 | while (next < 4000000) 14 | { 15 | next = a + b; 16 | a = b; 17 | b = next; 18 | if (next % 2 == 0) 19 | sum += next; 20 | } 21 | printf("%i\n", sum); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/104-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | /** 5 | * numLength - returns the lenth of string 6 | * @num : operand number 7 | * Return: number of digits 8 | */ 9 | 10 | int numLength(int num) 11 | { 12 | int length = 0; 13 | 14 | if (!num) 15 | { 16 | return (1); 17 | } 18 | 19 | while (num) 20 | { 21 | num = num / 10; 22 | length += 1; 23 | } 24 | 25 | return (length); 26 | } 27 | /** 28 | * *main - prints the first 98 fibonaci sequences 29 | * Return: 0 30 | */ 31 | 32 | int main(void) 33 | { 34 | unsigned long f1 = 1, f2 = 2, tmp, mx = 100000000, f1o = 0, f2o = 0, tmpo = 0; 35 | short int i = 1, initial0s; 36 | 37 | while (i <= 98) 38 | { 39 | if (f1o > 0) 40 | printf("%lu", f1o); 41 | initial0s = numLength(mx) - 1 - numLength(f1); 42 | while (f1o > 0 && initial0s > 0) 43 | { 44 | printf("%i", 0); 45 | initial0s--; 46 | } 47 | printf("%lu", f1); 48 | 49 | tmp = (f1 + f2) % mx; 50 | tmpo = f1o + f2o + (f1 + f2) / mx; 51 | f1 = f2; 52 | f1o = f2o; 53 | f2 = tmp; 54 | f2o = tmpo; 55 | 56 | if (i != 98) 57 | printf(", "); 58 | else 59 | printf("\n"); 60 | i++; 61 | } 62 | return (0); 63 | } 64 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/11-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - check the code 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | print_to_98(0); 11 | print_to_98(98); 12 | print_to_98(111); 13 | print_to_98(81); 14 | print_to_98(-10); 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/11-print_to_98.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * print_to_98 - prints numbers to 98 6 | * @n: starting integer 7 | * 8 | * Description: prints all natural number from n - 98 9 | * 10 | * Return: void 11 | */ 12 | void print_to_98(int n) 13 | { 14 | if (n < 98) 15 | { 16 | while (n < 98) 17 | { 18 | printf("%d, ", n); 19 | n++; 20 | } 21 | } 22 | else if (n > 98) 23 | { 24 | while (n > 98) 25 | { 26 | printf("%d, ", n); 27 | n--; 28 | } 29 | } 30 | printf("98\n"); 31 | } 32 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/2-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - check the code. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | print_alphabet_x10(); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/2-print_alphabet_x10.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * print_alphabet_x10 - Make alphabet x10 times 4 | * 5 | * Return: void 6 | */ 7 | void print_alphabet_x10(void) 8 | { 9 | char c; 10 | int i = 0; 11 | 12 | while (i <= 9) 13 | { 14 | for (c = 'a'; c <= 'z'; c++) 15 | { 16 | _putchar(c); 17 | } 18 | 19 | _putchar('\n'); 20 | 21 | i++; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/3-islower.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _islower - Entry point 4 | * Description: checks is a character is lowercase 5 | * @c: the integer value it receives 6 | * Return: 1 if true. 0 if false. 7 | */ 8 | int _islower(int c) 9 | { 10 | int i = 'a'; 11 | 12 | for (i = 'a'; i <= 'z'; i++) 13 | { 14 | /* refer int c*/ 15 | if (c == i) 16 | { 17 | return (1); 18 | } 19 | } 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/3-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - check the code. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | int r; 11 | 12 | r = _islower('H'); 13 | _putchar(r + '0'); 14 | r = _islower('o'); 15 | _putchar(r + '0'); 16 | r = _islower(108); 17 | _putchar(r + '0'); 18 | _putchar('\n'); 19 | return (0); 20 | } 21 | 22 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/4-isalpha.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _isalpha - Entry point 4 | * @c: the integer value it receives 5 | * Description: checks if alphabet 6 | * Return: 1 if true. 0 if false 7 | */ 8 | 9 | int _isalpha(int c) 10 | { 11 | if (c >= 'a' && c <= 'z') 12 | { 13 | return (1); 14 | } 15 | else if (c >= 'A' && c <= 'Z') 16 | { 17 | return (1); 18 | } 19 | else 20 | { 21 | return (0); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/4-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - check the code. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | int r; 11 | 12 | r = _isalpha('H'); 13 | _putchar(r + '0'); 14 | r = _isalpha('o'); 15 | _putchar(r + '0'); 16 | r = _isalpha(108); 17 | _putchar(r + '0'); 18 | r = _isalpha(';'); 19 | _putchar(r + '0'); 20 | _putchar('\n'); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/5-main.c: -------------------------------------------------------------------------------- 1 | /** 2 | * main - check the code. 3 | * 4 | * Return: Always 0. 5 | */ 6 | int main(void) 7 | { 8 | int r; 9 | 10 | r = print_sign(98); 11 | _putchar(','); 12 | _putchar(' '); 13 | _putchar(r + '0'); 14 | _putchar('\n'); 15 | r = print_sign(0); 16 | _putchar(','); 17 | _putchar(' '); 18 | _putchar(r + '0'); 19 | _putchar('\n'); 20 | r = print_sign(0xff); 21 | _putchar(','); 22 | _putchar(' '); 23 | _putchar(r + '0'); 24 | _putchar('\n'); 25 | r = print_sign(-1); 26 | _putchar(','); 27 | _putchar(' '); 28 | _putchar(r + '0'); 29 | _putchar('\n'); 30 | return (0); 31 | } 32 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/5-sign.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * print_sign - Entr point 4 | * @n: carrier variable 5 | * Description: prints the sign of a number 6 | * Return: 1 if n>0, 0 if n == 0, -1 if n<0 7 | */ 8 | 9 | int print_sign(int n) 10 | { 11 | if (n > 0) 12 | { 13 | _putchar('+'); 14 | return (1); 15 | } 16 | else if (n < 0) 17 | { 18 | _putchar('-'); 19 | return (-1); 20 | } 21 | _putchar('0'); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/6-abs.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _abs - Entry point 4 | * @n: n is an integer 5 | * Description: prints the absolute value of an integer 6 | * Return: int 7 | */ 8 | 9 | int _abs(int n) 10 | { 11 | if (n > 0) 12 | { 13 | return (n); 14 | } 15 | else if (n < 0) 16 | { 17 | return (-n); 18 | } 19 | else 20 | { 21 | return (n); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/6-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int r; 12 | 13 | r = _abs(-1); 14 | printf("%d\n", r); 15 | r = _abs(0); 16 | printf("%d\n", r); 17 | r = _abs(1); 18 | printf("%d\n", r); 19 | r = _abs(-98); 20 | printf("%d\n", r); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/7-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - check the code 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | int r; 11 | 12 | print_last_digit(98); 13 | print_last_digit(0); 14 | r = print_last_digit(-1024); 15 | _putchar('0' + r); 16 | _putchar('\n'); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/7-print_last_digit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * print_last_digit - prints the last digit of a number 4 | * @n: n is an integer 5 | * Description: prints the last digit of a number 6 | * Return: integer 7 | */ 8 | 9 | int print_last_digit(int n) 10 | { 11 | int last = n % 10; 12 | 13 | if (n < 0) 14 | { 15 | last = last * -1; 16 | } 17 | _putchar(last + '0'); 18 | return (last); 19 | } 20 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/8-24_hours.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * jack_bauer - prints every minute of the day 4 | * Description: prints every minute of the day 5 | * Return: void 6 | */ 7 | void jack_bauer(void) 8 | { 9 | int hours_tens, hours_ones, minutes_tens, minutes_ones, hours_max; 10 | 11 | hours_max = 58; 12 | hours_tens = '0'; 13 | while (hours_tens < '3') 14 | { 15 | if (hours_tens == '2') 16 | { 17 | hours_max = '4'; 18 | } 19 | hours_ones = '0'; 20 | while (hours_ones < hours_max) 21 | { 22 | minutes_tens = '0'; 23 | while (minutes_tens < '6') 24 | { 25 | minutes_ones = '0'; 26 | while (minutes_ones < 58) 27 | { 28 | _putchar(hours_tens); 29 | _putchar(hours_ones); 30 | _putchar(':'); 31 | _putchar(minutes_tens); 32 | _putchar(minutes_ones); 33 | _putchar('\n'); 34 | minutes_ones++; 35 | } 36 | minutes_ones = '0'; 37 | minutes_tens++; 38 | } 39 | minutes_tens = '0'; 40 | hours_ones++; 41 | } 42 | hours_ones = '0'; 43 | hours_tens++; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/8-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - check the code 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | jack_bauer(); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/9-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - check the code 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | times_table(); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/9-times_table.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * times_table - prints the 9 times table 4 | * Description: prints the 9 times table 5 | * Return: void 6 | */ 7 | 8 | void times_table(void) 9 | { 10 | int row, column, product, tens, ones; 11 | 12 | for (row = 0; row <= 9; row++) 13 | { 14 | for (column = 0; column <= 9; column++) 15 | { 16 | product = row * column; 17 | tens = product / 10; 18 | ones = product % 10; 19 | 20 | if (column == 0) 21 | { 22 | _putchar('0'); 23 | } 24 | else if (product < 10) 25 | { 26 | _putchar(','); 27 | _putchar(' '); 28 | _putchar(' '); 29 | _putchar(ones + '0'); 30 | } 31 | else 32 | { 33 | _putchar(','); 34 | _putchar(' '); 35 | _putchar(tens + '0'); 36 | _putchar(ones + '0'); 37 | } 38 | } 39 | _putchar('\n'); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/README.md: -------------------------------------------------------------------------------- 1 | # Functions, nested loops 2 | 3 | 4 | 5 | Project done during **Full Stack Software Engineering studies** at **ALX School**. It aims to learn about header files, functions, loops and nested loops in **C language**. 6 | 7 | 8 | 9 | ## Technologies 10 | 11 | * C files are compiled using `gcc 4.8.4` 12 | 13 | * C files are written according to the C90 standard 14 | 15 | * Tested on Ubuntu 14.04 LTS 16 | 17 | 18 | 19 | ## Files 20 | 21 | All of the following files are programs written in C: 22 | 23 | 24 | 25 | | Filename | Description | 26 | 27 | | -------- | ----------- | 28 | 29 | | `0-Putchar.c` | Prints `Putchar` | 30 | 31 | | `1-alphabet.c` | Prints the alphabet in lowercase | 32 | 33 | | `2-print_alphabet_x10.c` | Prints 10 times the alphabet, in lowercase | 34 | 35 | | `3-islower.c` | Checks for lowercase character | 36 | 37 | | `4-isalpha.c` | Checks for alphabetic character | 38 | 39 | | `5-sign.c` | Prints the sign of a number | 40 | 41 | | `6-abs.c` | Computes the absolute value of an integer | 42 | 43 | | `7-print_last_digit.c` | Prints the last digit of a number | 44 | 45 | | `8-24_hours.c` | Prints every minute of the day | 46 | 47 | | `9-times_table.c` | Prints the 9 times table, starting with 0 | 48 | 49 | | `10-add.c` | Adds two integers and returns the result | 50 | 51 | | `11-print_to_98.c` | Prints all natural numbers from `n` to `98` | 52 | 53 | | `100-times_table.c` | Prints the `n` times table, starting with 0 | 54 | 55 | | `101-natural.c` | Computes and prints the sum of all the multiplies of `3` or `5` below `1024` | 56 | 57 | | `102-fibonacci.c` | Prints the first 50 Fibonacci numbers, starting with `1` and `2` | 58 | 59 | | `103-fibonacci.c` | Finds and prints the sum of the even-valued terms | 60 | 61 | | `104-fibonacci.c` | Finds and prints the first 98 Fibonacci numbers | 62 | 63 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/_putchar: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _putchar - writes the character c to stdout 5 | * @c: The character to print 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | int _putchar(char c) 11 | { 12 | return (write(1, &c, 1)); 13 | } 14 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/main.h: -------------------------------------------------------------------------------- 1 | #ifndef main_h 2 | #define main_h 3 | 4 | int _putchar(char c); 5 | void print_alphabet(void); 6 | void print_alphabet_x10(void); 7 | int _islower(int c); 8 | int _isalpha(int c); 9 | int print_sign(int n); 10 | int _abs(int); 11 | int print_last_digit(int); 12 | void jack_bauer(void); 13 | void times_table(void); 14 | int add(int, int); 15 | void print_to_98(int n); 16 | void print_times_table(int n); 17 | #endif 18 | -------------------------------------------------------------------------------- /0x03-debugging/0-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - tests function that prints if integer is positive or negative 5 | * Return: 0 6 | */ 7 | 8 | int main(void) 9 | { 10 | int i; 11 | 12 | i = 0; 13 | positive_or_negative(i); 14 | 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x03-debugging/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - causes an infinite loop 5 | * Return: 0 6 | */ 7 | 8 | int main(void) 9 | { 10 | int i; 11 | 12 | printf("Infinite loop incoming :(\n"); 13 | 14 | i = 0; 15 | /* 16 | *while (i < 10) 17 | *{ 18 | * putchar(i); 19 | *} 20 | */ 21 | printf("Infinite loop avoided! \\o/\n"); 22 | 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x03-debugging/2-largest_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * largest_number - returns the largest of 3 numbers 5 | * @a: first int 6 | * @b: second int 7 | * @c: third int 8 | * Return: largest number 9 | */ 10 | 11 | int largest_number(int a, int b, int c) 12 | { 13 | int largest = b; 14 | 15 | if (a > b) 16 | { 17 | largest = a; 18 | } 19 | else if (c > b) 20 | { 21 | largest = c; 22 | } 23 | 24 | return (largest); 25 | } 26 | -------------------------------------------------------------------------------- /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 | * Description: prints the date 8 | * @month: month in number format 9 | * @day: day of month 10 | * @year: year 11 | * Return: void 12 | */ 13 | 14 | void print_remaining_days(int month, int day, int year) 15 | { 16 | if ((year % 100 == 0 && year % 400 == 0) || (year % 4 == 0)) 17 | { 18 | if (month > 2 && day >= 60) 19 | { 20 | day++; 21 | } 22 | 23 | printf("Day of the year: %d\n", day); 24 | printf("Remaining days: %d\n", 366 - day); 25 | } 26 | else 27 | { 28 | if (month == 2 && day == 60) 29 | { 30 | printf("Invalid date: %02d/%02d/%04d\n", month, day - 31, year); 31 | } 32 | else 33 | { 34 | printf("Day of the year: %d\n", day); 35 | printf("Remaining days: %d\n", 365 - day); 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /0x03-debugging/README.md: -------------------------------------------------------------------------------- 1 | I am learning on how to debug code 2 | -------------------------------------------------------------------------------- /0x03-debugging/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | #include 5 | 6 | void positive_or_negative(int i); 7 | int largest_number(int a, int b, int c); 8 | int convert_day(int month, int day); 9 | void print_remaining_days(int month, int day, int year); 10 | 11 | #endif /* MAIN_H */ 12 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/0-isupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isupper - checks is a character is uppercase 5 | * @c: carrier integer variable 6 | * Return: 1 if true, 0 if false 7 | */ 8 | 9 | int _isupper(int c) 10 | { 11 | if (c >= 'A' && c <= 'Z') 12 | { 13 | return (1); 14 | } 15 | else 16 | { 17 | return (0); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/1-isdigit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isdigit - checks if a character is digit 5 | * @c: carrier variable 6 | * Return: 1 if true, 0 if false 7 | */ 8 | 9 | int _isdigit(int c) 10 | { 11 | if (c >= '0' && c <= '9') 12 | return (1); 13 | 14 | else 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/1-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char c; 12 | 13 | c = '0'; 14 | printf("%c: %d\n", c, _isdigit(c)); 15 | c = 'a'; 16 | printf("%c: %d\n", c, _isdigit(c)); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/10-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - check the code 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | print_triangle(2); 11 | print_triangle(10); 12 | print_triangle(1); 13 | print_triangle(0); 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/10-print_triangle.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * print_triangle - check for a digit 4 | * @size : integer type 5 | * Return:void 6 | */ 7 | 8 | void print_triangle(int size) 9 | { 10 | int i = 1, ii; 11 | 12 | while (i <= size && size > 0) 13 | { 14 | ii = 0; 15 | while (ii < size - i) 16 | { 17 | _putchar(' '); 18 | ii++; 19 | } 20 | ii = 0; 21 | while (ii < i) 22 | { 23 | _putchar('#'); 24 | ii++; 25 | } 26 | 27 | _putchar('\n'); 28 | i++; 29 | } 30 | if (i == 1) 31 | _putchar('\n'); 32 | } 33 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/100-prime_factor.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - prime numbers 6 | * 7 | * Return: 0 8 | */ 9 | 10 | int main(void) 11 | { 12 | long num = 612852475143; 13 | long divisor = 2; 14 | long larg_prim = 0; 15 | 16 | while (num != 1) 17 | { 18 | if (num % divisor == 0) 19 | { 20 | num = num / divisor; 21 | larg_prim = divisor; 22 | } 23 | divisor += 1; 24 | } 25 | printf("%ld\n", larg_prim); 26 | return (0); 27 | } 28 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/101-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - check the code 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | print_number(98); 11 | _putchar('\n'); 12 | print_number(402); 13 | _putchar('\n'); 14 | print_number(1024); 15 | _putchar('\n'); 16 | print_number(0); 17 | _putchar('\n'); 18 | print_number(-98); 19 | _putchar('\n'); 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/101-print_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * print_number - prints numbers 4 | * @n: number to be printed 5 | * Return:void 6 | */ 7 | 8 | void print_number(int n) 9 | { 10 | unsigned int x; 11 | 12 | x = n; 13 | if (n < 0) 14 | { 15 | _putchar(45); 16 | x = -n; 17 | } 18 | if (x / 10) 19 | print_number(x / 10); 20 | _putchar((x % 10) + '0'); 21 | } 22 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/2-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | printf("%d\n", mul(98, 1024)); 12 | printf("%d\n", mul(-402, 4096)); 13 | return (0); 14 | } 15 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/2-mul.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * mul - gives the product of a and b 5 | * @a: variable 6 | * @b: variable 7 | * 8 | * Return: a * b 9 | */ 10 | 11 | int mul(int a, int b) 12 | { 13 | return (a * b); 14 | } 15 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/3-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - check the code 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | print_numbers(); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/3-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_numbers - print 0 - 9 5 | * 6 | * Return: void 7 | */ 8 | 9 | void print_numbers(void) 10 | { 11 | char a = 0; 12 | 13 | while (a <= 9) 14 | { 15 | _putchar(a + '0'); 16 | a++; 17 | } 18 | _putchar('\n'); 19 | } 20 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/4-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - check the code 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | print_most_numbers(); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/4-print_most_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_most_numbers - prints 0 - 9 except 2, 3 5 | * Description: prints 6 | * Return: void 7 | */ 8 | 9 | void print_most_numbers(void) 10 | { 11 | int a = 0; 12 | 13 | while (a <= 9) 14 | { 15 | if (a != 2 && a != 4) 16 | { 17 | _putchar(a + '0'); 18 | } 19 | a++; 20 | } 21 | _putchar('\n'); 22 | } 23 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/5-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - check the code 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | more_numbers(); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/5-more_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * more_numbers - prints 0 - 14 5 | * 6 | * Return: void 7 | */ 8 | 9 | void more_numbers(void) 10 | { 11 | char n, c; 12 | int i = 0; 13 | 14 | while (i < 10) 15 | { 16 | for (n = 0; n <= 14; n++) 17 | { 18 | c = n; 19 | if (n > 9) 20 | { 21 | _putchar('1'); 22 | c = n % 10; 23 | } 24 | _putchar('0' + c); 25 | } 26 | 27 | _putchar('\n'); 28 | i++; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/6-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - check the code 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | print_line(0); 11 | print_line(2); 12 | print_line(10); 13 | print_line(-4); 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/6-print_line.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_line - prints a line 5 | * @n: variable 6 | * 7 | * Return: 0 8 | */ 9 | 10 | void print_line(int n) 11 | { 12 | int a = 0; 13 | 14 | while (a < n && n > 0) 15 | { 16 | _putchar('_'); 17 | a++; 18 | } 19 | _putchar('\n'); 20 | } 21 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/7-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - check the code 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | print_diagonal(0); 11 | print_diagonal(2); 12 | print_diagonal(10); 13 | print_diagonal(-4); 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/7-print_diagonal.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_diagonal - diagonal lines made of backslashes 5 | * @n: number of \ to be printed 6 | * Return: void 7 | */ 8 | 9 | void print_diagonal(int n) 10 | { 11 | int i = 0, ii; 12 | 13 | while (i < n && n > 0) 14 | { 15 | ii = 0; 16 | while (ii < i) 17 | { 18 | _putchar(' '); 19 | ii++; 20 | } 21 | 22 | _putchar('\\'); 23 | _putchar('\n'); 24 | i++; 25 | } 26 | if (i == 0) 27 | _putchar('\n'); 28 | } 29 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/8-print_square.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * print_square - check for a digit 4 | * @n: number of _ to be printed 5 | * Return: void 6 | */ 7 | 8 | void print_square(int n) 9 | { 10 | int i = 0, ii; 11 | 12 | while (i < n && n > 0) 13 | { 14 | ii = 0; 15 | while (ii < n) 16 | { 17 | _putchar('#'); 18 | ii++; 19 | } 20 | _putchar('\n'); 21 | i++; 22 | } 23 | if (i == 0) 24 | _putchar('\n'); 25 | } 26 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/9-fizz_buzz.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - Fizz Buzz code 6 | * 7 | * Return: void 8 | */ 9 | 10 | int main(void) 11 | { 12 | int i = 1; 13 | 14 | while (i <= 100) 15 | { 16 | if (i % 3 == 0 && i % 5 == 0) 17 | { 18 | printf("FizzBuzz"); 19 | } 20 | else if (i % 3 == 0) 21 | { 22 | printf("Fizz"); 23 | } 24 | else if (i % 5 == 0) 25 | { 26 | printf("Buzz"); 27 | } 28 | else 29 | { 30 | printf("%i", i); 31 | } 32 | if (i != 100) 33 | { 34 | putchar(' '); 35 | } 36 | 37 | i++; 38 | } 39 | putchar('\n'); 40 | return (0); 41 | } 42 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/README.md: -------------------------------------------------------------------------------- 1 | 0. isupper 2 | Write a function that checks for uppercase character. 3 | Prototype: int isupper(int c); 4 | Returns 1 if c is uppercase 5 | Returns 0 otherwise 6 | 1. isdigit 7 | Write a function that checks for a digit (0 through 9). 8 | Prototype: int isdigit(int c); 9 | Returns 1 if c is a digit 10 | Returns 0 otherwise 11 | 2. Collaboration is multiplication 12 | Write a function that multiplies two integers. 13 | Prototype: int mul(int a, int b); 14 | 3. The numbers speak for themselves 15 | Write a function that prints the numbers, from 0 to 9, followed by a new line. 16 | Prototype: void print_numbers(void); 17 | You can only use _putchar twice in your code 18 | 4. I believe in numbers and signs 19 | Write a function that prints the numbers, from 0 to 9, followed by a new line. 20 | Prototype: void print_most_numbers(void); 21 | Do not print 2 and 4 22 | You can only use _putchar twice in your code 23 | 5. Numbers constitute the only universal language 24 | Write a function that prints 10 times the numbers, from 0 to 14, followed by a new line. 25 | Prototype: void more_numbers(void); 26 | You can only use _putchar three times in your code 27 | 6. The shortest distance between two points is a straight line 28 | Write a function that draws a straight line in the terminal. 29 | Prototype: void print_line(int n); 30 | You can only use _putchar function to print 31 | Where n is the number of times the character _ should be printed 32 | The line should end with a \n 33 | If n is 0 or less, the function should only print \n 34 | 7. I feel like I am diagonally parked in a parallel universe 35 | Write a function that draws a diagonal line on the terminal. 36 | Prototype: void print_diagonal(int n); 37 | You can only use _putchar function to print 38 | Where n is the number of times the character \ should be printed 39 | The diagonal should end with a \n 40 | If n is 0 or less, the function should only print a \n 41 | 8. You are so much sunshine in every square inch 42 | Write a function that prints a square, followed by a new line. 43 | Prototype: void print_square(int size); 44 | You can only use _putchar function to print 45 | Where size is the size of the square 46 | If size is 0 or less, the function should print only a new line 47 | Use the character # to print the square 48 | 9. Fizz-Buzz 49 | The “Fizz-Buzz test” is an interview question designed to help filter out the 99.5% of programming job candidates who can’t seem to program their way out of a wet paper bag. 50 | *Write a program that prints the numbers from 1 to 100, followed by a new line. But for multiples of three print Fizz instead of the number and for the multiples of five print Buzz. For numbers which are multiples of both three and five print FizzBuzz. 51 | Each number or word should be separated by a space 52 | You are allowed to use the standard library 53 | 10. Triangles 54 | Write a function that prints a triangle, followed by a new line. 55 | Prototype: void print_triangle(int size); 56 | You can only use _putchar function to print 57 | Where size is the size of the triangle 58 | If size is 0 or less, the function should print only a new line 59 | Use the character # to print the triangle 60 | 11. The problem of distinguishing prime numbers from composite numbers and of resolving the latter into their prime factors is known to be one of the most important and useful in arithmetic 61 | The prime factors of 1231952 are 2, 2, 2, 2, 37 and 2081. 62 | Write a program that finds and prints the largest prime factor of the number 612852475143, followed by a new line. 63 | You are allowed to use the standard library 64 | Your program will be compiled with this command: gcc -Wall -pedantic -Werror -Wextra -std=gnu89 100-prime_factor.c -o 100-prime_factor -lm 65 | 12. Numbers have life; they're not just symbols on paper 66 | Write a function that prints an integer. 67 | Prototype: void print_number(int n); 68 | You can only use _putchar function to print 69 | You are not allowed to use long 70 | You are not allowed to use arrays or pointers 71 | You are not allowed to hard-code special values 72 | 73 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/_putchar: -------------------------------------------------------------------------------- 1 | #include 3 | 4 | /** 5 | * _putchar - writes the character c to stdout 6 | * @c: The character to print 7 | * 8 | * Return: On success 1. 9 | * On error, -1 is returned, and errno is set appropriately. 10 | */ 11 | int _putchar(char c) 12 | { 13 | return (write(1, &c, 1)); 14 | } 15 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | int _putchar(char c); 4 | int _isupper(int c); 5 | int _isdigit(int c); 6 | int mul(int a, int b); 7 | void print_numbers(void); 8 | void print_most_numbers(void); 9 | void more_numbers(void); 10 | void print_line(int n); 11 | void print_diagonal(int n); 12 | void print_square(int size); 13 | void print_triangle(int size); 14 | void print_number(int n); 15 | #endif 16 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/0-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int n; 12 | 13 | n = 402; 14 | printf("n=%d\n", n); 15 | reset_to_98(&n); 16 | printf("n=%d\n", n); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/0-reset_to_98.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * reset_to_98 - updates the value of n to 98 5 | * @n: integer pointer to a variable to be updated 6 | * 7 | * Return: nothing 8 | */ 9 | 10 | void reset_to_98(int *n) 11 | { 12 | *n = 98; 13 | } 14 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/1-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int a; 12 | int b; 13 | 14 | a = 98; 15 | b = 42; 16 | printf("a=%d, b=%d\n", a, b); 17 | swap_int(&a, &b); 18 | printf("a=%d, b=%d\n", a, b); 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/1-swap.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * swap_int - swaps the values of a and b 5 | * @a: pointer 1 6 | * @b: pointer 2 7 | * Return; 0 8 | */ 9 | 10 | void swap_int(int *a, int *b) 11 | { 12 | *a += *b; 13 | *b = *a - *b; 14 | *a = *a - *b; 15 | } 16 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/100-atoi.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _atoi - convert a string to an integer. 5 | * @s: the string to be comverted. 6 | * 7 | *Return: The integer value of the comverted string. 8 | */ 9 | 10 | int _atoi(char *s) 11 | { 12 | int sign = 1; 13 | unsigned int num = 0; 14 | 15 | do { 16 | 17 | if (*s == '-') 18 | sign *= -1; 19 | 20 | else if (*s >= '0' && *s <= '9') 21 | num = (num * 10) + (*s - '0'); 22 | 23 | else if (num > 0) 24 | break; 25 | } while (*s++); 26 | 27 | return (num * sign); 28 | } 29 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/100-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int nb; 12 | 13 | nb = _atoi("98"); 14 | printf("%d\n", nb); 15 | nb = _atoi("-402"); 16 | printf("%d\n", nb); 17 | nb = _atoi(" ------++++++-----+++++--98"); 18 | printf("%d\n", nb); 19 | nb = _atoi("214748364"); 20 | printf("%d\n", nb); 21 | nb = _atoi("0"); 22 | printf("%d\n", nb); 23 | nb = _atoi("Suite 402"); 24 | printf("%d\n", nb); 25 | nb = _atoi(" + + - -98 Battery Street; San Francisco, CA 94111 - USA "); 26 | printf("%d\n", nb); 27 | nb = _atoi("---++++ -++ Sui - te - 402 #cisfun :)"); 28 | printf("%d\n", nb); 29 | return (0); 30 | } 31 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/101-keygen.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /** 6 | * main - generates random passwords for 101-crackme 7 | * Return: zero 8 | */ 9 | int main(void) 10 | { 11 | int sum; 12 | char c; 13 | 14 | srand(time(NULL)); 15 | while (sum <= 2645) 16 | { 17 | c = rand() % 128; 18 | sum += c; 19 | putchar(c); 20 | } 21 | putchar(2772 - sum); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/2-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char *str; 12 | int len; 13 | 14 | str = "My first strlen!"; 15 | len = _strlen(str); 16 | printf("%d\n", len); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/2-strlen.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strlen - checks the length of a string 5 | * @s: string to check 6 | * 7 | * Return: int length of the string 8 | */ 9 | 10 | int _strlen(char *s) 11 | { 12 | int length = 0; 13 | 14 | while (s[length]) 15 | length++; 16 | return (length); 17 | } 18 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/3-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - check the code 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | char *str; 11 | 12 | str = "I do not fear computers. I fear the lack of them - Isaac Asimov"; 13 | _puts(str); 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/3-puts.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _puts - prints a string and a new line 5 | * @str: pointer to a string to print 6 | * 7 | * Return: 0 8 | */ 9 | 10 | void _puts(char *str) 11 | { 12 | int i = 0; 13 | 14 | while (str[i]) 15 | { 16 | _putchar(str[i]); 17 | i++; 18 | } 19 | _putchar('\n'); 20 | } 21 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/4-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - check the code 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | char *str; 11 | 12 | str = "I do not fear computers. I fear the lack of them - Isaac Asimov"; 13 | print_rev(str); 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/4-print_rev.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_rev - prints a string in reverse 5 | * @s: the used string reference pointer 6 | * Return: 0 7 | */ 8 | 9 | void print_rev(char *s) 10 | { 11 | int i = 0; 12 | 13 | while (s[i]) 14 | i++; 15 | while (i--) 16 | _putchar(s[i]); 17 | _putchar('\n'); 18 | } 19 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/5-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char s[10] = "My School"; 12 | 13 | printf("%s\n", s); 14 | rev_string(s); 15 | printf("%s\n", s); 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/5-rev_string.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * rev_string - prints a string in reverse 5 | * @s: the used string to beb reversed 6 | * Return: 0 7 | */ 8 | 9 | void rev_string(char *s) 10 | { 11 | int len, i, half; 12 | char temp; 13 | 14 | for (len = 0; s[len] != '\0'; len++) 15 | ; 16 | i = 0; 17 | half = len / 2; 18 | 19 | while (half--) 20 | { 21 | temp = s[len - i - 1]; 22 | s[len - i - 1] = s[i]; 23 | s[i] = temp; 24 | i++; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/6-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - check the code 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | char *str; 11 | 12 | str = "0123456789"; 13 | puts2(str); 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/6-puts2.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * puts2 - prints xters of a string 5 | * @str: the used string reference 6 | * Return: 0 7 | */ 8 | 9 | void puts2(char *str) 10 | { 11 | int i = 0; 12 | 13 | while (str[i] != '\0') 14 | { 15 | if (i % 2 == 0) 16 | { 17 | _putchar(str[i]); 18 | } 19 | i++; 20 | } 21 | _putchar('\n'); 22 | } 23 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/7-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - check the code 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | char *str; 11 | 12 | str = "0123456789"; 13 | puts_half(str); 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/7-puts_half.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * puts_half - prints the second half of the string 5 | * @str: the string reference 6 | * Return: 0 7 | */ 8 | 9 | void puts_half(char *str) 10 | { 11 | int i; 12 | 13 | for (i = 0; str[i] != '\0'; i++) 14 | ; 15 | i++; 16 | for (i /= 2; str[i] != '\0'; i++) 17 | { 18 | _putchar(str[i]); 19 | } 20 | _putchar('\n'); 21 | } 22 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/8-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - check the code for 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | int array[5]; 11 | 12 | array[0] = 98; 13 | array[1] = 402; 14 | array[2] = -198; 15 | array[3] = 298; 16 | array[4] = -1024; 17 | print_array(array, 5); 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/8-print_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * print_array - print `n` elements of an array of integers 6 | * @a: int type array pointer 7 | * @n: int type integer 8 | * Description: Numbers must be separated by comma and space. 9 | * Numbers should be displayed in the same order they are stored in array. 10 | * You can only use _putchar to print. 11 | */ 12 | 13 | void print_array(int *a, int n) 14 | { 15 | int i; 16 | 17 | i = 0; 18 | for (n--; n >= 0; n--, i++) 19 | { 20 | printf("%d", a[i]); 21 | if (n > 0) 22 | { 23 | printf(", "); 24 | } 25 | } 26 | printf("\n"); 27 | } 28 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/9-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char s1[98]; 12 | char *ptr; 13 | 14 | ptr = _strcpy(s1, "First, solve the problem. Then, write the code\n"); 15 | printf("%s", s1); 16 | printf("%s", ptr); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/9-strcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * *_strcpy - copies the string pointed to by src 5 | * @dest: char type string 6 | * @src: char type string 7 | * Description: Copy the string pointed to by pointer `src` to 8 | * the buffer pointed to by `dest` 9 | * Return: Pointer to `dest 10 | */ 11 | 12 | char *_strcpy(char *dest, char *src) 13 | { 14 | int i = -1; 15 | 16 | do { 17 | i++; 18 | dest[i] = src[i]; 19 | } while (src[i] != '\0'); 20 | 21 | return (dest); 22 | } 23 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | 0x05. C - Pointers, arrays and strings 2 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/Test/1-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - checks my code 6 | * 7 | * Return: Always 0 8 | */ 9 | 10 | int main(void) 11 | { 12 | int a; 13 | int b; 14 | 15 | a = 98; 16 | b = 42; 17 | printf("a=%d, b=%d\n", a, b); 18 | swap_int(&a, &b); 19 | printf("a=%d, b=%d\n", a, b); 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/Test/main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - check my code 6 | * 7 | * Return: 0 8 | */ 9 | 10 | int main(void) 11 | { 12 | int n; 13 | 14 | n = 402; 15 | printf("n=%d\n",n); 16 | reset_to_98(&n); 17 | printf("n=%d\n",n); 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _putchar - writes the character c to stdout 5 | * @c: The character to print 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | int _putchar(char c) 11 | { 12 | return (write(1, &c, 1)); 13 | } 14 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | int _putchar(char c); 2 | void reset_to_98(int *n); 3 | void swap_int(int *a, int *b); 4 | int _strlen(char *s); 5 | void _puts(char *str); 6 | void print_rev(char *s); 7 | void rev_string(char *s); 8 | void puts2(char *str); 9 | void puts_half(char *str); 10 | void print_array(int *a, int n); 11 | char *_strcpy(char *dest, char *src); 12 | int _atoi(char *s); 13 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/0-strcat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * *_strcat - concatenates @src to @dest 5 | * @src: the source string to append to @dest 6 | * @dest: the destiation string to be concatenated upon 7 | * Return:pointer to the resulting string 8 | */ 9 | 10 | char *_strcat(char *dest, char *src) 11 | { 12 | int index = 0; 13 | int dest_len = 0; 14 | 15 | while (dest[index++]) 16 | dest_len++; 17 | for (index = 0; src[index]; index++) 18 | dest[dest_len++] = src[index]; 19 | return (dest); 20 | } 21 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/1-strncat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strncat - Concatenates two strings using at most 5 | * an inputted number of bytes from src. 6 | * @dest: The string to be appended upon. 7 | * @src: The string to be appended to dest. 8 | * @n: The number of bytes from src to be appended to dest. 9 | * 10 | * Return: A pointer to the resulting string dest. 11 | */ 12 | char *_strncat(char *dest, char *src, int n) 13 | { 14 | int index = 0, dest_len = 0; 15 | 16 | while (dest[index++]) 17 | dest_len++; 18 | for (index = 0; src[index] && index < n; index++) 19 | dest[dest_len++] = src[index]; 20 | return (dest); 21 | } 22 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/100-rot13.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * rot13 - Encodes a string using rot13. 5 | * @str: The string to be encoded. 6 | * 7 | * Return: A pointer to the encoded string. 8 | */ 9 | char *rot13(char *str) 10 | { 11 | int indx1 = 0, indx2; 12 | char alphabet[52] = {'A', 'B', 'C', 'D', 'E', 'F', 13 | 'G', 'H', 'I', 'J', 'K', 'L', 14 | 'M', 'N', 'O', 'P', 'Q', 'R', 15 | 'S', 'T', 'U', 'V', 'W', 'X', 16 | 'Y', 'Z', 'a', 'b', 'c', 'd', 17 | 'e', 'f', 'g', 'h', 'i', 'j', 18 | 'k', 'l', 'm', 'n', 'o', 'p', 19 | 'q', 'r', 's', 't', 'u', 'v', 20 | 'w', 'x', 'y', 'z'}; 21 | char rot13key[52] = {'N', 'O', 'P', 'Q', 'R', 'S', 22 | 'T', 'U', 'V', 'W', 'X', 'Y', 23 | 'Z', 'A', 'B', 'C', 'D', 'E', 24 | 'F', 'G', 'H', 'I', 'J', 'K', 25 | 'L', 'M', 'n', 'o', 'p', 'q', 26 | 'r', 's', 't', 'u', 'v', 'w', 27 | 'x', 'y', 'z', 'a', 'b', 'c', 28 | 'd', 'e', 'f', 'g', 'h', 'i', 29 | 'j', 'k', 'l', 'm'}; 30 | 31 | while (str[indx1]) 32 | { 33 | for (indx2 = 0; indx2 < 52; indx2++) 34 | { 35 | if (str[indx1] == alphabet[indx2]) 36 | { 37 | str[indx1] = rot13key[indx2]; 38 | break; 39 | } 40 | } 41 | 42 | indx1++; 43 | 44 | } 45 | return (str); 46 | } 47 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/101-print_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_number - Prints an integer. 5 | * @n: The integer to be printed. 6 | */ 7 | 8 | void print_number(int n) 9 | { 10 | unsigned int num = n; 11 | 12 | if (n < 0) 13 | { 14 | _putchar('-'); 15 | num = -num; 16 | } 17 | 18 | if ((num / 10) > 0) 19 | print_number(num / 10); 20 | 21 | _putchar((num % 10) + '0'); 22 | } 23 | -------------------------------------------------------------------------------- /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 | char *add_strings(char *n1, char *n2, char *r, int r_index); 4 | char *infinite_add(char *n1, char *n2, char *r, int size_r); 5 | 6 | /** 7 | * add_strings - Adds the numbers stored in two strings. 8 | * @n1: The string containing the first number to be added. 9 | * @n2: The string containing the second number to be added. 10 | * @r: The buffer to store the result. 11 | * @r_index: The current index of the buffer. 12 | * 13 | * Return: If r can store the sum - a pointer to the result. 14 | * If r cannot store the sum - 0. 15 | */ 16 | 17 | char *add_strings(char *n1, char *n2, char *r, int r_index) 18 | { 19 | int num, tens = 0; 20 | 21 | for (; *n1 && *n2; n1--, n2--, r_index--) 22 | { 23 | num = (*n1 - '0') + (*n2 - '0'); 24 | num += tens; 25 | *(r + r_index) = (num % 10) + '0'; 26 | tens = num / 10; 27 | } 28 | 29 | for (; *n1; n1--, r_index--) 30 | { 31 | num = (*n1 - '0') + tens; 32 | *(r + r_index) = (num % 10) + '0'; 33 | tens = num / 10; 34 | } 35 | 36 | for (; *n2; n2--, r_index--) 37 | { 38 | num = (*n2 - '0') + tens; 39 | *(r + r_index) = (num % 10) + '0'; 40 | tens = num / 10; 41 | } 42 | 43 | if (tens && r_index >= 0) 44 | { 45 | *(r + r_index) = (tens % 10) + '0'; 46 | return (r + r_index); 47 | } 48 | 49 | else if (tens && r_index < 0) 50 | return (0); 51 | 52 | return (r + r_index + 1); 53 | } 54 | /** 55 | * infinite_add - Adds two numbers. 56 | * @n1: The first number to be added. 57 | * @n2: The second number to be added. 58 | * @r: The buffer to store the result. 59 | * @size_r: The buffer size. 60 | * 61 | * Return: If r can store the sum - a pointer to the result. 62 | * If r cannot store the sum - 0. 63 | */ 64 | 65 | char *infinite_add(char *n1, char *n2, char *r, int size_r) 66 | { 67 | int index, n1_len = 0, n2_len = 0; 68 | 69 | for (index = 0; *(n1 + index); index++) 70 | n1_len++; 71 | 72 | for (index = 0; *(n2 + index); index++) 73 | n2_len++; 74 | 75 | if (size_r <= n1_len + 1 || size_r <= n2_len + 1) 76 | return (0); 77 | 78 | n1 += n1_len - 1; 79 | n2 += n2_len - 1; 80 | *(r + size_r) = '\0'; 81 | 82 | return (add_strings(n1, n2, r, --size_r)); 83 | } 84 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/104-print_buffer.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * print_buffer - Prints a buffer 10 bytes at a time, starting with 6 | * the byte position, then showing the hex content, 7 | * then displaying printable charcaters. 8 | * @b: The buffer to be printed. 9 | * @size: The number of bytes to be printed from the buffer. 10 | */ 11 | 12 | void print_buffer(char *b, int size) 13 | { 14 | int byte, index; 15 | 16 | for (byte = 0; byte < size; byte += 10) 17 | { 18 | printf("%08x: ", byte); 19 | 20 | for (index = 0; index < 10; index++) 21 | { 22 | if ((index + byte) >= size) 23 | printf(" "); 24 | else 25 | printf("%02x", *(b + index + byte)); 26 | if ((index % 2) != 0 && index != 0) 27 | printf(" "); 28 | } 29 | for (index = 0; index < 10; index++) 30 | { 31 | if ((index + byte) >= size) 32 | break; 33 | else if (*(b + index + byte) >= 31 && 34 | *(b + index + byte) <= 126) 35 | printf("%c", *(b + index + byte)); 36 | else 37 | printf("."); 38 | } 39 | if (byte >= size) 40 | continue; 41 | printf("\n"); 42 | } 43 | if (size <= 0) 44 | printf("\n"); 45 | } 46 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/2-strncpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strncpy - Copies at most an inputted number 5 | * of bytes from string src into dest. 6 | * @dest: The buffer storing the string copy. 7 | * @src: The source string. 8 | * @n: The maximum number of bytes to copied from src. 9 | * 10 | * Return: A pointer to the resulting string dest. 11 | */ 12 | char *_strncpy(char *dest, char *src, int n) 13 | { 14 | int index = 0, src_len = 0; 15 | 16 | while (src[index++]) 17 | src_len++; 18 | 19 | for (index = 0; src[index] && index < n; index++) 20 | dest[index] = src[index]; 21 | 22 | for (index = src_len; index < n; index++) 23 | dest[index] = '\0'; 24 | 25 | return (dest); 26 | } 27 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/3-strcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strcmp - Compares pointers to two strings. 5 | * @s1: A pointer to the first string to be compared. 6 | * @s2: A pointer to the second string to be compared. 7 | * 8 | * Return: If str1 < str2, the negative difference of 9 | * the first unmatched characters. 10 | * If s1 == s2, 0. 11 | * If s1 > s2, the positive difference of 12 | * the first unmatched characters. 13 | */ 14 | int _strcmp(char *s1, char *s2) 15 | { 16 | while ((*s1 && *s2) && (*s1 == *s2)) 17 | { 18 | s1++; 19 | s2++; 20 | } 21 | 22 | return (*s1 - *s2); 23 | } 24 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/4-rev_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * reverse_array - Reverses the content of an array of integers. 5 | * @a: The array of integers to be reversed. 6 | * @n: The number of elements in the array. 7 | */ 8 | 9 | void reverse_array(int *a, int n) 10 | { 11 | int tmp, index; 12 | 13 | for (index = n - 1; index >= n / 2; index--) 14 | { 15 | tmp = a[n - 1 - index]; 16 | a[n - 1 - index] = a[index]; 17 | a[index] = tmp; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/5-string_toupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * string_toupper - Changes all lowercase letters 5 | * of a string to uppercase. 6 | * @str: The string to be changed. 7 | * 8 | * Return: A pointer to the changed string. 9 | */ 10 | char *string_toupper(char *str) 11 | { 12 | int index = 0; 13 | 14 | while (str[index]) 15 | { 16 | if (str[index] >= 'a' && str[index] <= 'z') 17 | str[index] -= 32; 18 | 19 | index++; 20 | } 21 | 22 | return (str); 23 | } 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 | * @str: The string to be capitalized. 6 | * 7 | * Return: A pointer to the changed string. 8 | */ 9 | char *cap_string(char *str) 10 | { 11 | int index = 0; 12 | 13 | while (str[index]) 14 | { 15 | while (!(str[index] >= 'a' && str[index] <= 'z')) 16 | index++; 17 | 18 | if (str[index - 1] == ' ' || 19 | str[index - 1] == '\t' || 20 | str[index - 1] == '\n' || 21 | str[index - 1] == ',' || 22 | str[index - 1] == ';' || 23 | str[index - 1] == '.' || 24 | str[index - 1] == '!' || 25 | str[index - 1] == '?' || 26 | str[index - 1] == '"' || 27 | str[index - 1] == '(' || 28 | str[index - 1] == ')' || 29 | str[index - 1] == '{' || 30 | str[index - 1] == '}' || 31 | index == 0) 32 | str[index] -= 32; 33 | 34 | index++; 35 | } 36 | 37 | return (str); 38 | } 39 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/7-leet.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * leet - Encodes a string to 1337. 5 | * @str: The string to be encoded. 6 | * 7 | * Return: A pointer to the encoded string. 8 | */ 9 | char *leet(char *str) 10 | { 11 | int indx1 = 0, indx2; 12 | char leet[8] = {'O', 'L', '?', 'E', 'A', '?', '?', 'T'}; 13 | 14 | while (str[indx1]) 15 | { 16 | for (indx2 = 0; indx2 <= 7; indx2++) 17 | { 18 | if (str[indx1] == leet[indx2] || 19 | str[indx1] - 32 == leet[indx2]) 20 | str[indx1] = indx2 + '0'; 21 | } 22 | 23 | indx1++; 24 | } 25 | 26 | return (str); 27 | } 28 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/New-test/0-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char s1[98] = "Hello "; 12 | char s2[] = "World!\n"; 13 | char *ptr; 14 | 15 | printf("%s\n", s1); 16 | printf("%s", s2); 17 | ptr = _strcat(s1, s2); 18 | printf("%s", s1); 19 | printf("%s", s2); 20 | printf("%s", ptr); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/New-test/1-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char s1[98] = "Hello "; 12 | char s2[] = "World!\n"; 13 | char *ptr; 14 | 15 | printf("%s\n", s1); 16 | printf("%s", s2); 17 | ptr = _strncat(s1, s2, 1); 18 | printf("%s\n", s1); 19 | printf("%s", s2); 20 | printf("%s\n", ptr); 21 | ptr = _strncat(s1, s2, 1024); 22 | printf("%s", s1); 23 | printf("%s", s2); 24 | printf("%s", ptr); 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/New-test/100-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char s[] = "ROT13 (\"rotate by 13 places\", sometimes hyphenated ROT-13) is a simple letter substitution cipher.\n"; 12 | char *p; 13 | 14 | p = rot13(s); 15 | printf("%s", p); 16 | printf("------------------------------------\n"); 17 | printf("%s", s); 18 | printf("------------------------------------\n"); 19 | p = rot13(s); 20 | printf("%s", p); 21 | printf("------------------------------------\n"); 22 | printf("%s", s); 23 | printf("------------------------------------\n"); 24 | p = rot13(s); 25 | printf("%s", p); 26 | printf("------------------------------------\n"); 27 | printf("%s", s); 28 | return (0); 29 | } 30 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/New-test/101-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - check the code 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | print_number(98); 11 | _putchar('\n'); 12 | print_number(402); 13 | _putchar('\n'); 14 | print_number(1024); 15 | _putchar('\n'); 16 | print_number(0); 17 | _putchar('\n'); 18 | print_number(-98); 19 | _putchar('\n'); 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/New-test/103-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char *n = "1234567892434574367823574575678477685785645685876876774586734734563456453743756756784458"; 12 | char *m = "9034790663470697234682914569346259634958693246597324659762347956349265983465962349569346"; 13 | char r[100]; 14 | char r2[10]; 15 | char r3[11]; 16 | char *res; 17 | 18 | res = infinite_add(n, m, r, 100); 19 | if (res == 0) 20 | { 21 | printf("Error\n"); 22 | } 23 | else 24 | { 25 | printf("%s + %s = %s\n", n, m, res); 26 | } 27 | n = "1234567890"; 28 | m = "1"; 29 | res = infinite_add(n, m, r2, 10); 30 | if (res == 0) 31 | { 32 | printf("Error\n"); 33 | } 34 | else 35 | { 36 | printf("%s + %s = %s\n", n, m, res); 37 | } 38 | n = "999999999"; 39 | m = "1"; 40 | res = infinite_add(n, m, r2, 10); 41 | if (res == 0) 42 | { 43 | printf("Error\n"); 44 | } 45 | else 46 | { 47 | printf("%s + %s = %s\n", n, m, res); 48 | } 49 | res = infinite_add(n, m, r3, 11); 50 | if (res == 0) 51 | { 52 | printf("Error\n"); 53 | } 54 | else 55 | { 56 | printf("%s + %s = %s\n", n, m, res); 57 | } 58 | return (0); 59 | } 60 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/New-test/104-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char buffer[] = "This is a string!\0And this is the rest of the #buffer :)\1\2\3\4\5\6\7#cisfun\n\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\x20\x21\x34\x56#pointersarefun #infernumisfun\n"; 12 | 13 | printf("%s\n", buffer); 14 | printf("---------------------------------\n"); 15 | print_buffer(buffer, sizeof(buffer)); 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/New-test/2-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char s1[98]; 12 | char *ptr; 13 | int i; 14 | 15 | for (i = 0; i < 98 - 1; i++) 16 | { 17 | s1[i] = '*'; 18 | } 19 | s1[i] = '\0'; 20 | printf("%s\n", s1); 21 | ptr = _strncpy(s1, "First, solve the problem. Then, write the code\n", 5); 22 | printf("%s\n", s1); 23 | printf("%s\n", ptr); 24 | ptr = _strncpy(s1, "First, solve the problem. Then, write the code\n", 90); 25 | printf("%s", s1); 26 | printf("%s", ptr); 27 | for (i = 0; i < 98; i++) 28 | { 29 | if (i % 10) 30 | { 31 | printf(" "); 32 | } 33 | if (!(i % 10) && i) 34 | { 35 | printf("\n"); 36 | } 37 | printf("0x%02x", s1[i]); 38 | } 39 | printf("\n"); 40 | return (0); 41 | } 42 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/New-test/3-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char s1[] = "Hello"; 12 | char s2[] = "World!"; 13 | 14 | printf("%d\n", _strcmp(s1, s2)); 15 | printf("%d\n", _strcmp(s2, s1)); 16 | printf("%d\n", _strcmp(s1, s1)); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/New-test/4-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - check the code 6 | * @a: an array of integers 7 | * @n: the number of elements to swap 8 | * 9 | * Return: nothing. 10 | */ 11 | void print_array(int *a, int n) 12 | { 13 | int i; 14 | 15 | i = 0; 16 | while (i < n) 17 | { 18 | if (i != 0) 19 | { 20 | printf(", "); 21 | } 22 | printf("%d", a[i]); 23 | i++; 24 | } 25 | printf("\n"); 26 | } 27 | 28 | /** 29 | * main - check the code 30 | * 31 | * Return: Always 0. 32 | */ 33 | int main(void) 34 | { 35 | int a[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 98, 1024, 1337}; 36 | 37 | print_array(a, sizeof(a) / sizeof(int)); 38 | reverse_array(a, sizeof(a) / sizeof(int)); 39 | print_array(a, sizeof(a) / sizeof(int)); 40 | return (0); 41 | } 42 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/New-test/5-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char str[] = "Look up!\n"; 12 | char *ptr; 13 | 14 | ptr = string_toupper(str); 15 | printf("%s", ptr); 16 | printf("%s", str); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/New-test/6-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char str[] = "Expect the best. Prepare for the worst. Capitalize on what comes.\nhello world! hello-world 0123456hello world\thello world.hello world\n"; 12 | char *ptr; 13 | 14 | ptr = cap_string(str); 15 | printf("%s", ptr); 16 | printf("%s", str); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/New-test/7-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char s[] = "Expect the best. Prepare for the worst. Capitalize on what comes.\n"; 12 | char *p; 13 | 14 | p = leet(s); 15 | printf("%s", p); 16 | printf("%s", s); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | More Pointers, Arrays and Strings 2 | 3 | 4 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _putchar - writes the character c to stdout 5 | * @c: The character to print 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | int _putchar(char c) 11 | { 12 | return (write(1, &c, 1)); 13 | } 14 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | int _putchar(char c); 2 | char *_strcat(char *dest, char *src); 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 *); 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 | int main(void); 15 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/0-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * simple_print_buffer - prints buffer in hexa 6 | * @buffer: the address of memory to print 7 | * @size: the size of the memory to print 8 | * 9 | * Return: Nothing. 10 | */ 11 | void simple_print_buffer(char *buffer, unsigned int size) 12 | { 13 | unsigned int i; 14 | 15 | i = 0; 16 | while (i < size) 17 | { 18 | if (i % 10) 19 | { 20 | printf(" "); 21 | } 22 | if (!(i % 10) && i) 23 | { 24 | printf("\n"); 25 | } 26 | printf("0x%02x", buffer[i]); 27 | i++; 28 | } 29 | printf("\n"); 30 | } 31 | 32 | /** 33 | * main - check the code 34 | * 35 | * Return: Always 0. 36 | */ 37 | int main(void) 38 | { 39 | char buffer[98] = {0x00}; 40 | 41 | simple_print_buffer(buffer, 98); 42 | _memset(buffer, 0x01, 95); 43 | printf("-------------------------------------------------\n"); 44 | simple_print_buffer(buffer, 98); 45 | return (0); 46 | } 47 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/0-memset.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _memset - fill a block of memory with a specific value 4 | * @s: starting address of memory to be filled 5 | * @b: the desired value 6 | * @n: number of bytes to be changed 7 | * 8 | * Return: changed array with new value for n bytes 9 | */ 10 | char *_memset(char *s, char b, unsigned int n) 11 | { 12 | int i = 0; 13 | 14 | for (; n > 0; i++) 15 | { 16 | s[i] = b; 17 | n--; 18 | } 19 | return (s); 20 | } 21 | 22 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/1-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * simple_print_buffer - prints buffer in hexa 6 | * @buffer: the address of memory to print 7 | * @size: the size of the memory to print 8 | * 9 | * Return: Nothing. 10 | */ 11 | void simple_print_buffer(char *buffer, unsigned int size) 12 | { 13 | unsigned int i; 14 | 15 | i = 0; 16 | while (i < size) 17 | { 18 | if (i % 10) 19 | { 20 | printf(" "); 21 | } 22 | if (!(i % 10) && i) 23 | { 24 | printf("\n"); 25 | } 26 | printf("0x%02x", buffer[i]); 27 | i++; 28 | } 29 | printf("\n"); 30 | } 31 | 32 | /** 33 | * main - check the code 34 | * 35 | * Return: Always 0. 36 | */ 37 | int main(void) 38 | { 39 | char buffer[98] = {0}; 40 | char buffer2[98] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}; 41 | 42 | simple_print_buffer(buffer, 98); 43 | _memcpy(buffer + 50, buffer2, 10); 44 | printf("-------------------------------------------------\n"); 45 | simple_print_buffer(buffer, 98); 46 | return (0); 47 | } 48 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/1-memcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_memcpy - a function that copies memory area 4 | *@dest: memory where is stored 5 | *@src: memory where is copied 6 | *@n: number of bytes 7 | * 8 | *Return: copied memory with n byted changed 9 | */ 10 | char *_memcpy(char *dest, char *src, unsigned int n) 11 | { 12 | int r = 0; 13 | int i = n; 14 | 15 | for (; r < i; r++) 16 | { 17 | dest[r] = src[r]; 18 | n--; 19 | } 20 | return (dest); 21 | } 22 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/100-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char *s0 = "Bob Dylan"; 12 | char *s1 = "Robert Allen"; 13 | 14 | printf("%s, %s\n", s0, s1); 15 | set_string(&s1, s0); 16 | printf("%s, %s\n", s0, s1); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/100-set_string.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * set_string - sets the value of a pointer to a char 5 | * @s: pointer to pointer 6 | * @to: pointer char 7 | */ 8 | 9 | void set_string(char **s, char *to) 10 | { 11 | *s = to; 12 | } 13 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/101-crackme_password: -------------------------------------------------------------------------------- 1 | abc123 -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/2-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char *s = "hello"; 12 | char *f; 13 | 14 | f = _strchr(s, 'l'); 15 | 16 | if (f != NULL) 17 | { 18 | printf("%s\n", f); 19 | } 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/2-strchr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strchr - Entry point 4 | * @s: input 5 | * @c: input 6 | * Return: Always 0 (Success) 7 | */ 8 | char *_strchr(char *s, char c) 9 | { 10 | int i = 0; 11 | 12 | for (; s[i] >= '\0'; i++) 13 | { 14 | if (s[i] == c) 15 | return (&s[i]); 16 | } 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/3-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char *s = "hello, world"; 12 | char *f = "oleh"; 13 | unsigned int n; 14 | 15 | n = _strspn(s, f); 16 | printf("%u\n", n); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/3-strspn.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strspn - Entry point 4 | * @s: input 5 | * @accept: input 6 | * Return: Always 0 (Success) 7 | */ 8 | unsigned int _strspn(char *s, char *accept) 9 | { 10 | unsigned int n = 0; 11 | int r; 12 | 13 | while (*s) 14 | { 15 | for (r = 0; accept[r]; r++) 16 | { 17 | if (*s == accept[r]) 18 | { 19 | n++; 20 | break; 21 | } 22 | else if (accept[r + 1] == '\0') 23 | return (n); 24 | } 25 | s++; 26 | } 27 | return (n); 28 | } 29 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/4-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char *s = "hello, world"; 12 | char *f = "world"; 13 | char *t; 14 | 15 | t = _strpbrk(s, f); 16 | printf("%s\n", t); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/4-strpbrk.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strpbrk - Entry point 4 | * @s: input 5 | * @accept: input 6 | * Return: Always 0 (Success) 7 | */ 8 | char *_strpbrk(char *s, char *accept) 9 | { 10 | int k; 11 | 12 | while (*s) 13 | { 14 | for (k = 0; accept[k]; k++) 15 | { 16 | if (*s == accept[k]) 17 | return (s); 18 | } 19 | s++; 20 | } 21 | 22 | return ('\0'); 23 | } 24 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/5-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char *s = "hello, world"; 12 | char *f = "world"; 13 | char *t; 14 | 15 | t = _strstr(s, f); 16 | printf("%s\n", t); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/5-strstr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strstr - Entry point 4 | * @haystack: input 5 | * @needle: input 6 | * Return: Always 0 (Success) 7 | */ 8 | char *_strstr(char *haystack, char *needle) 9 | { 10 | for (; *haystack != '\0'; haystack++) 11 | { 12 | char *l = haystack; 13 | char *p = needle; 14 | 15 | while (*l == *p && *p != '\0') 16 | { 17 | l++; 18 | p++; 19 | } 20 | 21 | if (*p == '\0') 22 | return (haystack); 23 | } 24 | 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/7-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char board[8][8] = { 12 | {'r', 'k', 'b', 'q', 'k', 'b', 'k', 'r'}, 13 | {'p', 'p', 'p', 'p', 'p', 'p', 'p', 'p'}, 14 | {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}, 15 | {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}, 16 | {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}, 17 | {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}, 18 | {'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P'}, 19 | {'R', 'K', 'B', 'Q', 'K', 'B', 'K', 'R'}, 20 | }; 21 | print_chessboard(board); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/7-print_chessboard.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * print_chessboard - Entry point 4 | * @a: array 5 | * Return: Always 0 (Success) 6 | */ 7 | void print_chessboard(char (*a)[8]) 8 | { 9 | int i; 10 | int j; 11 | 12 | for (i = 0; i < 8; i++) 13 | { 14 | for (j = 0; j < 8; j++) 15 | _putchar(a[i][j]); 16 | _putchar('\n'); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/8-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int c3[3][3] = { 12 | {0, 1, 5}, 13 | {10, 11, 12}, 14 | {1000, 101, 102}, 15 | }; 16 | int c5[5][5] = { 17 | {0, 1, 5, 12124, 1234}, 18 | {10, 11, 12, 123521, 12512}, 19 | {1000, 101, 102, 12545, 214543435}, 20 | {100, 1012451, 11102, 12545, 214543435}, 21 | {10, 12401, 10452, 11542545, 1214543435}, 22 | }; 23 | print_diagsums((int *)c3, 3); 24 | print_diagsums((int *)c5, 5); 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/8-print_diagsums.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * print_diagsums - Entry point 5 | * @a: input 6 | * @size: input 7 | * Return: Always 0 (Success) 8 | */ 9 | void print_diagsums(int *a, int size) 10 | { 11 | int sum1, sum2, y; 12 | 13 | sum1 = 0; 14 | sum2 = 0; 15 | 16 | for (y = 0; y < size; y++) 17 | { 18 | sum1 = sum1 + a[y * size + y]; 19 | } 20 | 21 | for (y = size - 1; y >= 0; y--) 22 | { 23 | sum2 += a[y * size + (size - y - 1)]; 24 | } 25 | 26 | printf("%d, %d\n", sum1, sum2); 27 | } 28 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | #C - Even more pointers, arrays and strings 2 | 3 | #TASKS. 4 | 5 | 0. memset 6 | 7 | Write a function that fills memory with a constant byte. 8 | 9 | Prototype: char *_memset(char *s, char b, unsigned int n); 10 | The _memset() function fills the first n bytes of the memory area pointed to by s with the constant byte b 11 | Returns a pointer to the memory area s 12 | 13 | 1. memcpy 14 | 15 | Write a function that copies memory area. 16 | 17 | Prototype: char *_memcpy(char *dest, char *src, unsigned int n); 18 | The _memcpy() function copies n bytes from memory area src to memory area dest 19 | Returns a pointer to dest 20 | FYI: The standard library provides a similar function: memcpy. Run man memcpy to learn more. 21 | 22 | 2. strchr 23 | 24 | Write a function that locates a character in a string. 25 | 26 | Prototype: char *_strchr(char *s, char c); 27 | Returns a pointer to the first occurrence of the character c in the string s, or NULL if the character is not found 28 | FYI: The standard library provides a similar function: strchr. Run man strchr to learn more. 29 | 30 | 3. strspn 31 | 32 | Write a function that gets the length of a prefix substring. 33 | 34 | Prototype: unsigned int _strspn(char *s, char *accept); 35 | Returns the number of bytes in the initial segment of s which consist only of bytes from accept 36 | FYI: The standard library provides a similar function: strspn. Run man strspn to learn more. 37 | 38 | 4. strpbrk 39 | 40 | Write a function that searches a string for any of a set of bytes. 41 | 42 | Prototype: char *_strpbrk(char *s, char *accept); 43 | The _strpbrk() function locates the first occurrence in the string s of any of the bytes in the string accept 44 | Returns a pointer to the byte in s that matches one of the bytes in accept, or NULL if no such byte is found 45 | FYI: The standard library provides a similar function: strpbrk. Run man strpbrk to learn more. 46 | 47 | 5. strstr 48 | 49 | Write a function that locates a substring. 50 | 51 | Prototype: char *_strstr(char *haystack, char *needle); 52 | The _strstr() function finds the first occurrence of the substring needle in the string haystack. The terminating null bytes (\0) are not compared 53 | Returns a pointer to the beginning of the located substring, or NULL if the substring is not found. 54 | FYI: The standard library provides a similar function: strstr. Run man strstr to learn more. 55 | 56 | 57 | 6. Chess is mental torture 58 | 59 | Write a function that prints the chessboard. 60 | 61 | Prototype: void print_chessboard(char (*a)[8]); 62 | 63 | 7. The line of life is a ragged diagonal between duty and desire 64 | 65 | Write a function that prints the sum of the two diagonals of a square matrix of integers. 66 | 67 | Prototype: void print_diagsums(int *a, int size); 68 | Format: see example 69 | You are allowed to use the standard library 70 | Note that in the following example we are casting an int[][] into an int*. This is not something you should do. The goal here is to make sure you understand how an array of array is stored in memory. 71 | 72 | 8. Double pointer, double fun 73 | 74 | Write a function that sets the value of a pointer to a char. 75 | 76 | Prototype: void set_string(char **s, char *to); 77 | 78 | 9. My primary goal of hacking was the intellectual curiosity, the seduction of a 79 | 80 | Create a file that contains the password for the crackme2 executable. 81 | 82 | Your file should contain the exact password, no new line, no extra space 83 | ltrace, ldd, gdb and objdump can help 84 | You may need to install the openssl library to run the crakme2 program: sudo apt install libssl-dev 85 | Edit the source list sudo nano /etc/apt/sources.list to add the following line: deb http://security.ubuntu.com/ubuntu xenial-security main Then sudo apt update and sudo apt install libssl1.0.0 86 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/_putchar.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _putchar - writes the character c to stdout 6 | * @c: The character to print 7 | * 8 | * Return: On success 1. 9 | * On error, -1 is returned, and errno is set appropriately. 10 | */ 11 | int _putchar(char c) 12 | { 13 | return (write(1, &c, 1)); 14 | } 15 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | char *_memset(char *s, char b, unsigned int n); 5 | int _putchar(char c); 6 | char *_memcpy(char *dest, char *src, unsigned int n); 7 | char *_strchr(char *s, char c); 8 | unsigned int _strspn(char *s, char *accept); 9 | char *_strpbrk(char *s, char *accept); 10 | char *_strstr(char *haystack, char *needle); 11 | void print_chessboard(char (*a)[8]); 12 | void print_diagsums(int *a, int size); 13 | void set_string(char **s, char *to); 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /0x08-recursion/0-puts_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _puts_recursion - function like puts(); 4 | * @s: input 5 | * Return: Always 0 (Success) 6 | */ 7 | void _puts_recursion(char *s) 8 | { 9 | if (*s) 10 | { 11 | _putchar(*s); 12 | _puts_recursion(s + 1); 13 | } 14 | 15 | else 16 | _putchar('\n'); 17 | } 18 | -------------------------------------------------------------------------------- /0x08-recursion/1-print_rev_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _print_rev_recursion - Prints a string in reverse. 4 | * @s: The string to be printed. 5 | */ 6 | void _print_rev_recursion(char *s) 7 | { 8 | if (*s) 9 | { 10 | _print_rev_recursion(s + 1); 11 | _putchar(*s); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /0x08-recursion/100-is_palindrome.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | int check_pal(char *s, int i, int len); 4 | int _strlen_recursion(char *s); 5 | 6 | /** 7 | * is_palindrome - checks if a string is a palindrome 8 | * @s: string to reverse 9 | * 10 | * Return: 1 if it is, 0 it's not 11 | */ 12 | int is_palindrome(char *s) 13 | { 14 | if (*s == 0) 15 | return (1); 16 | return (check_pal(s, 0, _strlen_recursion(s))); 17 | } 18 | 19 | /** 20 | * _strlen_recursion - returns the length of a string 21 | * @s: string to calculate the length of 22 | * 23 | * Return: length of the string 24 | */ 25 | int _strlen_recursion(char *s) 26 | { 27 | if (*s == '\0') 28 | return (0); 29 | return (1 + _strlen_recursion(s + 1)); 30 | } 31 | 32 | /** 33 | * check_pal - checks the characters recursively for palindrome 34 | * @s: string to check 35 | * @i: iterator 36 | * @len: length of the string 37 | * 38 | * Return: 1 if palindrome, 0 if not 39 | */ 40 | int check_pal(char *s, int i, int len) 41 | { 42 | if (*(s + i) != *(s + len - 1)) 43 | return (0); 44 | if (i >= len) 45 | return (1); 46 | return (check_pal(s, i + 1, len - 1)); 47 | } 48 | -------------------------------------------------------------------------------- /0x08-recursion/101-wildcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * wildcmp - Compare strings 5 | * @s1: pointer to string params 6 | * @s2: pointer to string params 7 | * Return: 0 8 | */ 9 | 10 | int wildcmp(char *s1, char *s2) 11 | { 12 | if (*s1 == '\0') 13 | { 14 | if (*s2 != '\0' && *s2 == '*') 15 | { 16 | return (wildcmp(s1, s2 + 1)); 17 | } 18 | return (*s2 == '\0'); 19 | } 20 | 21 | if (*s2 == '*') 22 | { 23 | return (wildcmp(s1 + 1, s2) || wildcmp(s1, s2 + 1)); 24 | } 25 | else if (*s1 == *s2) 26 | { 27 | return (wildcmp(s1 + 1, s2 + 1)); 28 | } 29 | return (0); 30 | } 31 | -------------------------------------------------------------------------------- /0x08-recursion/2-strlen_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strlen_recursion - Returns the length of a string. 4 | * @s: The string to be measured. 5 | * 6 | * Return: The length of the string. 7 | */ 8 | int _strlen_recursion(char *s) 9 | { 10 | int longit = 0; 11 | 12 | if (*s) 13 | { 14 | longit++; 15 | longit += _strlen_recursion(s + 1); 16 | } 17 | 18 | return (longit); 19 | } 20 | -------------------------------------------------------------------------------- /0x08-recursion/3-factorial.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * factorial - returns the factorial of a number 5 | * @n: number to return the factorial from 6 | * 7 | * Return: factorial of n 8 | */ 9 | int factorial(int n) 10 | { 11 | if (n < 0) 12 | return (-1); 13 | if (n == 0) 14 | return (1); 15 | return (n * factorial(n - 1)); 16 | } 17 | -------------------------------------------------------------------------------- /0x08-recursion/4-pow_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _pow_recursion - returns the value of x raised to the power of y 5 | * @x: value to raise 6 | * @y: power 7 | * 8 | * Return: result of the power 9 | */ 10 | int _pow_recursion(int x, int y) 11 | { 12 | if (y < 0) 13 | return (-1); 14 | if (y == 0) 15 | return (1); 16 | return (x * _pow_recursion(x, y - 1)); 17 | } 18 | -------------------------------------------------------------------------------- /0x08-recursion/5-sqrt_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | int actual_sqrt_recursion(int n, int i); 4 | 5 | /** 6 | * _sqrt_recursion - returns the natural square root of a number 7 | * @n: number to calculate the square root of 8 | * 9 | * Return: the resulting square root 10 | */ 11 | int _sqrt_recursion(int n) 12 | { 13 | if (n < 0) 14 | return (-1); 15 | return (actual_sqrt_recursion(n, 0)); 16 | } 17 | 18 | /** 19 | * actual_sqrt_recursion - recurses to find the natural 20 | * square root of a number 21 | * @n: number to calculate the sqaure root of 22 | * @i: iterator 23 | * 24 | * Return: the resulting square root 25 | */ 26 | int actual_sqrt_recursion(int n, int i) 27 | { 28 | if (i * i > n) 29 | return (-1); 30 | if (i * i == n) 31 | return (i); 32 | return (actual_sqrt_recursion(n, i + 1)); 33 | } 34 | -------------------------------------------------------------------------------- /0x08-recursion/6-is_prime_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | int actual_prime(int n, int i); 4 | 5 | /** 6 | * is_prime_number - says if an integer is a prime number or not 7 | * @n: number to evaluate 8 | * 9 | * Return: 1 if n is a prime number, 0 if not 10 | */ 11 | int is_prime_number(int n) 12 | { 13 | if (n <= 1) 14 | return (0); 15 | return (actual_prime(n, n - 1)); 16 | } 17 | 18 | /** 19 | * actual_prime - calculates if a number is prime recursively 20 | * @n: number to evaluate 21 | * @i: iterator 22 | * 23 | * Return: 1 if n is prime, 0 if not 24 | */ 25 | int actual_prime(int n, int i) 26 | { 27 | if (i == 1) 28 | return (1); 29 | if (n % i == 0 && i > 0) 30 | return (0); 31 | return (actual_prime(n, i - 1)); 32 | } 33 | -------------------------------------------------------------------------------- /0x08-recursion/README.md: -------------------------------------------------------------------------------- 1 | all tasks 0x08. C - Recursion 2 | -------------------------------------------------------------------------------- /0x08-recursion/_putchar.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _putchar - writes the character c to stdout 6 | * @c: The character to print 7 | * 8 | * Return: On success 1. 9 | * On error, -1 is returned, and errno is set appropriately. 10 | */ 11 | int _putchar(char c) 12 | { 13 | return (write(1, &c, 1)); 14 | } 15 | -------------------------------------------------------------------------------- /0x08-recursion/main-all/.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mostafa101296/alx-low_level_programming/31145c309aa2ee439037a77e33b6ef9e6dc2c99f/0x08-recursion/main-all/.swp -------------------------------------------------------------------------------- /0x08-recursion/main-all/0-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - check the code 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | _puts_recursion("Puts with recursion"); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x08-recursion/main-all/1-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - check the code 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | _print_rev_recursion("\nColton Walker"); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x08-recursion/main-all/100-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int r; 12 | 13 | r = is_palindrome("level"); 14 | printf("%d\n", r); 15 | r = is_palindrome("redder"); 16 | printf("%d\n", r); 17 | r = is_palindrome("test"); 18 | printf("%d\n", r); 19 | r = is_palindrome("step on no pets"); 20 | printf("%d\n", r); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x08-recursion/main-all/101-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int r; 12 | 13 | r = wildcmp("main.c", "*.c"); 14 | printf("%d\n", r); 15 | r = wildcmp("main.c", "m*a*i*n*.*c*"); 16 | printf("%d\n", r); 17 | r = wildcmp("main.c", "main.c"); 18 | printf("%d\n", r); 19 | r = wildcmp("main.c", "m*c"); 20 | printf("%d\n", r); 21 | r = wildcmp("main.c", "ma********************************c"); 22 | printf("%d\n", r); 23 | r = wildcmp("main.c", "*"); 24 | printf("%d\n", r); 25 | r = wildcmp("main.c", "***"); 26 | printf("%d\n", r); 27 | r = wildcmp("main.c", "m.*c"); 28 | printf("%d\n", r); 29 | r = wildcmp("main.c", "**.*c"); 30 | printf("%d\n", r); 31 | r = wildcmp("main-main.c", "ma*in.c"); 32 | printf("%d\n", r); 33 | r = wildcmp("main", "main*d"); 34 | printf("%d\n", r); 35 | r = wildcmp("abc", "*b"); 36 | printf("%d\n", r); 37 | return (0); 38 | } 39 | -------------------------------------------------------------------------------- /0x08-recursion/main-all/2-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int n; 12 | 13 | n = _strlen_recursion("Corbin Coleman"); 14 | printf("%d\n", n); 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x08-recursion/main-all/3-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int r; 12 | 13 | r = factorial(1); 14 | printf("%d\n", r); 15 | r = factorial(5); 16 | printf("%d\n", r); 17 | r = factorial(10); 18 | printf("%d\n", r); 19 | r = factorial(-1024); 20 | printf("%d\n", r); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x08-recursion/main-all/4-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int r; 12 | 13 | r = _pow_recursion(1, 10); 14 | printf("%d\n", r); 15 | r = _pow_recursion(1024, 0); 16 | printf("%d\n", r); 17 | r = _pow_recursion(2, 16); 18 | printf("%d\n", r); 19 | r = _pow_recursion(5, 2); 20 | printf("%d\n", r); 21 | r = _pow_recursion(5, -2); 22 | printf("%d\n", r); 23 | r = _pow_recursion(-5, 3); 24 | printf("%d\n", r); 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /0x08-recursion/main-all/5-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int r; 12 | 13 | r = _sqrt_recursion(1); 14 | printf("%d\n", r); 15 | r = _sqrt_recursion(1024); 16 | printf("%d\n", r); 17 | r = _sqrt_recursion(16); 18 | printf("%d\n", r); 19 | r = _sqrt_recursion(17); 20 | printf("%d\n", r); 21 | r = _sqrt_recursion(25); 22 | printf("%d\n", r); 23 | r = _sqrt_recursion(-1); 24 | printf("%d\n", r); 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /0x08-recursion/main-all/6-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int r; 12 | 13 | r = is_prime_number(1); 14 | printf("%d\n", r); 15 | r = is_prime_number(1024); 16 | printf("%d\n", r); 17 | r = is_prime_number(16); 18 | printf("%d\n", r); 19 | r = is_prime_number(17); 20 | printf("%d\n", r); 21 | r = is_prime_number(25); 22 | printf("%d\n", r); 23 | r = is_prime_number(-1); 24 | printf("%d\n", r); 25 | r = is_prime_number(113); 26 | printf("%d\n", r); 27 | r = is_prime_number(7919); 28 | printf("%d\n", r); 29 | return (0); 30 | } 31 | -------------------------------------------------------------------------------- /0x08-recursion/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | /** 5 | * File: holberton.h 6 | * Desc: Header file containing prototypes for all functions 7 | * used in the 0x07-recursion directory. 8 | */ 9 | 10 | int _putchar(char c); 11 | void _puts_recursion(char *s); 12 | void _print_rev_recursion(char *s); 13 | int _strlen_recursion(char *s); 14 | int factorial(int n); 15 | int _pow_recursion(int x, int y); 16 | int _sqrt_recursion(int n); 17 | int is_prime_number(int n); 18 | int is_palindrome(char *s); 19 | int wildcmp(char *s1, char *s2); 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /0x09-static_libraries/0-isupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | int _isupper(int c) 8 | { 9 | return (c >= 'A' && c <= 'Z'); 10 | } 11 | -------------------------------------------------------------------------------- /0x09-static_libraries/0-isupper.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mostafa101296/alx-low_level_programming/31145c309aa2ee439037a77e33b6ef9e6dc2c99f/0x09-static_libraries/0-isupper.o -------------------------------------------------------------------------------- /0x09-static_libraries/0-memset.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | char *_memset(char *s, char b, unsigned int n) 8 | { 9 | return memset(s, b, n); 10 | } 11 | -------------------------------------------------------------------------------- /0x09-static_libraries/0-memset.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mostafa101296/alx-low_level_programming/31145c309aa2ee439037a77e33b6ef9e6dc2c99f/0x09-static_libraries/0-memset.o -------------------------------------------------------------------------------- /0x09-static_libraries/0-strcat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | char *_strcat(char *dest, char *src) 8 | { 9 | return strcat(dest, src); 10 | } 11 | -------------------------------------------------------------------------------- /0x09-static_libraries/0-strcat.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mostafa101296/alx-low_level_programming/31145c309aa2ee439037a77e33b6ef9e6dc2c99f/0x09-static_libraries/0-strcat.o -------------------------------------------------------------------------------- /0x09-static_libraries/1-isdigit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | int _isdigit(int c) 8 | { 9 | return isdigit(c); 10 | } 11 | -------------------------------------------------------------------------------- /0x09-static_libraries/1-isdigit.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mostafa101296/alx-low_level_programming/31145c309aa2ee439037a77e33b6ef9e6dc2c99f/0x09-static_libraries/1-isdigit.o -------------------------------------------------------------------------------- /0x09-static_libraries/1-memcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | char *_memcpy(char *dest, char *src, unsigned int n) 8 | { 9 | return memcpy(dest, src, n); 10 | } 11 | 12 | -------------------------------------------------------------------------------- /0x09-static_libraries/1-memcpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mostafa101296/alx-low_level_programming/31145c309aa2ee439037a77e33b6ef9e6dc2c99f/0x09-static_libraries/1-memcpy.o -------------------------------------------------------------------------------- /0x09-static_libraries/1-strncat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | char *_strncat(char *dest, char *src, int n) 8 | { 9 | return strncat(dest, src, n); 10 | } 11 | -------------------------------------------------------------------------------- /0x09-static_libraries/1-strncat.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mostafa101296/alx-low_level_programming/31145c309aa2ee439037a77e33b6ef9e6dc2c99f/0x09-static_libraries/1-strncat.o -------------------------------------------------------------------------------- /0x09-static_libraries/100-atoi.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | int _atoi(char *s) 7 | 8 | { 9 | return atoi(s); 10 | } 11 | -------------------------------------------------------------------------------- /0x09-static_libraries/100-atoi.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mostafa101296/alx-low_level_programming/31145c309aa2ee439037a77e33b6ef9e6dc2c99f/0x09-static_libraries/100-atoi.o -------------------------------------------------------------------------------- /0x09-static_libraries/2-strchr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | char *_strchr(char *s, char c) 8 | { 9 | return strchr(s, c); 10 | } 11 | 12 | -------------------------------------------------------------------------------- /0x09-static_libraries/2-strchr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mostafa101296/alx-low_level_programming/31145c309aa2ee439037a77e33b6ef9e6dc2c99f/0x09-static_libraries/2-strchr.o -------------------------------------------------------------------------------- /0x09-static_libraries/2-strlen.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | int _strlen(char *s) 8 | { 9 | return strlen(s); 10 | } 11 | -------------------------------------------------------------------------------- /0x09-static_libraries/2-strlen.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mostafa101296/alx-low_level_programming/31145c309aa2ee439037a77e33b6ef9e6dc2c99f/0x09-static_libraries/2-strlen.o -------------------------------------------------------------------------------- /0x09-static_libraries/2-strncpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | char *_strncpy(char *dest, char *src, int n) 8 | { 9 | return strncpy(dest, src, n); 10 | } 11 | -------------------------------------------------------------------------------- /0x09-static_libraries/2-strncpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mostafa101296/alx-low_level_programming/31145c309aa2ee439037a77e33b6ef9e6dc2c99f/0x09-static_libraries/2-strncpy.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-islower.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | int _islower(int c) 8 | { 9 | return (c >= 'a' && c <= 'z'); 10 | } 11 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-islower.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mostafa101296/alx-low_level_programming/31145c309aa2ee439037a77e33b6ef9e6dc2c99f/0x09-static_libraries/3-islower.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-puts.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | void _puts(char *s) 8 | { 9 | puts(s); 10 | } 11 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-puts.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mostafa101296/alx-low_level_programming/31145c309aa2ee439037a77e33b6ef9e6dc2c99f/0x09-static_libraries/3-puts.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-strcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | int _strcmp(char *s1, char *s2) 8 | { 9 | return strcmp(s1, s2); 10 | } 11 | 12 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-strcmp.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mostafa101296/alx-low_level_programming/31145c309aa2ee439037a77e33b6ef9e6dc2c99f/0x09-static_libraries/3-strcmp.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-strspn.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | unsigned int _strspn(char *s, char *accept) 8 | { 9 | return strspn(s, accept); 10 | } 11 | 12 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-strspn.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mostafa101296/alx-low_level_programming/31145c309aa2ee439037a77e33b6ef9e6dc2c99f/0x09-static_libraries/3-strspn.o -------------------------------------------------------------------------------- /0x09-static_libraries/4-isalpha.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | int _isalpha(int c) 8 | { 9 | return isalpha(c); 10 | } 11 | -------------------------------------------------------------------------------- /0x09-static_libraries/4-isalpha.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mostafa101296/alx-low_level_programming/31145c309aa2ee439037a77e33b6ef9e6dc2c99f/0x09-static_libraries/4-isalpha.o -------------------------------------------------------------------------------- /0x09-static_libraries/4-strpbrk.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | char *_strpbrk(char *s, char *accept) 8 | { 9 | return strpbrk(s, accept); 10 | } 11 | -------------------------------------------------------------------------------- /0x09-static_libraries/4-strpbrk.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mostafa101296/alx-low_level_programming/31145c309aa2ee439037a77e33b6ef9e6dc2c99f/0x09-static_libraries/4-strpbrk.o -------------------------------------------------------------------------------- /0x09-static_libraries/5-strstr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | char *_strstr(char *haystack, char *needle) 8 | { 9 | return strstr(haystack, needle); 10 | } 11 | -------------------------------------------------------------------------------- /0x09-static_libraries/5-strstr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mostafa101296/alx-low_level_programming/31145c309aa2ee439037a77e33b6ef9e6dc2c99f/0x09-static_libraries/5-strstr.o -------------------------------------------------------------------------------- /0x09-static_libraries/6-abs.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | int _abs(int n) 8 | { 9 | return abs(n); 10 | } 11 | -------------------------------------------------------------------------------- /0x09-static_libraries/6-abs.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mostafa101296/alx-low_level_programming/31145c309aa2ee439037a77e33b6ef9e6dc2c99f/0x09-static_libraries/6-abs.o -------------------------------------------------------------------------------- /0x09-static_libraries/9-strcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | char *_strcpy(char *dest, char *src) 7 | { 8 | return strcpy(dest, src); 9 | } 10 | -------------------------------------------------------------------------------- /0x09-static_libraries/9-strcpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mostafa101296/alx-low_level_programming/31145c309aa2ee439037a77e33b6ef9e6dc2c99f/0x09-static_libraries/9-strcpy.o -------------------------------------------------------------------------------- /0x09-static_libraries/README.md: -------------------------------------------------------------------------------- 1 | 0x09. C - Static libraries Task and explationation 2 | -------------------------------------------------------------------------------- /0x09-static_libraries/_putchar.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | int _putchar(char c) 8 | { 9 | return putchar(c); 10 | } 11 | -------------------------------------------------------------------------------- /0x09-static_libraries/_putchar.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mostafa101296/alx-low_level_programming/31145c309aa2ee439037a77e33b6ef9e6dc2c99f/0x09-static_libraries/_putchar.o -------------------------------------------------------------------------------- /0x09-static_libraries/create_static_lib.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -Wall -Wextra -Werror -pedantic -c *.c 3 | ar rc liball.a *.o 4 | -------------------------------------------------------------------------------- /0x09-static_libraries/liball.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mostafa101296/alx-low_level_programming/31145c309aa2ee439037a77e33b6ef9e6dc2c99f/0x09-static_libraries/liball.a -------------------------------------------------------------------------------- /0x09-static_libraries/libmy.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mostafa101296/alx-low_level_programming/31145c309aa2ee439037a77e33b6ef9e6dc2c99f/0x09-static_libraries/libmy.a -------------------------------------------------------------------------------- /0x09-static_libraries/main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | int main(void) 4 | { 5 | _puts("\"At the end of the day, my goal was to be the best hacker\"\n\t- Kevin Mitnick"); 6 | return (0); 7 | } 8 | -------------------------------------------------------------------------------- /0x09-static_libraries/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | int _putchar(char c); 5 | int _islower(int c); 6 | int _isalpha(int c); 7 | int _abs(int n); 8 | int _isupper(int c); 9 | int _isdigit(int c); 10 | int _strlen(char *s); 11 | void _puts(char *s); 12 | char *_strcpy(char *dest, char *src); 13 | int _atoi(char *s); 14 | char *_strcat(char *dest, char *src); 15 | char *_strncat(char *dest, char *src, int n); 16 | char *_strncpy(char *dest, char *src, int n); 17 | int _strcmp(char *s1, char *s2); 18 | char *_memset(char *s, char b, unsigned int n); 19 | char *_memcpy(char *dest, char *src, unsigned int n); 20 | char *_strchr(char *s, char c); 21 | unsigned int _strspn(char *s, char *accept); 22 | char *_strpbrk(char *s, char *accept); 23 | char *_strstr(char *haystack, char *needle); 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /0x09-static_libraries/main.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mostafa101296/alx-low_level_programming/31145c309aa2ee439037a77e33b6ef9e6dc2c99f/0x09-static_libraries/main.o -------------------------------------------------------------------------------- /0x09-static_libraries/quote: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mostafa101296/alx-low_level_programming/31145c309aa2ee439037a77e33b6ef9e6dc2c99f/0x09-static_libraries/quote -------------------------------------------------------------------------------- /0x0A-argc_argv/.3-mul.c.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mostafa101296/alx-low_level_programming/31145c309aa2ee439037a77e33b6ef9e6dc2c99f/0x0A-argc_argv/.3-mul.c.swp -------------------------------------------------------------------------------- /0x0A-argc_argv/0-whatsmyname.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - Prints the name of the program 6 | * @argc: Number of command line arguments 7 | * @argv: Array name 8 | * Return: 0 9 | */ 10 | 11 | int main(int argc __attribute__((unused)), char *argv[]) 12 | { 13 | printf("%s\n", *argv); 14 | 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x0A-argc_argv/1-args.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | /** 4 | * main - print the name of the program 5 | * @argc: Count arguments 6 | * @argv: Arguments 7 | * Return: Always 0 (Success) 8 | */ 9 | int main(int argc, char **argv) 10 | { 11 | (void) argv; /*Ignore argv*/ 12 | printf("%i\n", argc - 1); 13 | 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x0A-argc_argv/100-change.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "main.h" 4 | /** 5 | * main - function 6 | * @argc: length of argv 7 | * @argv: number of argument 8 | * Return: Always 0 9 | */ 10 | int main(int argc, char *argv[]) 11 | { 12 | /*Declaring variables*/ 13 | int position, total, change, aux; 14 | int coins[] = {25, 10, 5, 2, 1}; /*Array int*/ 15 | 16 | position = total = change = aux = 0; 17 | 18 | if (argc != 2) 19 | { 20 | printf("Error\n"); 21 | return (1); 22 | } 23 | 24 | total = atoi(argv[1]); /*Covert str to int*/ 25 | 26 | if (total <= 0) 27 | { 28 | printf("0\n"); 29 | return (0); 30 | } 31 | 32 | /*Declaring While*/ 33 | while (coins[position] != '\0') 34 | { 35 | if (total >= coins[position]) 36 | { 37 | aux = (total / coins[position]); 38 | change += aux; 39 | total -= coins[position] * aux; 40 | } 41 | 42 | position++; 43 | } 44 | printf("%d\n", change); 45 | 46 | return (0); 47 | } 48 | -------------------------------------------------------------------------------- /0x0A-argc_argv/2-args.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | /** 4 | * main - Print the name of the program 5 | * @argc: Count arguments 6 | * @argv: Arguments 7 | * Return: Always 0 (Success) 8 | */ 9 | int main(int argc, char **argv) 10 | { 11 | /*Declaring variables*/ 12 | int count = 0; 13 | 14 | if (argc > 0) 15 | { 16 | /*WHILE - Print each arguments*/ 17 | while (count < argc) 18 | { 19 | printf("%s\n", argv[count]); 20 | count++; 21 | } 22 | } 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x0A-argc_argv/3-mul.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | /** 5 | * main - prints the multiplication of two integers 6 | * @argc: argument count 7 | * @argv: argument vector 8 | * Return: 0 if it works, 1 if there is an ERROR 9 | */ 10 | int main(int argc, char **argv) 11 | { 12 | int n, m, diff; 13 | 14 | if (argc != 3) 15 | { 16 | printf("Error\n"); 17 | return (1); 18 | } 19 | 20 | n = atoi(argv[1]); 21 | m = atoi(argv[2]); 22 | diff = n * m; 23 | 24 | printf("%i\n", diff); 25 | 26 | return (0); 27 | } 28 | -------------------------------------------------------------------------------- /0x0A-argc_argv/4-add.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | /** 6 | * main - main funct 7 | * @argc: param count 8 | * @argv: param poiter 9 | * Return: int 10 | */ 11 | int main(int argc, char *argv[]) 12 | { 13 | int i, j, sum = 0; 14 | 15 | for (i = 1; i < argc; i++) 16 | { 17 | for (j = 0; argv[i][j] != '\0'; j++) 18 | { 19 | if (!isdigit(argv[i][j])) 20 | { 21 | printf("Error\n"); 22 | return (1); 23 | } 24 | } 25 | sum += atoi(argv[i]); 26 | } 27 | printf("%d\n", sum); 28 | 29 | return (0); 30 | } 31 | -------------------------------------------------------------------------------- /0x0A-argc_argv/README.md: -------------------------------------------------------------------------------- 1 | 0x0A. C - argc, argv 2 | -------------------------------------------------------------------------------- /0x0A-argc_argv/_putchar: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * _putchar - writes the character c to stdout 6 | * @c:Ther character to print 7 | * Return: On success 1. 8 | * On error, -1 is returned and errno is set appropriately. 9 | */ 10 | int _putchar(char c) 11 | { 12 | return (write(1, &c, 1)); 13 | } 14 | -------------------------------------------------------------------------------- /0x0A-argc_argv/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | int _putchar(char c); 5 | int _atoi(char *s); 6 | 7 | #endif 8 | -------------------------------------------------------------------------------- /0x0B-malloc_free/0-create_array.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | /** 4 | * create_array - creates an array of chars dynamically 5 | * @size: size of the array 6 | * @c: the string 7 | * Return: 0 means success except defined otherwise 8 | */ 9 | char *create_array(unsigned int size, char c) 10 | { 11 | char *buffer; 12 | unsigned int position; 13 | 14 | if (size == 0) 15 | { 16 | return (NULL); 17 | } 18 | 19 | /*Define values with malloc*/ 20 | buffer = (char *) malloc(size * sizeof(c)); 21 | 22 | if (buffer == 0) 23 | { 24 | return (NULL); 25 | } 26 | else 27 | { 28 | position = 0; 29 | while (position < size) /*While for array*/ 30 | { 31 | *(buffer + position) = c; 32 | position++; 33 | } 34 | 35 | return (buffer); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /0x0B-malloc_free/1-strdup.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_strlen - count array 4 | *@s: array of elements 5 | *Return: 1 6 | */ 7 | 8 | int _strlen(char *s) 9 | { 10 | unsigned int i; 11 | 12 | i = 0; 13 | while (s[i] != '\0') /*count character of string*/ 14 | { 15 | i++; 16 | } 17 | 18 | return (i); 19 | } 20 | 21 | /** 22 | *_strcpy - copy arrays 23 | *@src: array of elements 24 | *@dest: dest array 25 | *Return: dest 26 | */ 27 | 28 | char *_strcpy(char *dest, char *src) 29 | { 30 | int i = 0; 31 | 32 | while (src[i] != '\0') 33 | { 34 | dest[i] = src[i]; 35 | i++; 36 | } 37 | dest[i] = '\0'; 38 | 39 | return (dest); 40 | } 41 | 42 | /** 43 | *_strdup - array for prints a string 44 | *@str: array of elements 45 | *Return: pointer 46 | */ 47 | 48 | char *_strdup(char *str) 49 | { 50 | char *dst; 51 | unsigned int size; 52 | 53 | if (str == 0) 54 | { 55 | return (NULL); 56 | } 57 | 58 | size = _strlen(str) + 1; 59 | 60 | dst = (char *) malloc(size * sizeof(char)); 61 | 62 | if (dst == 0) 63 | { 64 | return (NULL); 65 | } 66 | 67 | _strcpy(dst, str); 68 | return (dst); 69 | } 70 | -------------------------------------------------------------------------------- /0x0B-malloc_free/100-argstostr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * argstostr - prints args 4 | * @ac: takes in width of grid 5 | * @av: height of grid 6 | * Return: the args one line at a time 7 | */ 8 | 9 | char *argstostr(int ac, char **av) 10 | { 11 | char *str; 12 | int count = 0, a = 0, b = 0, c = 0; 13 | 14 | if (ac == 0 || av == NULL) 15 | return (NULL); 16 | while (a < ac) 17 | { 18 | b = 0; 19 | while (av[a][b] != '\0') 20 | { 21 | count++; 22 | b++; 23 | } 24 | a++; 25 | } 26 | count = count + ac + 1; 27 | str = malloc(sizeof(char) * count); 28 | if (str == NULL) 29 | { 30 | return (NULL); 31 | } 32 | for (a = 0; a < ac; a++) 33 | { 34 | for (b = 0; av[a][b] != '\0'; b++) 35 | { 36 | str[c] = av[a][b]; 37 | c++; 38 | } 39 | str[c] = '\n'; 40 | c++; 41 | } 42 | return (str); 43 | } 44 | -------------------------------------------------------------------------------- /0x0B-malloc_free/101-strtow.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * count_word - helper function to count the number of words in a string 6 | * @s: string to evaluate 7 | * 8 | * Return: number of words 9 | */ 10 | int count_word(char *s) 11 | { 12 | int flag, c, w; 13 | 14 | flag = 0; 15 | w = 0; 16 | 17 | for (c = 0; s[c] != '\0'; c++) 18 | { 19 | if (s[c] == ' ') 20 | flag = 0; 21 | else if (flag == 0) 22 | { 23 | flag = 1; 24 | w++; 25 | } 26 | } 27 | 28 | return (w); 29 | } 30 | /** 31 | * **strtow - splits a string into words 32 | * @str: string to split 33 | * 34 | * Return: pointer to an array of strings (Success) 35 | * or NULL (Error) 36 | */ 37 | char **strtow(char *str) 38 | { 39 | char **matrix, *tmp; 40 | int i, k = 0, len = 0, words, c = 0, start, end; 41 | 42 | while (*(str + len)) 43 | len++; 44 | words = count_word(str); 45 | if (words == 0) 46 | return (NULL); 47 | 48 | matrix = (char **) malloc(sizeof(char *) * (words + 1)); 49 | if (matrix == NULL) 50 | return (NULL); 51 | 52 | for (i = 0; i <= len; i++) 53 | { 54 | if (str[i] == ' ' || str[i] == '\0') 55 | { 56 | if (c) 57 | { 58 | end = i; 59 | tmp = (char *) malloc(sizeof(char) * (c + 1)); 60 | if (tmp == NULL) 61 | return (NULL); 62 | while (start < end) 63 | *tmp++ = str[start++]; 64 | *tmp = '\0'; 65 | matrix[k] = tmp - c; 66 | k++; 67 | c = 0; 68 | } 69 | } 70 | else if (c++ == 0) 71 | start = i; 72 | } 73 | 74 | matrix[k] = NULL; 75 | 76 | return (matrix); 77 | } 78 | -------------------------------------------------------------------------------- /0x0B-malloc_free/2-str_concat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_strlen - count arrray 4 | *@s: array of elements 5 | *Return: i 6 | */ 7 | 8 | int _strlen(char *s) 9 | { 10 | unsigned int i; 11 | 12 | i = 0; 13 | while (s[i] != '\0') /*Count character of string*/ 14 | { 15 | i++; 16 | } 17 | 18 | return (i); 19 | } 20 | 21 | /** 22 | *str_concat - back a pointer to array 23 | *@s1: Array one 24 | *@s2: Array two 25 | *Return: Always an array dinamic 26 | */ 27 | 28 | char *str_concat(char *s1, char *s2) 29 | { 30 | char *dst; 31 | unsigned int i, j, size; 32 | 33 | /*If the array is empty*/ 34 | if (s1 == NULL) 35 | s1 = ""; 36 | 37 | if (s2 == NULL) 38 | s2 = ""; 39 | 40 | /*count size total*/ 41 | size = (_strlen(s1) + _strlen(s2) + 1); 42 | 43 | /*malloc*/ 44 | dst = (char *) malloc(size * sizeof(char)); 45 | 46 | if (dst == 0) 47 | { 48 | return (NULL); 49 | } 50 | 51 | /*Concatenate arrays*/ 52 | for (i = 0; *(s1 + i) != '\0'; i++) 53 | *(dst + i) = *(s1 + i); 54 | 55 | for (j = 0; *(s2 + j) != '\0'; j++) 56 | { 57 | *(dst + i) = *(s2 + j); 58 | i++; 59 | } 60 | 61 | return (dst); 62 | } 63 | -------------------------------------------------------------------------------- /0x0B-malloc_free/3-alloc_grid.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * alloc_grid - allocates a grid, make space and free space 4 | * @width: takes in width of grid 5 | * @height: height of grid 6 | * Return: grid with freed spaces 7 | */ 8 | 9 | int **alloc_grid(int width, int height) 10 | { 11 | /*Declaring variables*/ 12 | int **grid; 13 | int i, j; 14 | 15 | if (width <= 0 || height <= 0) 16 | { 17 | return (NULL); 18 | } 19 | 20 | grid = malloc(sizeof(int *) * height); /*malloc*/ 21 | 22 | if (grid == NULL) 23 | { 24 | return (NULL); 25 | } 26 | 27 | for (i = 0; i < height; i++) 28 | { 29 | grid[i] = malloc(sizeof(int) * width); 30 | if (grid[i] == NULL) 31 | { 32 | for (i = i - 1; i >= 0; i--) 33 | { 34 | free(grid[i]); 35 | } 36 | free(grid); 37 | return (NULL); 38 | } 39 | } 40 | for (i = 0; j < width; j++) 41 | grid[i][j] = 0; 42 | return (grid); 43 | } 44 | -------------------------------------------------------------------------------- /0x0B-malloc_free/4-free_grid.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * free_grid - allocates a grid, make space and free space 4 | * @grid: takes in width of grid 5 | * @height: height of grid 6 | * Return: free grid 7 | */ 8 | 9 | void free_grid(int **grid, int height) 10 | { 11 | int i; 12 | 13 | for (i = 0; i < height; i++) 14 | { 15 | free(grid[i]); 16 | } 17 | 18 | free(grid); 19 | } 20 | -------------------------------------------------------------------------------- /0x0B-malloc_free/README.md: -------------------------------------------------------------------------------- 1 | 0x0B-malloc_free 2 | -------------------------------------------------------------------------------- /0x0B-malloc_free/_putchar: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _putchar - writes the character c to stdout 5 | * @c: The character to print 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | int _putchar(char c) 11 | { 12 | return (write(1, &c, 1)); 13 | } 14 | -------------------------------------------------------------------------------- /0x0B-malloc_free/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | #include 4 | #include 5 | int _putchar(char c); 6 | char *create_array(unsigned int size, char c); 7 | char *_strdup(char *str); 8 | char *str_concat(char *s1, char *s2); 9 | int **alloc_grid(int width, int height); 10 | void free_grid(int **grid, int height); 11 | char *argstostr(int ac, char **av); 12 | #endif 13 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/0-malloc_checked.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "main.h" 4 | /** 5 | * malloc_checked - A function that allocates memory using malloc. 6 | * @b: integer variable dat holds the memory size 7 | * Return: 0 means program was successful 8 | */ 9 | void *malloc_checked(unsigned int b) 10 | { 11 | void *ptr; 12 | 13 | ptr = malloc(b); 14 | if (ptr == NULL) 15 | exit(98); 16 | 17 | return (ptr); 18 | } 19 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/1-string_nconcat.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "main.h" 4 | /** 5 | * string_nconcat - concatenates two strings. 6 | * @s1: first string 7 | * @s2: second string 8 | * @n: index 9 | * Return: char pointer 10 | */ 11 | 12 | char *string_nconcat(char *s1, char *s2, unsigned int n) 13 | { 14 | char *p; 15 | unsigned int size1 = 0, size2 = 0, i; 16 | 17 | if (s1 == NULL) 18 | s1 = ""; 19 | 20 | if (s2 == NULL) 21 | s2 = ""; 22 | 23 | while (s1[size1] != '\0') 24 | { 25 | size1++; 26 | } 27 | 28 | while (s2[size2] != '\0') 29 | { 30 | size2++; 31 | } 32 | 33 | if (n > size2) 34 | n = size2; 35 | p = malloc((size1 + n + 1) * sizeof(char)); 36 | 37 | if (p == NULL) 38 | return (0); 39 | 40 | for (i = 0; i < size1; i++) 41 | { 42 | p[i] = s1[i]; 43 | } 44 | 45 | for (; i < (size1 + n); i++) 46 | { 47 | p[i] = s2[i - size1]; 48 | } 49 | p[i] = '\0'; 50 | 51 | return (p); 52 | } 53 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/100-realloc.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "main.h" 4 | /** 5 | * _realloc - reallocates a memory block using malloc and free 6 | * @ptr: pointer 7 | * @old_size: old size 8 | * @new_size: new size 9 | * Return: pointer 10 | */ 11 | void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size) 12 | { 13 | char *clone, *relloc; 14 | unsigned int i; 15 | 16 | if (ptr != NULL) 17 | { 18 | clone = ptr; 19 | } 20 | else 21 | { 22 | return (malloc(new_size)); 23 | } 24 | if (new_size == old_size) 25 | { 26 | return (ptr); 27 | } 28 | if (new_size == 0 && ptr != NULL) 29 | { 30 | free(ptr); 31 | 32 | return (0); 33 | } 34 | relloc = malloc(new_size); 35 | 36 | if (relloc == NULL) 37 | { 38 | return (0); 39 | } 40 | for (i = 0; i < (old_size || i < new_size); i++) 41 | { 42 | *(relloc + i) = clone[i]; 43 | } 44 | free(ptr); 45 | 46 | return (relloc); 47 | } 48 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/101-mul.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | #include 4 | 5 | /** 6 | * _atoi_digit - convert a char to integer. 7 | * @x: character to convert. 8 | * Return: integer. 9 | **/ 10 | 11 | int _atoi_digit(char x) 12 | { 13 | unsigned int res; 14 | 15 | if (x <= '9' && x >= '0') 16 | res = x - '0'; 17 | return (res); 18 | } 19 | 20 | /** 21 | * _isNumber - Define if a string is a number. 22 | * @argv: Pointer to string. 23 | * Return: success (0). 24 | **/ 25 | int _isNumber(char *argv) 26 | { 27 | int i; 28 | 29 | for (i = 0; argv[i]; i++) 30 | if (argv[i] < 48 || argv[i] > 57) 31 | return (1); 32 | return (0); 33 | } 34 | 35 | /** 36 | *_calloc - allocate array of size * nmemb. 37 | * @nmemb: number of elements. 38 | * @size: size of element. 39 | * Return: pointer to array. 40 | **/ 41 | 42 | void *_calloc(unsigned int nmemb, unsigned int size) 43 | { 44 | char *tab; 45 | unsigned int i; 46 | 47 | tab = malloc(size * nmemb); 48 | 49 | if (tab == NULL) 50 | return (NULL); 51 | 52 | for (i = 0; i < (size * nmemb); i++) 53 | tab[i] = '0'; 54 | 55 | return (tab); 56 | } 57 | 58 | /** 59 | * mul_array - multiply two arrays. 60 | * @a1: first array. 61 | * @len1: length of array a1. 62 | * @a2: char. 63 | * @a3: array for result. 64 | * @lena: length of array a3. 65 | * Return: pointer to array. 66 | **/ 67 | 68 | void *mul_array(char *a1, int len1, char a2, char *a3, int lena) 69 | { 70 | int mul = 0, i, k; 71 | 72 | k = lena; 73 | for (i = len1 - 1; i >= 0 ; i--) 74 | { 75 | mul += (a1[i] - '0') * (a2 - '0') + (a3[k] - '0'); 76 | a3[k] = (mul % 10) + '0'; 77 | mul /= 10; 78 | k--; 79 | } 80 | 81 | while (mul != 0) 82 | { 83 | mul += a3[k] - '0'; 84 | a3[k] = (mul % 10) + '0'; 85 | mul /= 10; 86 | k--; 87 | } 88 | 89 | return (a3); 90 | } 91 | /** 92 | * print_array - print all digits of array. 93 | * @nb: number of elements to print. 94 | * @a: array of elements. 95 | **/ 96 | void print_array(char *a, int nb) 97 | { 98 | int i = 0; 99 | 100 | while (a[i] == '0' && (i + 1) < nb) 101 | { 102 | i++; 103 | } 104 | for (; i < nb; i++) 105 | { 106 | _putchar(a[i]); 107 | } 108 | _putchar('\n'); 109 | } 110 | 111 | /** 112 | *main - print the multiplication of 2 numbers. 113 | *@argc: array length. 114 | *@argv: array. 115 | *Return: 0. 116 | */ 117 | 118 | int main(int argc, char *argv[]) 119 | { 120 | int i, c, len1, len2, lenres; 121 | char E[6] = {'E', 'r', 'r', 'o', 'r', '\n'}; 122 | char *tabres; 123 | 124 | if (argc != 3 || _isNumber(argv[1]) == 1 || _isNumber(argv[2]) == 1) 125 | { 126 | for (i = 0; i < 6; i++) 127 | { 128 | _putchar(E[i]); 129 | } 130 | exit(98); 131 | } 132 | for (len1 = 0; argv[1][len1]; len1++) 133 | ; 134 | for (len2 = 0; argv[2][len2]; len2++) 135 | ; 136 | lenres = len1 + len2; 137 | tabres = _calloc(lenres, sizeof(int)); 138 | if (tabres == NULL) 139 | { 140 | free(tabres); 141 | return (0); 142 | } 143 | for (i = len2 - 1, c = 0; i >= 0; i--) 144 | { 145 | tabres = mul_array(argv[1], len1, argv[2][i], tabres, (lenres - 1 - c)); 146 | c++; 147 | } 148 | print_array(tabres, lenres); 149 | free(tabres); 150 | exit(EXIT_SUCCESS); 151 | return (0); 152 | } 153 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/2-calloc.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "main.h" 4 | /** 5 | * _memset - copy char 6 | * @s: string 7 | * @b: input 8 | * @n: bytes 9 | * Return: string 10 | */ 11 | char *_memset(char *s, char b, unsigned int n) 12 | { 13 | unsigned int i; 14 | 15 | for (i = 0; i < n; i++) 16 | { 17 | s[i] = b; 18 | } 19 | 20 | return (s); 21 | } 22 | /** 23 | * _calloc - allocates memory for an array using malloc 24 | * @nmemb: n elements 25 | * @size: bytes 26 | * Return: pointer 27 | */ 28 | void *_calloc(unsigned int nmemb, unsigned int size) 29 | { 30 | void *p; 31 | 32 | if (nmemb == 0 || size == 0) 33 | { 34 | return (NULL); 35 | } 36 | p = malloc(nmemb * size); 37 | 38 | if (p == NULL) 39 | { 40 | return (NULL); 41 | } 42 | _memset(p, 0, (nmemb * size)); 43 | 44 | return (p); 45 | } 46 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/3-array_range.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "main.h" 4 | /** 5 | * array_range - creates an array of integers 6 | * @min: minimum 7 | * @max: maximum 8 | * Return: array 9 | */ 10 | int *array_range(int min, int max) 11 | { 12 | int *arr, i = 0, t = min; 13 | 14 | if (min > max) 15 | return (0); 16 | arr = malloc((max - min + 1) * sizeof(int)); 17 | 18 | if (!arr) 19 | return (0); 20 | while (i <= max - min) 21 | arr[i++] = t++; 22 | return (arr); 23 | } 24 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/README.md: -------------------------------------------------------------------------------- 1 | more malloc task 2 | 3 | 4 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _putchar - writes the character c to stdout 5 | * @c: The character to print 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | int _putchar(char c) 11 | { 12 | return (write(1, &c, 1)); 13 | } 14 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | #include 5 | int _putchar(char c); 6 | void *malloc_checked(unsigned int b); 7 | char *string_nconcat(char *s1, char *s2, unsigned int n); 8 | void *_calloc(unsigned int nmemb, unsigned int size); 9 | int *array_range(int min, int max); 10 | void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size); 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /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 | * main - Program compilation starts from the main 4 | * Description: this program prints the name of the file it was comipiled from 5 | * Return: 0 (success) 6 | */ 7 | int main(void) 8 | { 9 | printf("%s\n", __FILE__); 10 | return (0); 11 | } 12 | -------------------------------------------------------------------------------- /0x0D-preprocessor/3-function_like_macro.h: -------------------------------------------------------------------------------- 1 | #ifndef _FUNCTION_LIKE_MACRO_ 2 | #define _FUNCTION_LIKE_MACRO_ 3 | 4 | #define ABS(X) ((X < (0)) ? ((X) * (-1)) : (X)) 5 | #endif 6 | -------------------------------------------------------------------------------- /0x0D-preprocessor/4-sum.h: -------------------------------------------------------------------------------- 1 | #ifndef SUM_X_Y 2 | #define SUM_X_Y 3 | 4 | #define SUM(X, Y) ((X) + (Y)) 5 | #endif 6 | -------------------------------------------------------------------------------- /0x0D-preprocessor/README.md: -------------------------------------------------------------------------------- 1 | Preprocessor files 2 | 3 | 4 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/1-init_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | /** 3 | * init_dog - initialize a variable of type struct dog. 4 | * @d: pointer to struct type dog 5 | * @name: pointer to char name's dog 6 | * @age: age's dog 7 | * @owner: pointer to char owner's dog 8 | * Return: No. 9 | */ 10 | 11 | void init_dog(struct dog *d, char *name, float age, char *owner) 12 | { 13 | if (d) 14 | { 15 | d->name = name; 16 | d->age = age; 17 | d->owner = owner; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/2-print_dog.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | /** 4 | * print_dog - prints a struct dog 5 | * @d: pointer to struct type dog 6 | * Return: Nothing cause it's void 7 | */ 8 | void print_dog(struct dog *d) 9 | { 10 | if (d) 11 | { 12 | if (d->name == NULL) 13 | printf("Name: (nil)\n"); 14 | 15 | else 16 | printf("Name: %s\n", d->name); 17 | 18 | printf("Age: %f\n", d->age); 19 | 20 | if (d->owner == NULL) 21 | printf("Owner: (nil)\n"); 22 | 23 | else 24 | printf("Owner: %s\n", d->owner); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/4-new_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | #include 4 | /** 5 | * new_dog - new dog 6 | * @name: name's dog 7 | * @age: age's dog 8 | * @owner: owner's dog 9 | * Return: newdog 10 | */ 11 | dog_t *new_dog(char *name, float age, char *owner) 12 | { 13 | 14 | int i = 0, j = 0, k; 15 | dog_t *doge; 16 | 17 | while (name[i] != '\0') 18 | i++; 19 | while (owner[j] != '\0') 20 | j++; 21 | doge = malloc(sizeof(dog_t)); 22 | if (doge == NULL) 23 | { 24 | free(doge); 25 | return (NULL); 26 | } 27 | doge->name = malloc(i * sizeof(doge->name)); 28 | if (doge->name == NULL) 29 | { 30 | free(doge->name); 31 | free(doge); 32 | return (NULL); 33 | } 34 | for (k = 0; k <= i; k++) 35 | doge->name[k] = name[k]; 36 | doge->age = age; 37 | doge->owner = malloc(j * sizeof(doge->owner)); 38 | if (doge->owner == NULL) 39 | { 40 | free(doge->owner); 41 | free(doge->name); 42 | free(doge); 43 | return (NULL); 44 | } 45 | for (k = 0; k <= j; k++) 46 | doge->owner[k] = owner[k]; 47 | return (doge); 48 | } 49 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/5-free_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | 4 | /** 5 | * free_dog - free memory of dog 6 | * @d: pointer to dog 7 | */ 8 | 9 | void free_dog(dog_t *d) 10 | { 11 | if (d) 12 | { 13 | if (d->name) 14 | free(d->name); 15 | if (d->owner) 16 | free(d->owner); 17 | free(d); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/README.md: -------------------------------------------------------------------------------- 1 | Structure and type def 2 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/dog.h: -------------------------------------------------------------------------------- 1 | #ifndef DOG_H 2 | #define DOG_H 3 | /** 4 | * struct dog - user defined data type 5 | * @name: name of the dog 6 | * @age: age of the dog 7 | * @owner: owner of the dog 8 | */ 9 | struct dog 10 | { 11 | char *name; 12 | float age; 13 | char *owner; 14 | }; 15 | typedef struct dog dog_t; 16 | void init_dog(struct dog *d, char *name, float age, char *owner); 17 | void print_dog(struct dog *d); 18 | dog_t *new_dog(char *name, float age, char *owner); 19 | void free_dog(dog_t *d); 20 | #endif 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 0x02. C - Functions, nested loops 2 | --------------------------------------------------------------------------------