-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.c
110 lines (94 loc) · 1.37 KB
/
functions.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
#include "main.h"
/**
* print_char - print char.
* @c: the char
*
* Return: len.
*/
int print_char(char c)
{
return (write(1, &c, 1));
}
/**
* print_string - print string.
* @s: the the string
*
* Return: len.
*/
int print_string(char *s)
{
return (write(1, s, strlen(s)));
}
/**
* print_number - print number
* @n: number to be printed
*
*/
void print_number(int n)
{
unsigned int n1;
if (n < 0)
{
n1 = -n;
print_char('-');
}
else
n1 = n;
if (n1 / 10)
print_number(n1 / 10);
print_char((n1 % 10) + '0');
}
/**
* num_length - return the number len
* @n: number to be printed
*
* Return: len.
*/
int num_length(int n)
{
int len = 1;
if (n < 0)
len++;
while (n /= 10)
len++;
return (len);
}
/**
* checker - print argument
* @c: the checker
* @args: the arguments
*
* Return: len.
*/
int checker(char c, va_list args)
{
int num, len = 0;
char *str;
switch (c)
{
case 'c':
len += print_char(va_arg(args, int));
break;
case 's':
str = va_arg(args, char *);
len += print_string(STRING);
break;
case 'd':
case 'i':
num = va_arg(args, int);
print_number(num);
len += num_length(num);
break;
case 'b':
len += print_binary(va_arg(args, unsigned int));
break;
case '%':
len += print_char('%');
break;
default:
len += print_char('%');
len += print_char(c);
break;
}
return (len);
}