-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_itoa.c
More file actions
68 lines (60 loc) · 1.76 KB
/
ft_itoa.c
File metadata and controls
68 lines (60 loc) · 1.76 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: marvin <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/19 18:05:13 by marvin #+# #+# */
/* Updated: 2022/06/02 16:58:07 by marvin ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_len_sign(int number, int *nonpositive_ptr)
{
int len;
if (number <= 0)
*nonpositive_ptr = 1;
else
*nonpositive_ptr = 0;
len = 1;
while (number)
{
number /= 10;
len++;
}
return (len + *nonpositive_ptr);
}
static void ft_print_string(char *string, int last,
int number, int negative)
{
while (number)
{
if (negative)
string[last--] = 48 + ((number % 10) * (-1));
else
string[last--] = 48 + (number % 10);
number /= 10;
}
}
char *ft_itoa(int number)
{
int len;
int nonpositive;
char *string;
len = ft_len_sign(number, &nonpositive);
string = ft_calloc((size_t)(len), sizeof(char));
if (!(string))
return (NULL);
if (number == 0)
string[0] = '0';
if (number < 0)
string[0] = '-';
ft_print_string(string, (len - 2), number, nonpositive);
return (string);
}
/* int main()
{
printf(ft_itoa(-2147483648));
return 0;
}*/