-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprint_char.c
92 lines (82 loc) · 1.63 KB
/
print_char.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
#include "main.h"
/**
* print_char - print a character using putchar.
* @ch: charachter.
* Return: 1 on sccess.
*/
int print_char(char ch)
{
putchar(ch);
return (1);
}
/**
* print_numbers - print d,i and unsigned i od numbers.
* @str: char.
* @list: it is variable of type va_list.
* Return: integer that reflect the nember of printed characters (digits).
*/
int print_numbers(va_list list, char str)
{
int len_pr = 0;
if (str == 'd')
len_pr += print_number(va_arg(list, int)), va_end(list);
if (str == 'i')
len_pr += print_number(va_arg(list, int)), va_end(list);
else if (str == 'u')
len_pr += print_unumber(va_arg(list, unsigned int)), va_end(list);
return (len_pr);
}
/**
* dec_to_bin - this function convert decimal to binary
* @n: the decimal number
* Return: it return the length of the binary number
*/
int dec_to_bin(unsigned int n)
{
int i = 0, j;
unsigned int tmp = n;
char *binary;
if (n == 0)
{
putchar('0');
return (0);
}
while (tmp > 0)
{
i++;
tmp /= 2;
}
binary = malloc(sizeof(char) * (i + 1));
if (binary == NULL)
return (-1);
for (j = i - 1; j >= 0; j--)
{
*(binary + j) = (n % 2) + '0';
n /= 2;
}
binary[i] = '\0';
for (j = 0; j < i; j++)
putchar(binary[j]);
free(binary);
return (i - 1);
}
/**
* it_spec - check if a char entred is a one of specifiers that came after %.
* @s: char.
* Return: return the lenght of the given string s.
*/
int it_spec(char s)
{
int i = 0;
if (s == 'c' || s == 'd' || s == 'e')
i++;
else if (s == 'f' || s == 'g' || s == 'i')
i++;
else if (s == 'o' || s == 's' || s == 'u')
i++;
else if (s == 'x')
i++;
else
i = 0;
return (i);
}