-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_itoa.c
60 lines (55 loc) · 1.35 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: amezioun <amezioun@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/26 13:50:02 by amezioun #+# #+# */
/* Updated: 2024/01/10 20:16:01 by amezioun ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ncount(long n)
{
int i;
i = 0;
if (n == 0)
return (1);
if (n < 0)
{
i++;
n *= -1;
}
while (n > 0)
{
i++;
n /= 10;
}
return (i);
}
char *ft_itoa(int n)
{
char *s;
int len;
long nb;
len = ncount(n);
s = malloc(len + 1);
if (!s)
return (NULL);
nb = n;
s[len] = '\0';
if (nb < 0)
{
nb *= -1;
s[0] = '-';
}
if (nb == 0)
s[0] = '0';
while (--len >= 0 && nb)
{
s[len] = nb % 10 + '0';
nb /= 10;
}
return (s);
}