-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprint_f2.c
More file actions
108 lines (92 loc) · 1.82 KB
/
print_f2.c
File metadata and controls
108 lines (92 loc) · 1.82 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
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
#include "main.h"
/**
* print_unsigned_number - Print an unsigned integer.
* @n: The unsigned integer to be printed.
*
* Return: The number of characters printed.
*/
int print_unsigned_number(unsigned int n)
{
int count = 0;
if (n >= 10)
{
count += print_unsigned_number(n / 10);
}
_putchar(n % 10 + '0');
count++;
return (count);
}
/**
* print_octal - Print an unsigned integer in octal format.
* @n: The unsigned integer to be printed in octal.
*
* Return: The number of characters printed.
*/
int print_octal(unsigned int n)
{
int count = 0;
if (n >= 8)
{
count += print_octal(n / 8);
}
_putchar(n % 8 + '0');
count++;
return (count);
}
/**
* print_hexadecimal - Print an unsigned integer in hexadecimal format.
* @n: The unsigned integer to be printed in hexadecimal.
* @uppercase: Whether to use uppercase letters (1) or not (0).
*
* Return: The number of characters printed.
*/
int print_hexadecimal(unsigned int n, int uppercase)
{
int count = 0;
char hexDigits[] = "0123456789abcdef";
if (uppercase)
{
hexDigits[10] = 'A';
hexDigits[11] = 'B';
hexDigits[12] = 'C';
hexDigits[13] = 'D';
hexDigits[14] = 'E';
hexDigits[15] = 'F';
}
if (n >= 16)
{
count += print_hexadecimal(n / 16, uppercase);
}
_putchar(hexDigits[n % 16]);
count++;
return (count);
}
/**
* print_npc - Prints a string with custom formatting.
* @str: The string to be printed.
*
* Return: The number of characters printed.
*/
int print_npc(char *str)
{
int i;
int count;
count = 0;
i = 0;
while (str[i] != '\0')
{
if ((str[i] > 0 && str[i] < 32) || str[i] >= 127)
{
count += print_string("\\x");
if (str[i] < 0x10)
count += print_hexadecimal(0, 1);
count += print_hexadecimal((unsigned int)str[i], 2);
i++;
}
else
{
count += _putchar(str[i++]);
}
}
return (count);
}