-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcustom_print.c
113 lines (108 loc) · 2.33 KB
/
custom_print.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
#include "holberton.h"
#include <stdarg.h>
#include <stdlib.h>
/**
* print_r - prints the reversed string
* @args: string to be printed
* @buffer: space used for printing
* @buflen: buffer length
* @bufpos: current buffer index
* Return: numbers of chars written to buffer
**/
int print_r(va_list args, char buffer[], int *buflen, int *bufpos)
{
char *str;
int i, numChars;
i = numChars = 0;
str = va_arg(args, char *);
if (str == NULL)
str = "(nil)";
while (str[i] != '\0')
i++;
i--;
for (; i >= 0; i--, numChars++)
{
buffer[*bufpos] = str[i];
*bufpos += 1;
*buflen += 1;
if (*buflen == 1024)
write_buffer(buffer, buflen, bufpos);
}
return (numChars);
}
/**
* print_R - prints the rot13'ed string
* @args: string to be printed
* @buffer: space used for printing
* @buflen: buffer length
* @bufpos: current buffer index
* Return: numbers of chars written to buffer
**/
int print_R(va_list args, char buffer[], int *buflen, int *bufpos)
{
char *str, temp;
int i, numChars;
i = numChars = 0;
str = va_arg(args, char *);
if (str == NULL)
str = "(nil)";
while (str[i] != '\0')
{
temp = str[i];
if ((temp >= 'A' && temp <= 'M') ||
(temp >= 'a' && temp <= 'm'))
temp += 13;
else if ((temp >= 'N' && temp <= 'Z')
|| (temp >= 'n' && temp <= 'z'))
temp -= 13;
buffer[*bufpos] = temp;
*bufpos += 1;
*buflen += 1;
if (*buflen == 1024)
write_buffer(buffer, buflen, bufpos);
i++, numChars++;
}
return (numChars);
}
/**
* print_p - prints the address of a variable
* @args: number to be printed
* @buffer: space used for printing
* @buflen: buffer length
* @bufpos: current buffer index
* Return: numbers of chars written to buffer
**/
int print_p(va_list args, char buffer[], int *buflen, int *bufpos)
{
char *str;
int i, numChars;
size_t add;
add = (size_t)va_arg(args, void *);
str = size_tHex('x', add);
if (str == NULL)
return (0);
i = numChars = 0;
buffer[*bufpos] = '0';
numChars++;
*bufpos += 1;
*buflen += 1;
if (*buflen == 1024)
write_buffer(buffer, buflen, bufpos);
buffer[*bufpos] = 'x';
numChars++;
*bufpos += 1;
*buflen += 1;
if (*buflen == 1024)
write_buffer(buffer, buflen, bufpos);
while (str[i] != '\0')
{
buffer[*bufpos] = str[i];
*bufpos += 1;
*buflen += 1;
if (*buflen == 1024)
write_buffer(buffer, buflen, bufpos);
i++, numChars++;
}
free(str);
return (numChars);
}