-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_itoa.c
73 lines (66 loc) · 1.75 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
70
71
72
73
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lpires-n <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/10 07:09:30 by lpires-n #+# #+# */
/* Updated: 2022/06/11 18:10:13 by lpires-n ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int counter_number(int n)
{
int i;
i = 0;
if (n < 0 || n == 0)
{
n *= -1;
i++;
}
while (n > 0)
{
i++;
n = n / 10;
}
return (i);
}
static void number_for_string(int number, char *number_string)
{
int len;
int aux;
int i;
i = 0;
aux = 0;
len = counter_number(number);
if (number < 0)
{
number *= -1;
number_string[0] = '-';
i = 1;
}
while (len-- - i)
{
aux = number % 10;
number = number / 10;
number_string[len] = aux + '0';
}
}
char *ft_itoa(int n)
{
char *number_string;
int len_number;
len_number = counter_number(n);
if (n == -2147483648)
{
number_string = ft_strdup("-2147483648");
return (number_string);
}
number_string = malloc(sizeof(char) * (len_number + 1));
if (number_string == NULL)
return (NULL);
number_for_string(n, number_string);
number_string[len_number] = '\0';
return (number_string);
}