-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_itoa.c
60 lines (55 loc) · 1.39 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: mmita <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/21 15:14:55 by mmita #+# #+# */
/* Updated: 2022/11/21 15:43:53 by mmita ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int len(long nl)
{
int l;
l = 0;
if (nl == 0)
l = 1;
else if (nl < 0)
{
nl = -nl;
l++;
}
while (nl > 0)
{
nl = nl / 10;
l++;
}
return (l);
}
char *ft_itoa(int n)
{
char *str;
int i;
long nl;
nl = n;
i = len(nl);
str = (char *)malloc (sizeof(char) * (len(nl) + 1));
if (!str)
return (NULL);
str[i--] = '\0';
if (nl == 0)
str[0] = '0';
if (nl < 0)
{
str[0] = '-';
nl = -nl;
}
while (nl > 0)
{
str[i--] = 48 + (nl % 10);
nl = nl / 10;
}
return (str);
}