-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_itoa.c
More file actions
68 lines (62 loc) · 1.59 KB
/
ft_itoa.c
File metadata and controls
68 lines (62 loc) · 1.59 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: uyilmaz <uyilmaz@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/11 17:41:12 by uyilmaz #+# #+# */
/* Updated: 2022/10/20 22:18:01 by uyilmaz ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int digit_finder(int a)
{
int result;
result = 0;
if (a == 0)
return (1);
else if (a == -2147483648)
return (11);
else if (a < 0)
{
result++;
a *= -1;
}
while (a > 9)
{
a /= 10;
result++;
}
return (++result);
}
void int_manup(int *a, char *result)
{
*a *= -1;
result[0] = '-';
}
char *ft_itoa(int n)
{
char *result;
int size;
size = digit_finder(n);
result = malloc(sizeof(char) * size + 1);
if (!result)
return (0);
result[size--] = '\0';
if (n == 0)
result[size--] = '0';
else if (n == -2147483648)
{
result[size--] = '8';
n = -214748364;
}
if (n < 0)
int_manup(&n, result);
while (n > 0)
{
result[size--] = (n % 10) + 48;
n /= 10;
}
return (result);
}