-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
85 lines (74 loc) · 1.97 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
74
75
76
77
78
79
80
81
82
83
84
85
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jgarlic <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/02 23:20:04 by jgarlic #+# #+# */
/* Updated: 2021/12/02 23:20:05 by jgarlic ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
static char *my_null_func(void)
{
char *str;
str = malloc(sizeof(char) * 2);
if (str != 0)
{
str[0] = '0';
str[1] = '\0';
return (str);
}
return (0);
}
static void my_gen_str(char *str, long long size, long long x, long long n)
{
long long int i;
i = 0;
while (i < (long long)size)
{
x = x / 10;
str[i] = n / x % 10 + '0';
i++;
}
str[i] = '\0';
}
static char *my_negative_number(long long size, long long n, long long x)
{
char *str;
str = malloc(sizeof(char) * (size + 2));
if (str == 0)
return (0);
str[0] = '-';
n = n * -1;
my_gen_str(str + 1, size, x, n);
return (str);
}
static char *my_positive_number(long long size, long long n, long long x)
{
char *str;
str = malloc(sizeof(char) * (size + 1));
if (str == 0)
return (0);
my_gen_str(str, size, x, n);
return (str);
}
char *ft_itoa(int n)
{
long long int x;
long long int size;
size = 0;
x = 1;
while (n / x != 0)
{
x = x * 10;
size++;
}
if (n == 0)
return (my_null_func());
if (n < 0)
return (my_negative_number(size, n, x));
else
return (my_positive_number(size, n, x));
}