-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_itoa.c
55 lines (50 loc) · 1.42 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rnbou <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/10/11 21:02:44 by rnbou #+# #+# */
/* Updated: 2018/10/17 19:50:22 by rnbou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_cnt(int n)
{
int nbr;
if (n == 0)
return (1);
nbr = 0;
while (n != 0)
{
n = n / 10;
nbr++;
}
return (nbr);
}
char *ft_itoa(int n)
{
char *str;
int l;
int i;
unsigned int k;
i = 1 + 1 * (n < 0);
if (n < 0)
k = -1 * n;
else
k = n;
l = ft_cnt(k);
str = (char *)malloc(sizeof(char) * (l + i));
str[l-- + i - 1] = '\0';
str[l-- + i - 1] = '0' + k % 10;
k = k / 10;
while (k != 0)
{
str[l-- + i - 1] = '0' + k % 10;
k = k / 10;
}
if (i == 2)
str[l + i - 1] = '-';
return (str);
}