-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
52 lines (47 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bpezeshk <bpezeshk@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/17 14:09:22 by bpezeshk #+# #+# */
/* Updated: 2017/01/19 13:03:35 by bpezeshk ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_itoa_len(int n)
{
int len;
if (n == 0)
return (1);
len = n < 0 ? 1 : 0;
while (n)
{
n /= 10;
len++;
}
return (len);
}
char *ft_itoa(int n)
{
char *a;
int len;
if (n == -2147483648)
return (ft_strdup("-2147483648"));
len = ft_itoa_len(n);
if ((a = ft_strnew(len)) == NULL)
return (NULL);
if (n <= 0)
{
*a = n < 0 ? '-' : '0';
n *= -1;
}
while (n)
{
len--;
a[len] = (n % 10) + '0';
n /= 10;
}
return (a);
}