-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_itoa.c
56 lines (51 loc) · 1.36 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mbaypara <mbaypara@student.42kocaeli.co +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/12 15:34:47 by mbaypara #+# #+# */
/* Updated: 2023/12/20 13:52:59 by mbaypara ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
static int len_int(int n)
{
int len;
len = 0;
if (n <= 0)
len++;
while (n)
{
n = n / 10;
len++;
}
return (len);
}
char *ft_itoa(int n)
{
int len;
int i;
char *res;
i = 0;
len = len_int(n);
res = malloc(sizeof(char) * (len + 1));
if (!res)
return (NULL);
res[len] = '\0';
if (n < 0)
{
res[0] = '-';
i = 1;
}
while (len-- > i)
{
if (n < 0)
res[len] = '0' + n % 10 * (-1);
else
res[len] = '0' + n % 10;
n = n / 10;
}
return (res);
}