-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
54 lines (49 loc) · 1.37 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ariahi <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/27 21:32:17 by ariahi #+# #+# */
/* Updated: 2021/11/27 21:32:20 by ariahi ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int count_size(int n)
{
int size;
size = 0;
if (n == 0)
return (1);
while (n != 0 && ++size)
n /= 10;
return (size);
}
char *ft_itoa(int n)
{
char *str;
int i;
int sign;
sign = 1;
i = count_size(n);
if (n < 0)
{
i++;
sign = -1;
}
str = (char *)malloc(sizeof(char) * (i + 1));
if (!str)
return (NULL);
str[i] = '\0';
if (n < 0)
str[0] = '-';
if (n == 0)
str[0] = '0';
while (n != 0)
{
str[--i] = (n % 10) * sign + 48;
n /= 10;
}
return (str);
}