-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathft_itoa.c
69 lines (63 loc) · 1.55 KB
/
ft_itoa.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sel-mlil <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/30 04:40:49 by sel-mlil #+# #+# */
/* Updated: 2024/10/30 04:40:50 by sel-mlil ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
static int nb_nbr(long int n)
{
int cp;
unsigned int pn;
cp = 1;
if (n < 0)
{
cp++;
pn = (unsigned int)(n * -1);
}
else
pn = (unsigned int)(n);
while (pn / 10)
{
pn /= 10;
cp++;
}
return (cp);
}
static char *filling(char *str, long int nb, int len)
{
str[len] = '\0';
if (nb == 0)
{
str[0] = '0';
return (str);
}
if (nb < 0)
{
str[0] = '-';
nb = -nb;
}
while (nb > 0)
{
str[--len] = (nb % 10) + '0';
nb /= 10;
}
return (str);
}
char *ft_itoa(int n)
{
long int nb;
int len;
char *str;
nb = (long)n;
len = nb_nbr(nb);
str = (char *)malloc(len + 1);
if (!str)
return (NULL);
return (filling(str, nb, len));
}