-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
58 lines (53 loc) · 1.48 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: radan <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/22 16:44:30 by radan #+# #+# */
/* Updated: 2021/10/22 16:44:32 by radan ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int check_size(int n)
{
int count;
long res;
res = n;
count = 1;
if (res < 0)
{
count++;
res *= (-1);
}
while (res >= 10)
{
count++;
res /= 10;
}
return (count);
}
char *ft_itoa(int n)
{
char *array;
long res;
int i;
i = 0;
array = (char *)malloc(sizeof(char) * check_size(n) + 1);
if (!array)
return (NULL);
if (n < 0)
array[0] = '-';
res = n;
if (res < 0)
res *= (-1);
array[check_size(n) - i++] = '\0';
while (res >= 10)
{
array[check_size(n) - i++] = (res % 10) + '0';
res /= 10;
}
array[check_size(n) - i++] = (res % 10) + '0';
return (array);
}