-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_itoa.c
More file actions
58 lines (53 loc) · 1.36 KB
/
Copy pathft_itoa.c
File metadata and controls
58 lines (53 loc) · 1.36 KB
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: tbenedic <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/06/14 10:01:12 by tbenedic #+# #+# */
/* Updated: 2018/06/14 14:56:13 by tbenedic ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_count_int(int n)
{
int i;
i = 1;
if (n < 0)
{
n = -n;
i++;
}
while (n >= 10)
{
n = n / 10;
i++;
}
return (i);
}
char *ft_itoa(int n)
{
int i;
int j;
char *num;
i = ft_count_int(n);
j = 0;
if (!(num = ft_strnew(i)))
return (NULL);
if (n == -2147483648)
return (ft_strdup("-2147483648"));
if (n < 0)
{
num[0] = '-';
j = 1;
n = -n;
}
while (i - 1 >= j)
{
num[i - 1] = n % 10 + 48;
n = n / 10;
i--;
}
return (num);
}