-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom_functions.c
91 lines (83 loc) · 1.94 KB
/
custom_functions.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
#include "printf.h"
int print_binary(va_list ap, const fields_t *fields)
{
(void)fields;
return print_binary_rec(va_arg(ap, int), 0);
}
int print_binary_rec(unsigned int x, unsigned int len)
{
if (x > 1)
len += print_binary_rec(x / 2, len);
return len + _putchar((x % 2) + '0');
}
/**
* print_S - Print a string and convert any non-printable character to their
* hexadecimal value.
* @ap: argument pointer
* @fields: pointer to a struct fields
*
* Return: The number of characters printed
*/
int print_S(va_list ap, const fields_t *fields)
{
(void)fields;
char *sval = va_arg(ap, char *);
int len = 0, offset;
for (; *sval; ++sval) {
/* Convert non printable chars to hex */
if (*sval > 0 && (*sval < 32 || *sval >= 127)) {
_puts_without_newline("\\x");
if (*sval <= 15)
_putchar('0');
offset = (*sval < 10) ? 0 : 'A' - ':';
_putchar(*sval + '0' + offset);
len += 4;
continue;
}
len += _putchar(*sval);
}
return len;
}
/**
* print_rev - Print a string, in reverse.
* @ap: argument pointer
* @fields: pointer to a struct fields
*
* Return: the number of characters printed
*/
int print_rev(va_list ap, const fields_t *fields)
{
(void)fields;
char *sp = va_arg(ap, char *);
int len, i;
len = i = _strlen(sp);
while (i--)
_putchar(*(sp + i));
return len;
}
/**
* print_rot13 - print the rot13'ed string
* @ap: argument pointer
* @fields: pointer to a struct fields
*
* Return: The number of characters printed
*/
int print_rot13(va_list ap, const fields_t *fields)
{
(void)fields;
const char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
const char mirror[] = "NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm";
const char *sp = va_arg(ap, char *);
unsigned int i, j;
for (i = 0; sp[i]; ++i) {
for (j = 0; alphabet[j]; ++j) {
if (alphabet[j] == sp[i]) {
_putchar(mirror[j]);
break;
}
}
if (j == sizeof(alphabet) - 1)
_putchar(sp[i]);
}
return i;
}