-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprint_conversions.c
More file actions
142 lines (122 loc) · 2.79 KB
/
print_conversions.c
File metadata and controls
142 lines (122 loc) · 2.79 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include "main.h"
#include <stdlib.h>
/**
* rot13 - prints the rotated position of a string
* @ap: String to rotate
* Description: Rotates each character of a string to
* the next thirteenth position in the alphabet.
* Return: char printed to stdout.
*/
int rot13(va_list ap)
{ char *str = NULL;
char c;
int count = 0;
str = va_arg(ap, char *);
while (*str)
{
if ((*str >= 65 && *str <= 77) || (*str >= 97 && *str <= 109))
{
c = *str + 13;
count += write(1, &c, 1);
}
else if ((*str >= 78 && *str <= 90) || (*str >= 110 && *str <= 122))
{
c = *str - 13;
count += write(1, &c, 1);
}
else
{
c = *str;
count += write(1, &c, 1);
}
++str;
}
return (count);
}
/**
* print_rev - print a string in reverse
* @ap: variadic argument
*
* Return: Number of char printed to stdout
*/
int print_rev(va_list ap)
{
int len, i, count = 0;
char *str = va_arg(ap, char *);
len = strlen(str);
if (len == 0)
return (0);
for (i = len - 1; i >= 0; i--)
count += write(1, &str[i], 1);
return (count);
}
/**
* print_binary - Auxilliary Function
*
* Description: This function handles the binary specifier %b which converts
* an uncsigned int into binary
*
* @ap: Argument Pointer
*
* Return: (count) int
*/
int print_binary(va_list ap)
{
int i;
unsigned int count = 0, length = 1, num = va_arg(ap, unsigned int);
char *num_string, *buffer;
long tmp = num;
if (num == 0)
{
count += write(1, "0", 1);
return (count);
}
while (num)
{
num /= 2;
length++;
}
buffer = (char *) alloc(length * sizeof(char));
buffer[length - 1] = '\0';
num_string = print_bin_helper(tmp, buffer);
--num_string;
for (i = length - 2; i >= 0; --i)
count += write(1, &num_string[i], 1);
free(buffer);
return (count);
}
int print_hexa_upper(va_list ap)
{
int i, j;
unsigned int k, count = 0, length = 1, num = va_arg(ap, unsigned int);
char *hex_string, *syms = "0123456789ABCDEF";
long tmp = num;
if (num == 0)
{
count += write(1, "0", 1);
return (count);
}
while (num)
{
num /= 16;
length++;
}
hex_string = (char *) alloc(length * sizeof(char));
for (j = length - 2; j >= 0; --j)
{
for (i = 0; i < 16; ++i)
{
if (i == tmp % 16)
{
hex_string[j] = syms[i];
tmp /= 16;
break;
}
}
}
hex_string[length - 1] = '\0';
for (k = 0; k < length - 1; ++k)
count += write(1, &hex_string[k], 1);
free(hex_string);
return (count);
}