-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_itoa.c
68 lines (62 loc) · 1.57 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ogenc <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/21 18:10:32 by ogenc #+# #+# */
/* Updated: 2023/01/02 15:05:17 by ogenc ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int numlen(int num)
{
int count;
count = 0;
if (num < 0 || num == 0)
count++;
while (num)
{
num /= 10;
count++;
}
return (count);
}
char *itoanext(char *number, int n, int len)
{
if (number[len - 1] == '8')
{
len = len - 1;
}
while (n)
{
number[len-- -1] = (n % 10) + '0';
n /= 10;
}
return (number);
}
char *ft_itoa(int n)
{
char *number;
int len;
len = numlen(n);
number = malloc (1 * len + 1);
if (!number)
return (NULL);
if (n == 0)
number[0] = '0';
else if (n < 0)
{
if (n == -2147483648)
{
number[len - 1] = '8';
n /= 10;
}
number[0] = '-';
n = -(n);
}
number[len] = '\0';
itoanext(number, n, len);
return (number);
}