-
Notifications
You must be signed in to change notification settings - Fork 0
/
1-printf_ptr.c
executable file
·114 lines (101 loc) · 1.74 KB
/
1-printf_ptr.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
#include "main.h"
#include <stdarg.h>
/**
* print_ptr - print_base16_upper_lower
* @arg: va_list parameter
* Description: This function print address pointer
* in representation parameter for print hexadecimal format
* Return: address pointer
*/
int print_ptr(va_list arg)
{
unsigned long int dec, buffr;
char c[100];
int count, n, i;
dec = (unsigned long int)va_arg(arg, void*);
buffr = dec;
count = 1;
i = 0;
if (!dec)
{
_puts("(nil)");
return (5);
}
while (buffr)
{
buffr /= 16;
count++;
}
c[count + 1] = '\0';
while (dec > 0)
{
n = (dec % 16);
if (n >= 0 && n <= 9)
c[count] = ((char)(n + '0'));
else
c[count] = ((char)(n + 'W'));
count--;
dec /= 16;
}
c[0] = '0';
c[1] = 'x';
while (c[i] != '\0')
{
_putchar(c[i]);
i++;
}
return (i);
}
/**
* print_rot13 - prints a string using rot13
* @arg: list of arguments from _printf
* Return: length of the printed string
*/
int print_rot13(va_list arg)
{
register short i, j;
char rot13[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
char ROT13[] = "nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM";
char *s = va_arg(arg, char *);
if (!s)
{
return (-1);
}
for (j = 0; s[j]; j++)
{
if (s[j] < 'A' || (s[j] > 'Z' && s[j] < 'a') || s[j] > 'z')
_putchar(s[j]);
else
{
for (i = 0; i <= 52; i++)
if (s[j] == rot13[i])
_putchar(ROT13[i]);
}
}
return (j);
}
/**
* print_rev - prints a string in reverse
* @arg: argument from _printf
* if a flag is passed to _printf
* Return: length of the printed string
*/
int print_rev(va_list arg)
{
int i = 0;
int j;
char *s = va_arg(arg, char *);
if (!s)
{
return (-1);
}
while (s[i])
{
i++;
}
for (j = i - 1; j >= 0; j--)
{
_putchar(s[j]);
}
return (i);
}