From 1fd0ebe76b81187fa428b4d699fb70cd4407fef5 Mon Sep 17 00:00:00 2001 From: Cabergs Charles Date: Sun, 7 Jul 2019 15:29:30 +0200 Subject: c04/c05 testing + c06 --- c02/ex10/ft_strlcpy.c | 2 +- c03/ex00/ft_strcmp.c | 4 +- c03/ex01/ft_strncmp.c | 10 +++-- c03/ex02/ft_strcat.c | 16 +++----- c03/ex03/ft_strncat.c | 19 ++++------ c03/ex04/ft_strstr.c | 10 ++--- c03/ex05/ft_strlcat.c | 31 ++++++++------- c03/main.c | 41 ++++++++++---------- c04/ex00/ft_strlen.c | 4 +- c04/ex01/ft_putstr.c | 4 +- c04/ex02/ft_putnbr.c | 15 +++----- c04/ex03/ft_atoi.c | 13 ++----- c04/ex04/ft_putnbr_base.c | 30 ++++++++------- c04/ex05/ft_atoi_base.c | 40 +++++++++++++++++--- c04/ft_atoi.o | Bin 0 -> 1300 bytes c04/ft_putnbr.o | Bin 0 -> 1212 bytes c04/ft_putnbr_base.o | Bin 0 -> 1756 bytes c04/ft_putstr.o | Bin 0 -> 708 bytes c04/ft_strlen.o | Bin 0 -> 652 bytes c04/main.c | 20 +++++++++- c05/ex00/ft_iterative_factorial.c | 26 +++++++++++++ c05/ex01/ft_recursive_factorial.c | 20 ++++++++++ c05/ex02/ft_iterative_power.c | 28 ++++++++++++++ c05/ex03/ft_recursive_power.c | 22 +++++++++++ c05/ex04/ft_fibonacci.c | 22 +++++++++++ c05/ex05/ft_sqrt.c | 25 +++++++++++++ c05/ex06/ft_is_prime.c | 31 +++++++++++++++ c05/ex07/ft_find_next_prime.c | 40 ++++++++++++++++++++ c05/main.c | 76 +++++++++++++++++++++++++++++++++++++ c06/bonjour | Bin 0 -> 8432 bytes c06/ex00/ft_print_program_name.c | 23 ++++++++++++ c06/ex01/ft_print_params.c | 31 +++++++++++++++ c06/ex02/ft_rev_params.c | 28 ++++++++++++++ c06/ex03/ft_sort_params.c | 77 ++++++++++++++++++++++++++++++++++++++ 34 files changed, 596 insertions(+), 112 deletions(-) create mode 100644 c04/ft_atoi.o create mode 100644 c04/ft_putnbr.o create mode 100644 c04/ft_putnbr_base.o create mode 100644 c04/ft_putstr.o create mode 100644 c04/ft_strlen.o create mode 100644 c05/ex00/ft_iterative_factorial.c create mode 100644 c05/ex01/ft_recursive_factorial.c create mode 100644 c05/ex02/ft_iterative_power.c create mode 100644 c05/ex03/ft_recursive_power.c create mode 100644 c05/ex04/ft_fibonacci.c create mode 100644 c05/ex05/ft_sqrt.c create mode 100644 c05/ex06/ft_is_prime.c create mode 100644 c05/ex07/ft_find_next_prime.c create mode 100644 c05/main.c create mode 100755 c06/bonjour create mode 100644 c06/ex00/ft_print_program_name.c create mode 100644 c06/ex01/ft_print_params.c create mode 100644 c06/ex02/ft_rev_params.c create mode 100644 c06/ex03/ft_sort_params.c diff --git a/c02/ex10/ft_strlcpy.c b/c02/ex10/ft_strlcpy.c index ef4af68..ca12b06 100644 --- a/c02/ex10/ft_strlcpy.c +++ b/c02/ex10/ft_strlcpy.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/04 15:27:01 by cacharle #+# #+# */ -/* Updated: 2019/07/05 14:31:13 by cacharle ### ########.fr */ +/* Updated: 2019/07/06 16:13:23 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/c03/ex00/ft_strcmp.c b/c03/ex00/ft_strcmp.c index 9131204..ab09c68 100644 --- a/c03/ex00/ft_strcmp.c +++ b/c03/ex00/ft_strcmp.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/05 10:39:51 by cacharle #+# #+# */ -/* Updated: 2019/07/05 16:07:46 by cacharle ### ########.fr */ +/* Updated: 2019/07/06 13:10:58 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,5 +17,5 @@ int ft_strcmp(char *s1, char *s2) s1++; s2++; } - return *s1 - *s2; + return (*s1 - *s2); } diff --git a/c03/ex01/ft_strncmp.c b/c03/ex01/ft_strncmp.c index 1dc2639..9009164 100644 --- a/c03/ex01/ft_strncmp.c +++ b/c03/ex01/ft_strncmp.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/05 10:50:46 by cacharle #+# #+# */ -/* Updated: 2019/07/05 16:06:57 by cacharle ### ########.fr */ +/* Updated: 2019/07/07 13:58:50 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,8 +14,12 @@ int ft_strncmp(char *s1, char *s2, unsigned int n) { unsigned int i; + if (n == 0) + return (0); i = 0; - while (s1[i] == s2[i] && s1[i] && s2[i] && i < n - 1) + while (s1[i] == s2[i] && s1[i] && s2[i] && i < n) i++; - return s1[i] - s2[i]; + if (i == n) + i--; + return (s1[i] - s2[i]); } diff --git a/c03/ex02/ft_strcat.c b/c03/ex02/ft_strcat.c index 1ec10c0..e3b5e7a 100644 --- a/c03/ex02/ft_strcat.c +++ b/c03/ex02/ft_strcat.c @@ -6,23 +6,19 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/05 11:52:56 by cacharle #+# #+# */ -/* Updated: 2019/07/05 12:58:36 by cacharle ### ########.fr */ +/* Updated: 2019/07/07 10:31:36 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -char *ft_strcat(char *dest, char *src) +char *ft_strcat(char *dest, char *src) { - char *dest_head; + char *dest_head; dest_head = dest; while (*dest) dest++; while (*src) - { - *dest = *src; - dest++; - src++; - } - *dest = *src; - return dest_head; + *(dest++) = *(src++); + *dest = '\0'; + return (dest_head); } diff --git a/c03/ex03/ft_strncat.c b/c03/ex03/ft_strncat.c index c28fb38..4e5a999 100644 --- a/c03/ex03/ft_strncat.c +++ b/c03/ex03/ft_strncat.c @@ -6,24 +6,21 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/05 13:02:04 by cacharle #+# #+# */ -/* Updated: 2019/07/05 15:33:07 by cacharle ### ########.fr */ +/* Updated: 2019/07/07 10:41:01 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -char *ft_strncat(char *dest, char *src, unsigned int nb) +char *ft_strncat(char *dest, char *src, unsigned int nb) { unsigned int i; - char *dest_end; + unsigned int j; i = 0; - while (dest[i] != '\0') + while (dest[i]) i++; - dest_end = dest + i; - i = 0; - while (i < nb && src[i] != '\0') - { - dest_end[i] = src[i]; - i++; - } + j = 0; + while (j < nb && src[j]) + dest[i++] = src[j++]; + dest[i] = '\0'; return (dest); } diff --git a/c03/ex04/ft_strstr.c b/c03/ex04/ft_strstr.c index e27ffc8..c8d015e 100644 --- a/c03/ex04/ft_strstr.c +++ b/c03/ex04/ft_strstr.c @@ -6,13 +6,13 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/05 15:20:54 by cacharle #+# #+# */ -/* Updated: 2019/07/05 16:46:57 by cacharle ### ########.fr */ +/* Updated: 2019/07/06 13:12:55 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #define MY_NULL 0x0 -int ft_strlen(char *str) +int ft_strlen(char *str) { int counter; @@ -25,19 +25,19 @@ int ft_strlen(char *str) return (counter); } -char *ft_strstr(char *str, char *to_find) +char *ft_strstr(char *str, char *to_find) { int i; if (!ft_strlen(to_find)) - return str; + return (str); while (*str) { i = 0; while (to_find[i] && str[i]) { if (to_find[i] != str[i]) - break; + break ; i++; } if (i == ft_strlen(to_find)) diff --git a/c03/ex05/ft_strlcat.c b/c03/ex05/ft_strlcat.c index 5cacc4d..1e7f9dc 100644 --- a/c03/ex05/ft_strlcat.c +++ b/c03/ex05/ft_strlcat.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/05 15:43:34 by cacharle #+# #+# */ -/* Updated: 2019/07/06 07:12:28 by cacharle ### ########.fr */ +/* Updated: 2019/07/07 11:08:08 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,30 +15,29 @@ unsigned int my_strlen(char *str) int counter; counter = 0; - while (*str != '\0') - { + while (str[counter]) counter++; - str++; - } return (counter); } unsigned int ft_strlcat(char *dest, char *src, unsigned int size) { unsigned int i; - unsigned int len_src; - unsigned int len_dest; + unsigned int dest_len; + unsigned int src_len; - len_src = my_strlen(src); - len_dest = my_strlen(dest); - if (!len_src) - return (len_dest); - i = len_dest - 1; - while (i < size - len_dest - 1 && src[i]) + dest_len = my_strlen(dest); + src_len = my_strlen(src); + if (size == 0) + return (src_len); + i = dest_len; + while (i < size - 1 && src[i - dest_len]) { - dest[i] = src[i]; + dest[i] = src[i - dest_len]; i++; } - dest[i] = '\0'; - return (len_src + len_dest); + if (dest[size - 1] != '\0') + return (src_len + size); + dest[size - 1] = '\0'; + return (dest_len + src_len); } diff --git a/c03/main.c b/c03/main.c index 9c1add8..11ae881 100644 --- a/c03/main.c +++ b/c03/main.c @@ -9,16 +9,17 @@ int main() { - char s1[] = "bonjour"; - char s2[] = "bonjouj"; - printf("%d : %d\n", ft_strcmp(s1, s2), strcmp(s1, s2)); + char s3[] = ""; + char s4[] = "qwer"; + printf("%d : %d\n", ft_strcmp(s3, s4), strcmp(s3, s4)); - unsigned int size = 10; - printf("%d : %d\n", ft_strncmp(s1, s2, size), strncmp(s1, s2, size)); + unsigned int size = 4; + printf("%d : %d\n", ft_strncmp(s3, s4, size), strncmp(s3, s4, size)); + printf("-----------------\n"); char *head; char dest[10] = "abc"; - char src[] = "defg"; + char src[] = "a"; head = ft_strcat(dest, src); printf("\n%s ", head); for (int i = 0; i < 15; i++) @@ -29,45 +30,47 @@ int main() for (int i = 0; i < 15; i++) printf("%d ", head[i]); - unsigned int nsize = 1; + printf("\n-----------------\n"); + unsigned int nsize = 2; char *nhead; - char ndest[10] = "abc"; + char ndest[12] = "abc"; char nsrc[] = "defg"; nhead = ft_strncat(ndest, nsrc, nsize); printf("\n%s ", nhead); for (int i = 0; i < 15; i++) printf("%d ", nhead[i]); - char _ndest[10] = "abc"; + char _ndest[12] = "abc"; nhead = strncat(_ndest, nsrc, nsize); printf("\n%s ", nhead); for (int i = 0; i < 15; i++) printf("%d ", nhead[i]); - printf("\n"); + printf("\n-----------------\n"); char *haystack = "abcdefg"; - char *needle = "abcdefg"; + char *needle = ""; char *found; found = ft_strstr(haystack, needle); + printf("\n%s ", found); if (found) for (int i = 0; i < 5; i++) printf("%d ", found[i]); - printf("\n%s\n", found); found = strstr(haystack, needle); + printf("\n%s ", found); if (found) for (int i = 0; i < 5; i++) printf("%d ", found[i]); - printf("\n%s\n", found); - unsigned int lsize = 100; - char ldest[100] = "abcdef"; - char lsrc[] = "d"; + printf("\n-----------------\n"); + unsigned int lsize = 15; + char ldest[15] = "0123456789"; + char lsrc[] = "doit"; printf("\nsize %u, ", ft_strlcat(ldest, lsrc, lsize)); printf("%s ", ldest); for (int i = 0; i < 15; i++) printf("%d ", ldest[i]); - char _ldest[100] = "abcdef"; + char _ldest[15] = "0123456789"; printf("\nsize %lu, ", strlcat(_ldest, lsrc, lsize)); - printf("%s ", ldest); + printf("%s ", _ldest); for (int i = 0; i < 15; i++) - printf("%d ", ldest[i]); + printf("%d ", _ldest[i]); } diff --git a/c04/ex00/ft_strlen.c b/c04/ex00/ft_strlen.c index 271088d..4355784 100644 --- a/c04/ex00/ft_strlen.c +++ b/c04/ex00/ft_strlen.c @@ -6,11 +6,11 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/06 08:31:20 by cacharle #+# #+# */ -/* Updated: 2019/07/06 08:32:00 by cacharle ### ########.fr */ +/* Updated: 2019/07/06 13:14:51 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -int ft_strlen(char *str) +int ft_strlen(char *str) { int counter; diff --git a/c04/ex01/ft_putstr.c b/c04/ex01/ft_putstr.c index e98b5a0..92b855d 100644 --- a/c04/ex01/ft_putstr.c +++ b/c04/ex01/ft_putstr.c @@ -6,13 +6,13 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/06 07:22:38 by cacharle #+# #+# */ -/* Updated: 2019/07/06 08:34:03 by cacharle ### ########.fr */ +/* Updated: 2019/07/06 13:15:17 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include -void ft_putstr(char *str) +void ft_putstr(char *str) { while (*str) { diff --git a/c04/ex02/ft_putnbr.c b/c04/ex02/ft_putnbr.c index 4ba6a0e..020dfa6 100644 --- a/c04/ex02/ft_putnbr.c +++ b/c04/ex02/ft_putnbr.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/06 07:24:04 by cacharle #+# #+# */ -/* Updated: 2019/07/06 08:47:01 by cacharle ### ########.fr */ +/* Updated: 2019/07/06 15:09:46 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,21 +19,21 @@ void ft_putchar(char c) void ft_putnbr(int nb) { - int i; + int i; int rev_digits[100]; unsigned int nbu; if (nb == 0) { ft_putchar('0'); - return; + return ; } + nbu = nb; if (nb < 0) { ft_putchar('-'); nbu = -nb; - } else - nbu = nb; + } i = 0; while (nbu > 0) { @@ -42,8 +42,5 @@ void ft_putnbr(int nb) i++; } while (i > 0) - { - i--; - ft_putchar(rev_digits[i] + '0'); - } + ft_putchar(rev_digits[--i] + '0'); } diff --git a/c04/ex03/ft_atoi.c b/c04/ex03/ft_atoi.c index 10f6b07..a26308e 100644 --- a/c04/ex03/ft_atoi.c +++ b/c04/ex03/ft_atoi.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/06 07:31:38 by cacharle #+# #+# */ -/* Updated: 2019/07/06 10:23:20 by cacharle ### ########.fr */ +/* Updated: 2019/07/06 15:48:43 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -31,29 +31,22 @@ int ft_atoi(char *str) int j; while (*str == ' ' || *str == '\t' || *str == '\n' - || *str == '\v' || *str == '\f' || *str == '\r') + || *str == '\v' || *str == '\f' || *str == '\r') str++; is_negative = 0; - /*printf("> %s\n", str);*/ while (*str == '-' || *str == '+') { if (*str == '-') is_negative = !is_negative; str++; } - /*printf("> %s\n", str);*/ nb = 0; i = 0; while (str[i] >= '0' && str[i] <= '9') i++; j = 0; while (str[j] >= '0' && str[j] <= '9') - { - /*printf("%d i, %d nb\n", i, nb);*/ - i--; - nb += pow10(i) * (str[j] - '0'); - j++; - } + nb += pow10(--i) * (str[j++] - '0'); if (is_negative) nb = -nb; return (nb); diff --git a/c04/ex04/ft_putnbr_base.c b/c04/ex04/ft_putnbr_base.c index 764c42b..da6fb22 100644 --- a/c04/ex04/ft_putnbr_base.c +++ b/c04/ex04/ft_putnbr_base.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/06 07:47:44 by cacharle #+# #+# */ -/* Updated: 2019/07/06 11:12:25 by cacharle ### ########.fr */ +/* Updated: 2019/07/07 12:30:16 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -48,28 +48,30 @@ int my_strlen(char *str) void ft_putnbr_base(int nbr, char *base) { - int radix; - int i; - int nb; - char rev_digits[1024]; + int radix; + int i; + unsigned int nbu; + char rev_digits[1024]; if (!check_base(base)) - return; + return ; radix = my_strlen(base); + nbu = nbr; + if (nbr < 0) + { + write(1, "-", 1); + nbu = -nbr; + } i = 0; - /*printf("\n%d base %d (%s)\n", nbr, radix, base);*/ - while (nbr > 0) + while (nbu > 0) { - /*printf("%d %d %c\n", nbr, nbr % radix, base[nbr % radix]);*/ - rev_digits[i] = base[nbr % radix]; - nbr /= radix; + rev_digits[i] = base[nbu % radix]; + nbu /= radix; i++; } - /*for (int k = 0; k < 10; k++)*/ - /*printf("%d ", rev_digits[k]);*/ while (i > 0) { i--; - write(1, rev_digits + i, 1); + write(1, rev_digits + i, 1); } } diff --git a/c04/ex05/ft_atoi_base.c b/c04/ex05/ft_atoi_base.c index 5de5f81..9a89fc1 100644 --- a/c04/ex05/ft_atoi_base.c +++ b/c04/ex05/ft_atoi_base.c @@ -6,10 +6,36 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/06 08:07:17 by cacharle #+# #+# */ -/* Updated: 2019/07/06 12:47:25 by cacharle ### ########.fr */ +/* Updated: 2019/07/07 13:26:28 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ +int check_the_base(char *base) +{ + int i; + int j; + + i = 0; + while (base[i]) + { + if (base[i] == '-' || base[i] == '+' || base[i] == ' ' + || base[i] == '\t' || base[i] == '\n' || base[i] == '\v' + || base[i] == '\f' || base[i] == '\r') + return (0); + j = 0; + while (base[j]) + { + if (j != i && base[j] == base[i]) + return (0); + j++; + } + i++; + } + if (i < 2) + return (0); + return (1); +} + int my_pow(int base, int exponent) { int accumulator; @@ -33,23 +59,25 @@ int position_in_base(char digit, char *base) return (i); } -int ft_atoi_base(char *str, char *base) +int ft_atoi_base(char *str, char *base) { int radix; int i; int j; int nb; + if (!check_the_base(base)) + return (0); nb = 0; radix = 0; - while (base[++radix]); + while (base[radix]) + radix++; i = 0; - while (str[++i]); - printf("%s base %d (%s)\n", str, radix, base); + while (str[i]) + i++; j = 0; while (--i >= 0) { - printf("%c: %d * (%d^%d) = %d\n", str[i], position_in_base(str[i], base), radix, i, my_pow(radix, i) * position_in_base(str[j], base)); nb += my_pow(radix, i) * position_in_base(str[j], base); j++; } diff --git a/c04/ft_atoi.o b/c04/ft_atoi.o new file mode 100644 index 0000000..8c7622b Binary files /dev/null and b/c04/ft_atoi.o differ diff --git a/c04/ft_putnbr.o b/c04/ft_putnbr.o new file mode 100644 index 0000000..4d8c488 Binary files /dev/null and b/c04/ft_putnbr.o differ diff --git a/c04/ft_putnbr_base.o b/c04/ft_putnbr_base.o new file mode 100644 index 0000000..db3788f Binary files /dev/null and b/c04/ft_putnbr_base.o differ diff --git a/c04/ft_putstr.o b/c04/ft_putstr.o new file mode 100644 index 0000000..099c1e4 Binary files /dev/null and b/c04/ft_putstr.o differ diff --git a/c04/ft_strlen.o b/c04/ft_strlen.o new file mode 100644 index 0000000..33f0ca0 Binary files /dev/null and b/c04/ft_strlen.o differ diff --git a/c04/main.c b/c04/main.c index 0e035a9..d4324a8 100644 --- a/c04/main.c +++ b/c04/main.c @@ -17,12 +17,14 @@ int main() ft_putstr(s); printf("\n"); + printf("----------------------\n"); ft_putnbr(0); printf("\n"); ft_putnbr(42); printf("\n"); ft_putnbr(-42); printf("\n"); ft_putnbr(INT_MAX); printf("\n"); ft_putnbr(INT_MIN); printf("\n"); + printf("----------------------\n"); char *s_happypath = "42"; char *s_happypathn = "-42"; char *s_int0 = "0"; @@ -33,6 +35,7 @@ int main() char *pos = "++--+++--4"; char *garbage_tail = "76iqu21#!@"; char *all = "\n\t \v++++---12341234#3%^@"; + char *subject_test = " ---+--+1234ab567"; printf("%d\n", ft_atoi(s_happypath)); printf("%d\n", ft_atoi(s_happypathn)); printf("%d\n", ft_atoi(s_int0)); @@ -43,18 +46,31 @@ int main() printf("%d\n", ft_atoi(pos)); printf("%d\n", ft_atoi(garbage_tail)); printf("%d\n", ft_atoi(all)); + printf("%d\n", ft_atoi(subject_test)); printf("----------------------\n"); - // doit gerer les nombres negatifs ?? ft_putnbr_base(42, "0123456789"); printf("\n"); + ft_putnbr_base(-42, "0123456789"); printf("\n"); ft_putnbr_base(42, "01"); printf("\n"); + ft_putnbr_base(-42, "01"); printf("\n"); ft_putnbr_base(42, "0123456789abcdef"); printf("\n"); ft_putnbr_base(42, "01234567"); printf("\n"); - ft_putnbr_base(INT_MAX - 5, "01"); printf("\n"); + ft_putnbr_base(INT_MAX, "0123456789abcdef"); printf("\n"); + ft_putnbr_base(INT_MIN, "0123456789abcdef"); printf("\n"); ft_putnbr_base(INT_MAX, ""); ft_putnbr_base(INT_MAX, "a"); + ft_putnbr_base(INT_MAX, "abb"); + ft_putnbr_base(INT_MAX, "-0123456789"); printf("----------------------\n"); printf("%d\n", ft_atoi_base("111000", "01")); printf("%d\n", ft_atoi_base("ff", "0123456789abcdef")); + printf("%d\n", ft_atoi_base("52", "01234567")); + printf("%d\n", ft_atoi_base("2a", "0123456789abcdef")); + printf("%d\n", ft_atoi_base("7fffffff", "0123456789abcdef")); + printf("%d\n", ft_atoi_base("ff", "0123-456789abcdef")); + printf("%d\n", ft_atoi_base("ff", "01\r23456789abcdef")); + printf("%d\n", ft_atoi_base("ff", "abcc")); + printf("%d\n", ft_atoi_base("ff", "")); + printf("%d\n", ft_atoi_base("ff", "a")); } diff --git a/c05/ex00/ft_iterative_factorial.c b/c05/ex00/ft_iterative_factorial.c new file mode 100644 index 0000000..fa64f59 --- /dev/null +++ b/c05/ex00/ft_iterative_factorial.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_iterative_factorial.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/06 16:48:21 by cacharle #+# #+# */ +/* Updated: 2019/07/06 17:14:26 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_iterative_factorial(int nb) +{ + int acc; + int i; + + if (nb < 0) + return (0); + if (nb == 0) + return (1); + acc = 1; + while (nb > 0) + acc *= nb--; + return (acc); +} diff --git a/c05/ex01/ft_recursive_factorial.c b/c05/ex01/ft_recursive_factorial.c new file mode 100644 index 0000000..615cd04 --- /dev/null +++ b/c05/ex01/ft_recursive_factorial.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_recursive_factorial.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/06 17:15:10 by cacharle #+# #+# */ +/* Updated: 2019/07/06 17:18:58 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_recursive_factorial(int nb) +{ + if (nb < 0) + return (0); + if (nb == 0 || nb == 1) + return (1); + return nb * ft_recursive_factorial(nb - 1); +} diff --git a/c05/ex02/ft_iterative_power.c b/c05/ex02/ft_iterative_power.c new file mode 100644 index 0000000..ccfd359 --- /dev/null +++ b/c05/ex02/ft_iterative_power.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_iterative_power.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/06 17:20:15 by cacharle #+# #+# */ +/* Updated: 2019/07/06 17:39:06 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_iterative_power(int nb, int power) +{ + int acc; + + if (power < 0) + return (0); + if (power == 0) + return (1); + acc = 1; + while (power > 0) + { + acc *= nb; + power--; + } + return (acc); +} diff --git a/c05/ex03/ft_recursive_power.c b/c05/ex03/ft_recursive_power.c new file mode 100644 index 0000000..94c4fbd --- /dev/null +++ b/c05/ex03/ft_recursive_power.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_recursive_power.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/06 17:39:43 by cacharle #+# #+# */ +/* Updated: 2019/07/06 19:32:17 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_recursive_power(int nb, int power) +{ + if (power < 0) + return (0); + if (power == 0) + return (1); + if (power == 1) + return (nb); + return nb * ft_recursive_power(nb, power - 1); +} diff --git a/c05/ex04/ft_fibonacci.c b/c05/ex04/ft_fibonacci.c new file mode 100644 index 0000000..66ff9b1 --- /dev/null +++ b/c05/ex04/ft_fibonacci.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_fibonacci.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/06 19:34:02 by cacharle #+# #+# */ +/* Updated: 2019/07/06 19:37:37 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_fibonacci(int index) +{ + if (index < 0) + return (-1); + if (index == 0) + return (0); + if (index == 1 || index == 2) + return (1); + return ft_fibonacci(index - 1) + ft_fibonacci(index - 2); +} diff --git a/c05/ex05/ft_sqrt.c b/c05/ex05/ft_sqrt.c new file mode 100644 index 0000000..8a86489 --- /dev/null +++ b/c05/ex05/ft_sqrt.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_sqrt.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/06 20:06:48 by cacharle #+# #+# */ +/* Updated: 2019/07/06 20:11:19 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_sqrt(int nb) +{ + int i; + + i = 0; + while (i <= nb) + { + if (i * i == nb) + return (i); + i++; + } + return (0); +} diff --git a/c05/ex06/ft_is_prime.c b/c05/ex06/ft_is_prime.c new file mode 100644 index 0000000..05628a2 --- /dev/null +++ b/c05/ex06/ft_is_prime.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_is_prime.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/06 20:17:59 by cacharle #+# #+# */ +/* Updated: 2019/07/07 08:27:21 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_is_prime(int nb) +{ + int i; + + if (nb <= 1) + return (0); + if (nb <= 3) + return (1); + if (nb % 2 == 0 || nb % 3 == 0) + return (0); + i = 5; + while (i * i <= nb) + { + if (nb % i == 0 || nb % (i + 2) == 0) + return (0); + i += 6; + } + return (1); +} diff --git a/c05/ex07/ft_find_next_prime.c b/c05/ex07/ft_find_next_prime.c new file mode 100644 index 0000000..a83f308 --- /dev/null +++ b/c05/ex07/ft_find_next_prime.c @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_find_next_prime.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/07 07:50:11 by cacharle #+# #+# */ +/* Updated: 2019/07/07 08:29:02 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + + +int is_prime(int nb) +{ + int i; + + if (nb <= 1) + return (0); + if (nb <= 3) + return (1); + if (nb % 2 == 0 || nb % 3 == 0) + return (0); + i = 5; + while (i * i <= nb) + { + if (nb % i == 0 || nb % (i + 2) == 0) + return (0); + i += 6; + } + return (1); +} + +int ft_find_next_prime(int nb) +{ + while (!is_prime(nb)) + nb++; + return (nb); +} + diff --git a/c05/main.c b/c05/main.c new file mode 100644 index 0000000..ca9a1c1 --- /dev/null +++ b/c05/main.c @@ -0,0 +1,76 @@ +#include +#include +#include +#include "ex00/ft_iterative_factorial.c" +#include "ex01/ft_recursive_factorial.c" +#include "ex02/ft_iterative_power.c" +#include "ex03/ft_recursive_power.c" +#include "ex04/ft_fibonacci.c" +#include "ex05/ft_sqrt.c" +#include "ex06/ft_is_prime.c" +#include "ex07/ft_find_next_prime.c" + +int main() +{ + printf("%d! = %d\n", -1, ft_iterative_factorial(-1)); + printf("%d! = %d\n", 0, ft_iterative_factorial(0)); + printf("%d! = %d\n", 1, ft_iterative_factorial(1)); + printf("%d! = %d\n", 4, ft_iterative_factorial(4)); + printf("%d! = %d\n", 6, ft_iterative_factorial(6)); + printf("%d! = %d\n", 10, ft_iterative_factorial(10)); + + printf("---------------------\n"); + printf("%d! = %d\n", -1, ft_recursive_factorial(-1)); + printf("%d! = %d\n", 0, ft_recursive_factorial(0)); + printf("%d! = %d\n", 1, ft_recursive_factorial(1)); + printf("%d! = %d\n", 4, ft_recursive_factorial(4)); + printf("%d! = %d\n", 6, ft_recursive_factorial(6)); + printf("%d! = %d\n", 10, ft_recursive_factorial(10)); + + printf("---------------------\n"); + printf("%d^%d = %d\n", 2, 0, ft_iterative_power(2, 0)); + printf("%d^%d = %d\n", 2, 2, ft_iterative_power(2, 4)); + printf("%d^%d = %d\n", 3, 3, ft_iterative_power(3, 3)); + printf("%d^%d = %d\n", 4, 5, ft_iterative_power(4, 5)); + + printf("---------------------\n"); + printf("%d^%d = %d\n", 2, 0, ft_recursive_power(2, 0)); + printf("%d^%d = %d\n", 2, 2, ft_recursive_power(2, 4)); + printf("%d^%d = %d\n", 3, 3, ft_recursive_power(3, 3)); + printf("%d^%d = %d\n", 4, 5, ft_recursive_power(4, 5)); + + printf("---------------------\n"); + printf("F%d = %d\n", -1, ft_fibonacci(-1)); + printf("F%d = %d\n", 0, ft_fibonacci(0)); + printf("F%d = %d\n", 1, ft_fibonacci(1)); + printf("F%d = %d\n", 2, ft_fibonacci(2)); + printf("F%d = %d\n", 3, ft_fibonacci(3)); + printf("F%d = %d\n", 8, ft_fibonacci(8)); + printf("F%d = %d\n", 30, ft_fibonacci(30)); + /*printf("F%d = %d\n", 41, ft_fibonacci(41));*/ + + printf("---------------------\n"); + printf("sqrt(%d) = %d\n", -36, ft_sqrt(-36)); + printf("sqrt(%d) = %d\n", 0, ft_sqrt(0)); + printf("sqrt(%d) = %d\n", 4, ft_sqrt(4)); + printf("sqrt(%d) = %d\n", 9, ft_sqrt(4)); + printf("sqrt(%d) = %d\n", 678, ft_sqrt(678 * 678)); + + printf("---------------------\n"); + printf("prime(%d) = %d\n", 3, ft_is_prime(3)); + printf("prime(%d) = %d\n", 25, ft_is_prime(25)); + printf("prime(%d) = %d\n", 17, ft_is_prime(17)); + printf("prime(%d) = %d\n", 21, ft_is_prime(21)); + printf("prime(%d) = %d\n", 36, ft_is_prime(36)); + printf("prime(%d) = %d\n", 2147483617, ft_is_prime(2147483617)); + /*printf("prime(%d) = %d\n", 2147483629, ft_is_prime(2147483629));*/ + /*for (int i = INT_MAX; i > INT_MAX - 100; i--)*/ + /*printf("%d is %d\n", i, ft_is_prime(i));*/ + + printf("---------------------\n"); + printf("nextp(%d) = %d\n", 21, ft_find_next_prime(21)); + printf("nextp(%d) = %d\n", 23, ft_find_next_prime(23)); + printf("nextp(%d) = %d\n", 2147483600, ft_find_next_prime(2147483600)); + /*for (int i = INT_MAX; i > INT_MAX - 100; i--)*/ + /*printf("%d is %d\n", i, ft_find_next_prime(i));*/ +} diff --git a/c06/bonjour b/c06/bonjour new file mode 100755 index 0000000..2b50ec2 Binary files /dev/null and b/c06/bonjour differ diff --git a/c06/ex00/ft_print_program_name.c b/c06/ex00/ft_print_program_name.c new file mode 100644 index 0000000..3e0ada3 --- /dev/null +++ b/c06/ex00/ft_print_program_name.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_print_program_name.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/07 14:53:06 by cacharle #+# #+# */ +/* Updated: 2019/07/07 14:56:01 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +int main(int argc, char **argv) +{ + while (**argv) + { + write(1, *argv, 1); + (*argv)++; + } + return (0); +} diff --git a/c06/ex01/ft_print_params.c b/c06/ex01/ft_print_params.c new file mode 100644 index 0000000..29806db --- /dev/null +++ b/c06/ex01/ft_print_params.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_print_params.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/07 14:57:09 by cacharle #+# #+# */ +/* Updated: 2019/07/07 15:01:02 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +int main(int argc, char **argv) +{ + int i; + + i = 1; + while (i < argc) + { + while (*argv[i]) + { + write(1, argv[i], 1); + argv[i]++; + } + write(1, "\n", 1); + i++; + } + return (0); +} diff --git a/c06/ex02/ft_rev_params.c b/c06/ex02/ft_rev_params.c new file mode 100644 index 0000000..fe64949 --- /dev/null +++ b/c06/ex02/ft_rev_params.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_rev_params.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/07 15:02:56 by cacharle #+# #+# */ +/* Updated: 2019/07/07 15:10:34 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +int main(int argc, char **argv) +{ + while (argc > 1) + { + argc--; + while (*argv[argc]) + { + write(1, argv[argc], 1); + argv[argc]++; + } + write(1, "\n", 1); + } + return (0); +} diff --git a/c06/ex03/ft_sort_params.c b/c06/ex03/ft_sort_params.c new file mode 100644 index 0000000..01f64db --- /dev/null +++ b/c06/ex03/ft_sort_params.c @@ -0,0 +1,77 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_sort_params.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/07 15:12:51 by cacharle #+# #+# */ +/* Updated: 2019/07/07 15:27:49 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +int ft_strcmp(char *s1, char *s2) +{ + while (*s1 == *s2 && *s1 && *s2) + { + s1++; + s2++; + } + return (*s1 - *s2); +} + +int is_sorted(char **argv, int argc) +{ + int i; + + i = 0; + while (i < argc - 1) + { + if (ft_strcmp(argv[i], argv[i + 1]) > 0) + return (0); + i++; + } + return (1); +} + +void sort_argv(char **argv, int argc) +{ + int i; + char *tmp; + + while (!is_sorted(argv, argc)) + { + i = 0; + while (i < argc - 1) + { + if (ft_strcmp(argv[i], argv[i + 1]) > 0) + { + tmp = argv[i]; + argv[i] = argv[i + 1]; + argv[i + 1] = tmp; + } + i++; + } + } +} + +int main(int argc, char **argv) +{ + sort_argv(argv, argc); + int i; + + i = 1; + while (i < argc) + { + while (*argv[i]) + { + write(1, argv[i], 1); + argv[i]++; + } + write(1, "\n", 1); + i++; + } + return (0); +} -- cgit