-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
69 lines (62 loc) · 1.71 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
60
61
62
63
64
65
66
67
68
69
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dpylypen <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/29 20:54:55 by dpylypen #+# #+# */
/* Updated: 2016/12/02 18:29:06 by dpylypen ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static void ft_put_to_str(char *str, char c)
{
*str = c;
}
static char *ft_str_for_num(int nb)
{
int len;
char *str;
len = 1;
while (nb > 9 || nb < -9)
{
if (nb < 0 && len == 1)
len++;
if (nb > 9 || nb < -9)
{
nb = (nb - (nb % 10)) / 10;
len++;
}
}
str = (char *)ft_memalloc(len + 1);
if (!str)
return (0);
return (str);
}
static char *ft_num_to_str(int nb, char *str)
{
if (nb > 9 || nb < -9)
{
ft_num_to_str((nb - (nb % 10)) / 10, str);
nb -= (nb - (nb % 10));
if (nb < 0)
nb *= -1;
}
if (nb < 0)
{
ft_put_to_str(ft_strchr(str, 0), '-');
ft_put_to_str(ft_strchr(str, 0), (nb * -1) + '0');
}
else
ft_put_to_str(ft_strchr(str, 0), (nb) + '0');
return (str);
}
char *ft_itoa(int nb)
{
char *str;
str = ft_str_for_num(nb);
if (!str)
return (0);
return (ft_num_to_str(nb, str));
}