-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_itoa.c
72 lines (66 loc) · 1.54 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: akdemir <[email protected] +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/07/09 20:41:36 by akdemir #+# #+# */
/* Updated: 2023/07/18 16:13:37 by akdemir ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
size_t count_len(int n)
{
size_t i;
i = 0;
if (n == 0)
return (1);
if (n == -2147483648)
{
n = 147483648;
i = 2;
}
if (n < 0)
{
n *= -1;
i = 1;
}
while (n)
{
i++;
n /= 10;
}
return (i);
}
char *ft_itoa(int n)
{
char *str;
int bas;
bas = count_len(n);
str = (char *)ft_calloc(bas + 1, sizeof(char));
if (!str)
return (NULL);
if (n == 0)
*str = '0';
else if (n < 0)
{
if (n == -2147483648)
{
ft_strlcpy(str, "-2147483648", bas + 1);
return (str);
}
str[0] = '-';
n *= -1;
}
while (n != 0)
{
*(str + --bas) = (n % 10) + '0';
n = n / 10;
}
return (str);
}
int main()
{
printf("%d",ft_itoa(90));
}