-
Notifications
You must be signed in to change notification settings - Fork 1
/
101-pint_rev-rot13.c
95 lines (88 loc) · 2.22 KB
/
101-pint_rev-rot13.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
#include "main.h"
/**
* print_rev - Prints a string in reverse
* @arg: List of arguments passed to function
* @buffer: Buffer array used to handle printing
* @flags: Calculates active flags (not used in this function)
* @width: Specifies minimum width of printed field (not used too)
* @precision: Specifies maximum number of characters to print (not used too)
* @size: Size specifier (not used too)
* Return: Number of characters printed
*/
int print_rev(va_list arg, char buffer[], int flags,
int width, int precision, int size)
{
char *str;
int i, count = 0;
UNUSED(buffer);
UNUSED(flags);
UNUSED(width);
UNUSED(size);
/* Get string from arguments */
str = va_arg(arg, char *);
if (str == NULL)
{
UNUSED(precision);
str = ")Null(";
}
/* Iterate through string backwards and print each character */
for (i = 0; str[i]; i++)
;
for (i = i - 1; i >= 0; i--)
{
char z = str[i];
write(1, &z, 1);
count++;
}
return (count);
}
/**
* print_rot13 - Prints a string in ROT13 encryption.
* @arg: List of arguments passed to function
* @buffer: Buffer array used to handle printing
* @flags: Calculates active flags (not used in this function)
* @width: Specifies minimum width of printed field (not used in this function)
* @precision: Specifies maximum number of characters to print (not used too)
* @size: Size specifier (not used in this function)
* Return: Number of characters printed
*/
int print_rot13(va_list arg, char buffer[], int flags,
int width, int precision, int size)
{
unsigned int i, j;
int count = 0;
char *string;
char x;
/* Iterate through string and apply ROT13 encryption */
char original[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
char hashed[] = "NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm";
/* Get string from arguments */
string = va_arg(arg, char *);
UNUSED(buffer);
UNUSED(flags);
UNUSED(width);
UNUSED(precision);
UNUSED(size);
if (string == NULL)
string = "(AHYY)";
for (i = 0; string[i]; i++)
{
for (j = 0; original[j]; j++)
{
if (original[j] == string[i])
{
x = hashed[j];
write(1, &x, 1);
count++;
break;
}
}
if (!original[j])
{
x = string[i];
write(1, &x, 1);
count++;
}
}
return (count);
}