forked from henrychris/printf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathother.c
More file actions
89 lines (84 loc) · 1.47 KB
/
Copy pathother.c
File metadata and controls
89 lines (84 loc) · 1.47 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include "main.h"
/**
* print_unsgn - prints an unsigned int
* @num: the unsigned int to be printed
* Return: number of characters printed
*/
int print_unsgn(unsigned int num)
{
char *str;
str = convert_ui_to_str(num);
if (str == NULL)
return (-1);
return (print_str(str));
}
/**
* convert_ui_to_str - converts a number to a string
* @num: the number to be converted
* Return: nothing
*/
char *convert_ui_to_str(unsigned int num)
{
int i, rem, len = 0, a = 0;
unsigned int digits = num;
char *str;
if (num != 0)
{
while (digits != 0)
{
len++;
digits /= 10;
}
str = malloc((digits + 1) * sizeof(char));
if (str == NULL)
return (NULL);
for (i = 0; i < len; i++)
{
rem = num % 10;
num = num / 10;
str[len - (i + 1)] = rem + '0';
}
str[len] = '\0';
if (a == 1)
str[0] = '-';
}
else if (num == 0)
{
str = malloc(2 * sizeof(char));
if (str == NULL)
return (NULL);
str[0] = 0 + '0';
str[1] = '\0';
}
return (str);
}
/**
* print_S - prints a string to stdout
* @str: the string to be printed
* to be increased after printing a character
* Return: void
*/
int print_S(char *str)
{
int j = 0, a = 0;
if (str == NULL)
str = "(null)";
while (str[j] != '\0')
{
if ((str[j] > 0 && str[j] < 32) || str[j] >= 127)
{
print_char(92);
print_char('x');
if (str[j] > 0 && str[j] < 16)
print_char(0 + '0');
print_hex(str[j], 'A');
a += 3;
j++;
} else
{
print_char(str[j]);
j++;
}
}
return (j + a);
}