-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_itoa.c
More file actions
76 lines (70 loc) · 1.65 KB
/
ft_itoa.c
File metadata and controls
76 lines (70 loc) · 1.65 KB
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
/* ************************************************************************** */
/* */
/* :::::::: */
/* ft_itoa.c :+: :+: */
/* +:+ */
/* By: mgraaf <mgraaf@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2021/12/16 14:21:55 by mgraaf #+# #+# */
/* Updated: 2021/12/16 14:21:59 by mgraaf ######## odam.nl */
/* */
/* ************************************************************************** */
#include "libft.h"
int determine_length(long long n, int neg_or_pos)
{
int i;
i = 0;
if (n == 0)
i++;
while (n > 0)
{
n = n / 10;
i++;
}
if (neg_or_pos)
i++;
return (i);
}
char *fill_arr(long long n, char *arr, int i, int neg_or_pos)
{
arr[i] = '\0';
i--;
if (n == 0)
{
arr[i] = '0';
i--;
}
while (n > 0)
{
arr[i] = (n % 10) + '0';
i--;
n = n / 10;
}
if (neg_or_pos)
{
arr[i] = '-';
i++;
}
return (arr);
}
char *ft_itoa(int n)
{
int i;
int neg_or_pos;
char *arr;
long long j;
neg_or_pos = 0;
if (n < 0)
{
neg_or_pos = 1;
j = (long long)n * -1;
}
else
j = (long long)n;
i = determine_length(j, neg_or_pos);
arr = malloc(i * sizeof(char) + 1);
if (!arr)
return (0);
arr = fill_arr(j, arr, i, neg_or_pos);
return (arr);
}