-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_itoa.c
68 lines (62 loc) · 1.58 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: eablak <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/14 18:20:58 by eablak #+# #+# */
/* Updated: 2022/10/16 14:53:38 by eablak ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_check(long int n)
{
long int sayi;
int len;
sayi = n;
len = 0;
if (sayi <= 0)
{
sayi *= -1;
len++;
}
while (sayi > 0)
{
len++;
sayi /= 10;
}
return (len);
}
static void sayi_ata(char *dizi, int len, long int sayi)
{
while (sayi > 0)
{
dizi[--len] = (sayi % 10) + '0';
sayi /= 10;
}
}
char *ft_itoa(int n)
{
int len;
char *dizi;
long int sayi;
len = ft_check(n);
dizi = (char *)malloc(sizeof(char) * len + 1);
if (dizi == 0)
return (NULL);
sayi = n;
if (n == 0)
{
dizi[0] = '0';
dizi[1] = '\0';
return (dizi);
}
else if (n < 0)
sayi *= -1;
sayi_ata(dizi, len, sayi);
dizi[len] = '\0';
if (n < 0)
dizi[0] = '-';
return (dizi);
}