add: itoA

This commit is contained in:
starnakin 2023-07-05 20:30:59 +02:00
parent 698e18ecb4
commit 3ed6ae7c27
3 changed files with 50 additions and 0 deletions

View File

@ -3,3 +3,4 @@
#include "./src/lst/lst.h" #include "./src/lst/lst.h"
#include "./src/str/str.h" #include "./src/str/str.h"
#include "./src/tab/tab.h" #include "./src/tab/tab.h"
#include "./src/int/int.h"

6
src/int/int.h Normal file
View File

@ -0,0 +1,6 @@
#pragma once
#include <stdlib.h>
#include <string.h>
char* itoA(int n);

43
src/int/itoA.c Normal file
View File

@ -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);
}