-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.c
120 lines (101 loc) · 2.02 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
#include "main.h"
/**
* print_number - prints an integer
* @value: integer to be printed
* Return: number of digits printed
*/
int print_number(unsigned int value)
{
int power = 1, len = 0;
while ((value / power) > 9)
power *= 10;
while (power > 0)
{
len += _putchar('0' + value / power);
value %= power;
power /= 10;
}
return (len);
}
/**
* print_string - prints a string
* @str: string to be printed
* Return: number of characters printed
*/
int print_string(char *str)
{
int len = 0;
while (*str)
{
len += _putchar(*str);
str++;
}
return (len);
}
/**
* print_binary - prints a value in binary format, recursively
* @value: value to be printed
* Return: number of digits printed
*/
int print_binary(unsigned int value)
{
int count = 0;
if (value == 0)
{
return (0);
}
else
{
count += print_binary(value / 2);
count += _putchar((value % 2) + '0');
}
return (count);
}
/**
* print_hexadecimal - prints a value in hexadecimal format
* in upper case - always 2 characters
* @value: value to be printed
* @prefix: flag to indicate if '0' prefix should be printed, 0 = no, 1 = yes
* @caps: flag to indicate if upper case should be used, 0 = no, 1 = yes
* Return: number of digits printed
*/
int print_hexadecimal(unsigned long int value, int prefix, int caps)
{
int count = 0;
if (value < 16)
{
if (!prefix)
count += _putchar('0');
if (value < 10)
count += _putchar(value + '0');
else
count += caps ?
_putchar((value - 10) + 'A') : _putchar((value - 10) + 'a');
}
else
{
prefix = 1;
count += print_hexadecimal((value / 16), prefix, caps);
count += print_hexadecimal((value % 16), prefix, caps);
}
return (count);
}
/**
* print_octal - prints a value in octal format recursively
* @value: value to be printed
* Return: number of digits printed
*/
int print_octal(unsigned int value)
{
int count = 0;
if (value < 8)
{
count += _putchar(value + '0');
}
else
{
count += print_octal((value / 8));
count += _putchar((value % 8) + '0');
}
return (count);
}