-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
67 lines (61 loc) · 1.05 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
#include "libft.h"
/* Counts the number of digits of the integer. */
static int get_size(int long n, int sign)
{
int size;
size = 0;
while (n >= 10 || n <= -10)
{
n = n / 10;
size++;
}
if (sign == -1)
size += 2;
else if (sign == 1)
size += 1;
return (size);
}
/* Copies the digits of the integer into a string. */
static char *copy_digits(long b, int sign, int size)
{
char c;
int i;
char *str;
i = 0;
str = malloc(sizeof(char) * (size + 1));
if (str == NULL)
return (NULL);
if (b == 0)
str[0] = 0 + '0';
if (sign == -1)
str[0] = '-';
i = size - 1;
while (b > 0 || (b * -1) > 0)
{
c = b % 10 + '0';
str[i] = c;
i--;
b = b / 10;
}
str[size] = '\0';
return (str);
}
/* Allocates and returns a string of the converted integer received as paramter.
Returns the integer or NULL if the allocation fails. */
char *ft_itoa(int n)
{
int sign;
int size;
char *str;
long b;
sign = 1;
b = (long)n;
if (n < 0)
{
b = -b;
sign = -sign;
}
size = get_size((long)n, sign);
str = copy_digits(b, sign, size);
return (str);
}