-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandle_customs_specifiers.c
103 lines (94 loc) · 1.61 KB
/
handle_customs_specifiers.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
#include "main.h"
#include <stdio.h>
#include <unistd.h>
/**
* write_custom_S - Acustom specifier
* @str: Pointer to string
* Return: Number of character printed
*/
int write_custom_S(char *str)
{
int count;
while (*str != '\0')
{
if (*str < 32 || *str >= 127)
{
char hex[5];
snprintf(hex, sizeof(hex), "\\x%02X", (unsigned char)*str);
write(1, hex, 4);
count += 4;
} else
{
write(1, str, 1);
count++;
}
str++;
}
return (count);
}
/**
* write_pointer - Print a pointer value
* @p: Pointer
* Return: Number of character printed
*/
int write_pointer(void *p)
{
char buffer[20];
int len;
len = snprintf(buffer, sizeof(buffer), "%p", p);
write(1, buffer, len);
return (len);
}
/**
* write_rot13 - Print a string in Rot13
* @str: Pointer to string
* Return: Number of character printed
*/
int write_rot13(char *str)
{
int i, j, count = 0;
char c;
char input[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
char output[] = "NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm";
for (i = 0; str[i] != '\0'; i++)
{
for (j = 0; input[j] != '\0'; j++)
{
if (str[i] == input[j])
{
c = output[j];
write(1, &c, 1);
count++;
break;
}
}
if (!input[j])
{
c = str[i];
write(1, &c, 1);
count++;
}
}
return (count);
}
/**
* write_reverse - Write a string in reverse
* @str: Pointer to the string
* Return: Number of character printed
*/
int write_reverse(char *str)
{
int count = 0, len = 0;
while (str[len] != '\0')
{
len++;
}
len--;
while (len >= 0)
{
write(1, &str[len], 1);
len--;
count++;
}
return (count);
}