Skip to content

Commit 13972fe

Browse files
committed
Provision for rot13 print
1 parent c9218b6 commit 13972fe

File tree

4 files changed

+47
-2
lines changed

4 files changed

+47
-2
lines changed

_printf.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ int p_func(va_list ap, char specifier)
7777
case '%':
7878
char_count += write(1, "%", 1);
7979
break;
80+
case 'R':
81+
char_count += rot13(ap);
82+
break;
8083
}
8184
va_end(ap);
8285
return (char_count);

main.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#ifndef MAIN_H
22
#define MAIN_H
3+
#include <unistd.h>
34
#include <stdarg.h>
45
#include <string.h>
56
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
@@ -9,8 +10,8 @@ int _printf(const char *format, ...);
910
int print_char(va_list);
1011
int print_string(va_list);
1112
int p_func(va_list, char);
12-
void print_error(char *);
1313
int parse_format(const char *);
14+
int rot13(va_list);
1415

1516
void *alloc(size_t);
1617
char *int_to_string(long);

parser.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ int parse_format(const char *fmt)
2222
/* char fmt[i] does not denote allowed specifier */
2323
if (fmt[i + 1] != 'c' && fmt[i + 1] != 's' && fmt[i +
2424
1] != '%' && fmt[i + 1] != 'd' && fmt[i
25-
+ 1] != 'i')
25+
+ 1] != 'i' && fmt[i + 1] != 'R')
2626
{
2727
ret_val = 0;
2828
break;

print_conversions.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include "main.h"
2+
3+
/**
4+
* rot13 - prints the rotated position of a string
5+
* @ap: String to rotate
6+
* Description: Rotates each character of a string to
7+
* the next thirteenth position in the alphabet.
8+
* Return: char printed to stdout.
9+
*/
10+
int rot13(va_list ap)
11+
{ char *str = NULL;
12+
char c;
13+
int count = 0;
14+
15+
str = va_arg(ap, char *);
16+
while (*str)
17+
{
18+
if (*str >= 97 && *str <= 109) /* Rotates a - m to n - z */
19+
{
20+
c = *str + 13;
21+
count += write(1, &c, 1);
22+
}
23+
if (*str >= 110 && *str <= 122) /* Rotates n - z to a - m */
24+
{
25+
c = *str - 13;
26+
count += write(1, &c, 1);
27+
}
28+
if (*str >= 65 && *str <= 77) /* Rotates A - M to N - Z */
29+
{
30+
c = *str + 13;
31+
count += write(1, &c, 1);
32+
}
33+
if (*str >= 78 && *str <= 90) /* Rotates N - Z to A - M */
34+
{
35+
c = *str - 13;
36+
count += write(1, &c, 1);
37+
}
38+
*++str;
39+
}
40+
return (count);
41+
}

0 commit comments

Comments
 (0)