diff options
| author | Charles <sircharlesaze@gmail.com> | 2019-07-06 08:42:30 +0200 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2019-07-06 08:42:30 +0200 |
| commit | d732047d6681b1f64f7907d1c0413abdaab76050 (patch) | |
| tree | c2123d55a7431de9bb55371dcc78c955da607c6f /c04 | |
| parent | af8435d40cdb8e7871ff004fb21382c236f9bd0f (diff) | |
| download | piscine-d732047d6681b1f64f7907d1c0413abdaab76050.tar.gz piscine-d732047d6681b1f64f7907d1c0413abdaab76050.tar.bz2 piscine-d732047d6681b1f64f7907d1c0413abdaab76050.zip | |
c03 and begin c04
Diffstat (limited to 'c04')
| -rw-r--r-- | c04/ex00/ft_strlen.c | 21 | ||||
| -rw-r--r-- | c04/ex01/ft_putstr.c | 22 | ||||
| -rw-r--r-- | c04/ex02/ft_putnbr.c | 44 | ||||
| -rw-r--r-- | c04/ex03/ft_atoi.c | 50 | ||||
| -rw-r--r-- | c04/ex04/ft_putnbr_base.c | 70 | ||||
| -rw-r--r-- | c04/ex05/ft_atoi_base.c | 48 | ||||
| -rw-r--r-- | c04/main.c | 20 |
7 files changed, 275 insertions, 0 deletions
diff --git a/c04/ex00/ft_strlen.c b/c04/ex00/ft_strlen.c new file mode 100644 index 0000000..271088d --- /dev/null +++ b/c04/ex00/ft_strlen.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlen.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/06 08:31:20 by cacharle #+# #+# */ +/* Updated: 2019/07/06 08:32:00 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_strlen(char *str) +{ + int counter; + + counter = 0; + while (str[counter]) + counter++; + return (counter); +} diff --git a/c04/ex01/ft_putstr.c b/c04/ex01/ft_putstr.c new file mode 100644 index 0000000..e98b5a0 --- /dev/null +++ b/c04/ex01/ft_putstr.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/06 07:22:38 by cacharle #+# #+# */ +/* Updated: 2019/07/06 08:34:03 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include <unistd.h> + +void ft_putstr(char *str) +{ + while (*str) + { + write(1, str, 1); + str++; + } +} diff --git a/c04/ex02/ft_putnbr.c b/c04/ex02/ft_putnbr.c new file mode 100644 index 0000000..8a13dc5 --- /dev/null +++ b/c04/ex02/ft_putnbr.c @@ -0,0 +1,44 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putnbr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/06 07:24:04 by cacharle #+# #+# */ +/* Updated: 2019/07/06 08:34:48 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include <unistd.h> + +void ft_putchar(char c) +{ + write(1, &c, 1); +} + +void ft_putnbr(int nb) +{ + int i; + int rev_digits[100]; + unsigned int nbu; + + if (nb < 0) + { + ft_putchar('-'); + nbu = -nb; + } else + nbu = nb; + i = 0; + while (nbu > 0) + { + rev_digits[i] = nbu % 10; + nbu /= 10; + i++; + } + while (i > 0) + { + i--; + ft_putchar(rev_digits[i] + '0'); + } +} diff --git a/c04/ex03/ft_atoi.c b/c04/ex03/ft_atoi.c new file mode 100644 index 0000000..0d4247b --- /dev/null +++ b/c04/ex03/ft_atoi.c @@ -0,0 +1,50 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_atoi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/06 07:31:38 by cacharle #+# #+# */ +/* Updated: 2019/07/06 08:36:47 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int pow10(int exponent) +{ + int accumulator; + + accumulator = 1; + while (exponent > 0) + { + accumulator *= 10; + exponent--; + } + return (accumulator); +} + +int ft_atoi(char *str) +{ + int is_negative; + int nb; + int i; + + while (*str == ' ' || *str == '\t' || *str == '\n' + || *str == '\v' || *str == '\f' || *str == '\r') + str++; + while (*str == '-' || *str == '+') + { + if (*str == '-') + is_negative = !is_negative; + str++; + } + i = 0; + while (str[i] >= '0' && str[i] <= '9') + { + nb += pow10(i) * (str[i] - '0'); + i++; + } + if (is_negative) + nb = -nb; + return (nb); +} diff --git a/c04/ex04/ft_putnbr_base.c b/c04/ex04/ft_putnbr_base.c new file mode 100644 index 0000000..6827475 --- /dev/null +++ b/c04/ex04/ft_putnbr_base.c @@ -0,0 +1,70 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putnbr_base.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/06 07:47:44 by cacharle #+# #+# */ +/* Updated: 2019/07/06 08:37:59 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int check_base(char *base) +{ + int i; + + if (!*base) + return (0); + while (*base) + { + if (*base == '-' || *base == '+') + return (0); + i = 0; + while (base[i]) + { + if (base[i] == *base) + return (0); + i++; + } + } + return (1); +} + +int my_strlen(char *str) +{ + int counter; + + counter = 0; + while (*str) + { + counter++; + str++; + } + return (counter); +} + +void ft_putnbr_base(int nbr, char *base) +{ + int radix; + int i; + int nb; + int rev_digits[1024]; + + if (!check_base(base)) + return; + radix = my_strlen(base); + nb = 0; + i = 0; + while (nb > 0) + { + rev_digits[i] = base[nb % radix]; + nb /= radix; + i++; + } + while (i > 0) + { + i--; + write(1, rev_digits + i, 1); + } +} diff --git a/c04/ex05/ft_atoi_base.c b/c04/ex05/ft_atoi_base.c new file mode 100644 index 0000000..b595a5c --- /dev/null +++ b/c04/ex05/ft_atoi_base.c @@ -0,0 +1,48 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_atoi_base.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/06 08:07:17 by cacharle #+# #+# */ +/* Updated: 2019/07/06 08:30:16 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int my_pow(int base, int exponent) +{ + int accumulator; + + accumulator = 1; + while (exponent > 0) + { + accumulator *= base; + exponent--; + } + return (accumulator); +} + +int ft_atoi_base(char *str, char *base) +{ + int radix; + int i; + int j; + int nb; + + radix = 0; + while (base[radix]) + radix++; + i = 0; + while (str[i]) + i++; + j = 0; + while (i > 0) + { + nb += my_pow(radix, j); + nb %= radix; + i--; + j++; + } + return nb; +} diff --git a/c04/main.c b/c04/main.c new file mode 100644 index 0000000..a165b3c --- /dev/null +++ b/c04/main.c @@ -0,0 +1,20 @@ +#include <stdio.h> +#include <string.h> +#include "ex00/ft_strlen.c" +#include "ex01/ft_putstr.c" +#include "ex02/ft_putnbr.c" +#include "ex03/ft_atoi.c" +#include "ex04/ft_putnbr_base.c" +#include "ex05/ft_atoi_base.c" + +int main() +{ + char *s1 = "jesuis7"; + printf("%d\n", ft_strlen(s1)); + + char *s = "bonjour"; + ft_putstr(s); + printf("\n"); + + +} |
