-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_length_and_itoa.c
108 lines (97 loc) · 2.04 KB
/
ft_length_and_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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_length_and_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jfreitas <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/08/14 17:03:05 by jfreitas #+# #+# */
/* Updated: 2020/02/27 11:36:10 by jfreitas ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_intlen_base(unsigned long n, int base)
{
int len;
len = 0;
if (n < 0)
n *= -1;
if (n == 0)
len = 1;
while (n)
{
n = n / base;
len++;
}
return (len);
}
int ft_intlen_positive(int n)
{
int len;
len = 0;
if ((unsigned int)n == 2147483648 || (long)n == -2147483648)
{
len = 10;
return (len);
}
if (n < 0)
n *= -1;
if (n == 0)
len = 1;
while (n > 0)
{
n = n / 10;
len++;
}
return (len);
}
size_t ft_strlen(char const *s)
{
size_t len;
len = 0;
while (*s && s)
{
len++;
s++;
}
return (len);
}
static char ft_calculate_char(int res, char c)
{
char ret_char;
ret_char = '0';
while (res)
{
ret_char++;
if (ret_char == ':')
ret_char = c;
res--;
}
return (ret_char);
}
char *ft_itoa_base(unsigned long n, int base, char c)
{
unsigned long nb;
int res;
int n_len;
char *str;
if (n == 0)
{
if (!(str = ft_strnew(1)))
return (NULL);
*str = '0';
return (str);
}
nb = n;
n_len = ft_intlen_base(n, base);
if (!(str = ft_strnew(n_len)))
return (NULL);
while (nb)
{
res = nb % base;
nb /= base;
str[n_len - 1] = ft_calculate_char(res, c);
n_len--;
}
return (str);
}