Repository navigation
Expand file tree
/
Copy pathft_itoa.c
More file actions
68 lines (61 loc) · 1.67 KB
/
Copy pathft_itoa.c
File metadata and controls
68 lines (61 loc) · 1.67 KB
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: smasatak <smasatak@student.42tokyo.jp> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/06/22 15:50:45 by smasatak #+# #+# */
/* Updated: 2026/06/22 17:46:42 by smasatak ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int count_digits(long nmb)
{
int count;
count = 0;
if (nmb <= 0)
count++;
while (nmb != 0)
{
nmb /= 10;
count++;
}
return (count);
}
char *ft_itoa(int n)
{
int count;
char *res;
long nmb;
nmb = n;
count = count_digits(nmb);
res = (char *)malloc(count + 1);
if (!res)
return (NULL);
res[count] = '\0';
if (nmb < 0)
nmb = -nmb;
if (nmb == 0)
res[0] = '0';
while (nmb != 0)
{
res[count - 1] = (nmb % 10) + 48;
nmb /= 10;
count--;
}
if (n < 0)
res[0] = '-';
return (res);
}
// #include <stdio.h>
// int main(void)
// {
// printf("Testing ft_itoa...\n");
// printf("%s\n", ft_itoa(42));
// printf("%s\n", ft_itoa(INT_MAX));
// printf("%s\n", ft_itoa(INT_MIN));
// printf("%s\n", ft_itoa(0));
// printf("%s\n", ft_itoa(-1));
// return (0);
// }