-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
66 lines (59 loc) · 1.69 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
58
59
60
61
62
63
64
65
66
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: chris <chris@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/18 16:26:54 by cgodecke #+# #+# */
/* Updated: 2023/01/09 14:02:40 by chris ### ########.fr */
/* */
/* ************************************************************************** */
// This function ft_itoa converts an int to a string.
// Return: The string representing the integer. NULL if the allocation fails.
#include "libft.h"
static int c_digits(int n)
{
int i;
i = 0;
if (n == 0)
return (1);
while (n != 0)
{
n = n / 10;
i++;
}
return (i);
}
static void zero_or_neg(char *s, int n_ori, int digits)
{
if (n_ori == 0)
s[digits - 1] = 0 + '0';
if (n_ori < 0)
s[digits - 1] = '-';
}
char *ft_itoa(int n)
{
char *s;
int n_ori;
int digits;
n_ori = n;
digits = c_digits(n);
if (n < 0)
digits++;
s = (char *)malloc((digits + 1) * sizeof(char));
if (s == NULL)
return (NULL);
s[digits] = '\0';
while (n != 0)
{
if (n < 0)
s[digits - 1] = ((n % 10) * -1) + '0';
else
s[digits - 1] = (n % 10) + '0';
n = n / 10;
digits--;
}
zero_or_neg(s, n_ori, digits);
return (s);
}