From 3ed6ae7c279f0583a0ddbd1e19f7f7a151369ca0 Mon Sep 17 00:00:00 2001 From: starnakin Date: Wed, 5 Jul 2023 20:30:59 +0200 Subject: [PATCH] add: itoA --- bozolib.h | 1 + src/int/int.h | 6 ++++++ src/int/itoA.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+) create mode 100644 src/int/int.h create mode 100644 src/int/itoA.c diff --git a/bozolib.h b/bozolib.h index 46c2e6e..a756b45 100644 --- a/bozolib.h +++ b/bozolib.h @@ -3,3 +3,4 @@ #include "./src/lst/lst.h" #include "./src/str/str.h" #include "./src/tab/tab.h" +#include "./src/int/int.h" diff --git a/src/int/int.h b/src/int/int.h new file mode 100644 index 0000000..241524c --- /dev/null +++ b/src/int/int.h @@ -0,0 +1,6 @@ +#pragma once + +#include +#include + +char* itoA(int n); diff --git a/src/int/itoA.c b/src/int/itoA.c new file mode 100644 index 0000000..9af052a --- /dev/null +++ b/src/int/itoA.c @@ -0,0 +1,43 @@ +#include "int.h" + +static int ft_nb_digit(int n) +{ + int out; + + out = 0; + while (n) + { + n /= 10; + out++; + } + return (out); +} + +char* itoA(int n) +{ + char *out; + unsigned int nb[2]; + + if (!n) + return (strdup("0")); + nb[0] = ft_nb_digit(n); + if (n < 0) + { + nb[1] = -n; + nb[0]++; + } + else + nb[1] = n; + out = malloc(sizeof(char) * (nb[0] + 1)); + if (out == NULL) + return (NULL); + out[nb[0]--] = 0; + if (n < 0) + out[0] = '-'; + while (nb[1]) + { + out[nb[0]--] = nb[1] % 10 + 48; + nb[1] /= 10; + } + return (out); +}