-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
57 lines (52 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
55
56
57
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: amaarifa <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/03 09:15:30 by amaarifa #+# #+# */
/* Updated: 2021/11/03 11:34:03 by amaarifa ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stdlib.h>
int count_int_char(int n)
{
int i;
i = 0;
if (n < 0)
i++;
while (n)
{
n /= 10;
i++;
}
return (i);
}
char *ft_itoa(int n)
{
int s;
char *str;
long nb;
if (n == 0)
return (ft_strdup("0"));
s = count_int_char(n);
str = (char *)malloc(sizeof(char) * (s + 1));
if (!str)
return (0);
str[s] = '\0';
nb = n;
if (n < 0)
{
str[0] = '-';
nb *= -1;
}
while (nb > 0)
{
str[s - 1] = (nb % 10) + 48;
nb /= 10;
s--;
}
return (str);
}