-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathft_itoa.c
59 lines (54 loc) · 1.38 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rlambert <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2014/11/05 16:04:21 by rlambert #+# #+# */
/* Updated: 2015/01/26 15:48:46 by rlambert ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_countchars(int n)
{
int i;
i = 0;
if (n == 0)
i++;
else if (n < 0)
i++;
else
{
while (n > 0)
{
i++;
n /= 10;
}
}
return (i);
}
char *ft_itoa(int n)
{
int i;
unsigned int x;
int sign;
char *buf;
buf = ft_strnew(ft_countchars(n));
x = n;
if ((sign = n) < 0)
x = -n;
i = 0;
if (x == 0)
buf[i++] = '0';
while (x > 0)
{
buf[i++] = x % 10 + '0';
x /= 10;
}
if (sign < 0)
buf[i++] = '-';
buf[i] = '\0';
ft_strrev(buf);
return (buf);
}