-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_itoa.c
More file actions
53 lines (48 loc) · 1.39 KB
/
ft_itoa.c
File metadata and controls
53 lines (48 loc) · 1.39 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lilmende <lilmende@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/04 10:41:52 by lilmende #+# #+# */
/* Updated: 2023/11/04 11:26:52 by lilmende ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int count_digits(int n)
{
int count;
count = 0;
if (n <= 0)
count = 1;
while (n)
{
count++;
n /= 10;
}
return (count);
}
char *ft_itoa(int n)
{
char *str;
int count;
count = count_digits(n);
str = (char *)malloc(sizeof(char) * (count + 1));
if (!str)
return (NULL);
str[count] = '\0';
if (n == 0)
str[0] = '0';
if (n < 0)
str[0] = '-';
while (n)
{
if (n > 0)
str[--count] = '0' + (n % 10);
else
str[--count] = '0' + (n % 10 * -1);
n /= 10;
}
return (str);
}