-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_itoa.c
58 lines (53 loc) · 1.34 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: edelangh <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2014/11/05 11:02:35 by edelangh #+# #+# */
/* Updated: 2014/11/12 10:46:01 by edelangh ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_int_len(int n)
{
int len;
len = 0;
if (n == 0)
return (1);
if (n < 0)
{
++len;
n *= -1;
}
while (n)
{
++len;
n /= 10;
}
return (len);
}
char *ft_itoa(int n)
{
char *s;
int len;
if (n == -2147483648)
return (ft_strdup("-2147483648"));
len = ft_int_len(n);
s = ft_strnew(len);
if (n == 0)
s[0] = '0';
if (n < 0)
{
n *= -1;
s[0] = '-';
}
while (n)
{
--len;
s[len] = '0' + n % 10;
n /= 10;
}
return (s);
}