-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions1.c
83 lines (79 loc) · 1.29 KB
/
functions1.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
#include "main.h"
/**
* print_int - prints an integer
*
* @args: list of arguments
*
* Return: number of characters printed
*/
int print_int(va_list args)
{
int n = va_arg(args, int), len = 0, copy = n, negative = 0, size, i = 0;
char zero = '0', neg = '-';
char *num;
if (n == 0)
{
write(1, &zero, 1);
return (1);
}
if (n < 0)
{
if (n == -2147483648)
negative = 2, n++;
else
negative = 1;
len++, n = -n;
}
while (copy)
len++, copy /= 10;
if (negative)
size = len - 1;
else
size = len;
num = (char *)malloc(sizeof(char) * size);
if (num == NULL)
return (-1);
for (i = size - 1; i >= 0; i--)
num[i] = zero + (n % 10), n /= 10;
if (negative)
{
write(1, &neg, 1);
if (negative == 2)
num[size - 1] = '8';
}
for (i = 0; i < size; i++)
write(1, &num[i], 1);
free(num);
return (len);
}
/**
* print_rot13_str - ...
* @args: ...
*
* Return: length of string
*/
int print_rot13_str(va_list args)
{
int i = 0;
char *s = va_arg(args, char *);
char x;
if (s == NULL)
s = "(null)";
for (i = 0; s[i] != '\0'; i++)
{
if (s[i] >= 'a' && s[i] <= 'z')
{
x = s[i] - 'a';
x = 'a' + ((x + 13) % 26);
}
else if (s[i] >= 'A' && s[i] <= 'Z')
{
x = s[i] - 'A';
x = 'A' + ((x + 13) % 26);
}
else
x = s[i];
write(1, &x, 1);
}
return (i);
}