-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_itoa.c
68 lines (61 loc) · 1.49 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nwatanab <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/09 01:45:55 by nwatanab #+# #+# */
/* Updated: 2020/11/24 01:13:35 by nwatanab ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_putnbr(char *p, int n, int len)
{
long num;
num = n;
p[--len] = '\0';
if (num == 0)
p[0] = 0 + '0';
if (num < 0)
{
num *= -1;
p[0] = '-';
}
while (num > 0)
{
p[--len] = num % 10 + '0';
num /= 10;
}
return (p);
}
int ft_len(int n)
{
int len;
long num;
num = n;
len = 0;
if (n == 0)
return (2);
if (num < 0)
{
num *= -1;
len++;
}
while (num > 0)
{
len++;
num /= 10;
}
return (len + 1);
}
char *ft_itoa(int n)
{
int len;
char *p;
len = ft_len(n);
p = ft_calloc(len, sizeof(char));
if (p == NULL)
return (NULL);
return (ft_putnbr(p, n, len));
}