-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprint_functions.c
61 lines (52 loc) · 1.2 KB
/
print_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
#include "holberton.h"
/*** PRINT WITOUTH USING PRINF AND ADDING NEWLINE AS NEEDED ***/
/**
* print_str - Prints a string character by character.
* @str: String to be printed. If the string is NULL it will print (null)
* @new_line: If integer is 0 a new line will be printed. Otherwise a new line
* will not be printed.
*/
void print_str(char *str, int new_line)
{
int i;
if (str == NULL)
str = "(null)";
for (i = 0; str[i] != '\0'; i++)
write(STDOUT_FILENO, &str[i], 1);
if (new_line == 0)
write(STDOUT_FILENO, "\n", 1);
}
/*** PRINT A SINGLE LETTER ***/
/**
* _write_char - Writes a character to stdout
* @c: Character that will be written to stdout
* Return: Upon success how many characters were written.
*/
int _write_char(char c)
{
return (write(1, &c, 1));
}
/*** PRINT EVERY DIGIT OF A NUMBER AS SINGLE CHARS ***/
/**
* print_number - Prints an unsigned number
* @n: unsigned integer to be printed
* Return: The amount of numbers printed
*/
int print_number(int n)
{
int div;
int len;
unsigned int num;
div = 1;
len = 0;
num = n;
for (; num / div > 9; )
div *= 10;
for (; div != 0; )
{
len += _write_char('0' + num / div);
num %= div;
div /= 10;
}
return (len);
}