-
Notifications
You must be signed in to change notification settings - Fork 0
/
3-custom.c
119 lines (104 loc) · 1.76 KB
/
3-custom.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
115
116
117
118
119
#include "main.h"
/**
* print_binary - Prints an unsigned integer as binary.
* @num: The unsigned integer to be printed.
*
* Return: The number of characters printed.
*/
int print_binary(unsigned int num)
{
int i = 0;
int arr[32];
int j = 0;
if (num == 0)
{
_putchar('0');
return (1);
}
while (num > 0)
{
arr[j] = num % 2;
num /= 2;
j++;
}
for (i = j - 1; i >= 0; i--)
{
_putchar(arr[i] + '0');
}
return (j);
}
/**
* print_S - Prints a string with special handling for S
* @str: The string to be printed.
*
* Return: The number of characters printed (excluding the null byte).
*/
int print_S(const char *str)
{
int i = 0, count = 0;
if (str == NULL)
{
return (print_S("(null)"));
}
while (str[i])
{
if (str[i] >= 32 && str[i] < 127)
{
_putchar(str[i]);
count++;
}
else
{
_putchar('\\');
_putchar('x');
if (str[i] < 16)
_putchar('0');
count += 3;
count += print_hexa(str[i], 1);
}
i++;
}
return (count);
}
/**
* reversed - Prints a reversed string.
* @str: The string to be printed in reverse.
*
* Return: The number of characters printed.
*/
int reversed(const char *str)
{
int j = 0, i;
while (str[j])
j++;
for (i = j - 1; i >= 0; i--)
_putchar(str[i]);
return (j);
}
/**
* _rot13 - Prints a ROT13-encrypted string.
* @str: The string to be printed.
*
* Return: The number of characters printed.
*/
int _rot13(const char *str)
{
int i = 0;
char chars;
if (str == NULL)
return (print_string("(null)"));
while (str[i])
{
chars = str[i];
if ((chars >= 'a' && chars <= 'z') || (chars >= 'A' && chars <= 'Z'))
{
if ((chars >= 'a' && chars <= 'm') || (chars >= 'A' && chars <= 'M'))
chars += 13;
else
chars -= 13;
}
_putchar(chars);
i++;
}
return (i);
}