-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandle_rot13.c
More file actions
69 lines (56 loc) · 1.1 KB
/
handle_rot13.c
File metadata and controls
69 lines (56 loc) · 1.1 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
#include "main.h"
/**
* rot13ed - encodes a string using rot13
* @str: the pointer to the string to be encoded
*
* Return: a pointer to the encoded string
*/
char *rot13ed(char *str)
{
int i, j, k = 0, len = 0;
char *rot13_str;
char nm[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
char rt[] = "nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM";
while (str[len])
len++;
rot13_str = malloc(len + 1);
if (rot13_str == NULL)
return (NULL);
while (str[k])
{
rot13_str[k] = str[k];
k++;
}
rot13_str[k] = '\0';
for (i = 0; str[i] != '\0'; i++)
{
for (j = 0; j < 52; j++)
{
if (str[i] == nm[j])
{
rot13_str[i] = rt[j];
break;
}
}
}
return (rot13_str);
}
/**
* handle_rot13 - handles the specifier, 'R' which encodes a string in rot13
* @rot13: the variable argument(s) passed to _printf
*
* Return: the number of character(s) printed
*/
int handle_rot13(va_list rot13)
{
int count = 0;
char *rot13_str;
char *str = va_arg(rot13, char *);
rot13_str = rot13ed(str);
while (*rot13_str)
{
_putchar(*rot13_str++);
count++;
}
return (count);
}