-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.c
131 lines (108 loc) · 1.71 KB
/
helpers.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include "main.h"
/**
* _reverse - reverses a string
* @s: string to reverse
* Return: length of string
*/
int _reverse(char s[])
{
int len;
int start;
int end;
char temp;
len = 0;
start = 0;
end = 0;
while (s[len] != '\0')
{
len++;
}
start = 0;
end = len - 1;
while (start < end)
{
temp = s[start];
s[start] = s[end];
s[end] = temp;
start++;
end--;
}
return (len);
}
/**
* _itoa - converts an integer to a string
* @n: integer to convert
* @s: string to convert to
* Return: length of string
*/
int _itoa(int n, char s[])
{
int i;
int sign;
int len;
i = 0;
sign = n;
if (sign < 0)
n = -n;
do {
s[i++] = n % 10 + '0';
} while ((n /= 10) > 0);
if (sign < 0)
{
s[i++] = '-';
}
s[i] = '\0';
len = _reverse(s);
return (len);
}
/**
* itoa_unsigned_int - converts an integer to a string
* @n: integer to convert
* @s: string to convert to
* Return: length of string
*/
void itoa_unsigned_int(unsigned int n, char s[])
{
int i;
i = 0;
do {
s[i++] = n % 10 + '0';
} while ((n /= 10) > 0);
s[i] = '\0';
_reverse(s);
}
/**
* itoa_hex - converts an integer to a string
* @n: integer to convert
* @s: string to convert to
* @uppercase: whether to use uppercase or lowercase letters
* Return: length of string
*/
void itoa_hex(unsigned int n, char s[], int uppercase)
{
int i;
unsigned int digit;
i = 0;
do {
digit = n % 16;
s[i++] = digit < 10 ? digit + '0' : digit - 10 + (uppercase ? 'A' : 'a');
} while ((n /= 16) > 0);
s[i] = '\0';
_reverse(s);
}
/**
* itoa_octal - converts an integer to a string
* @n: integer to convert
* @s: string to convert to
* Return: length of string
*/
void itoa_octal(unsigned int n, char s[])
{
int i;
i = 0;
do {
s[i++] = n % 8 + '0';
} while ((n /= 8) > 0);
s[i] = '\0';
_reverse(s);
}