Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion _printf.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ int _printf(const char *format, ...)
}

va_start(arg_p, format);

while (*format)
{
if (*format != '%')
{
write(1, format, 1);
_putchar(*format);
char_printed++;
}

Expand Down
1 change: 1 addition & 0 deletions get_printer.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ int (*get_printer(char specifier))(va_list args)
{'o', print_octal},
{'x', print_hex_x},
{'X', print_hex_X},
{'S', print_custom_string},
};

for (i = 0; i < sizeof(printers) / sizeof(printers[0]); i++)
Expand Down
2 changes: 2 additions & 0 deletions main.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ int _putchar(char c);
int _print_digit(int num);
int _print_string(char *str);
int _print_number(unsigned long n);
int _print_hex_X(unsigned int num);

/* Printers */
int print_char(va_list args);
Expand All @@ -40,6 +41,7 @@ int print_percentage(va_list args);
int print_octal(va_list args);
int print_hex_x(va_list args);
int print_hex_X(va_list args);
int print_custom_string(va_list args);

int _printf(const char *format, ...);
int (*get_printer(char specifier))(va_list args);
Expand Down
45 changes: 24 additions & 21 deletions printers2.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,33 +97,36 @@ int print_hex_x(va_list args)
*/
int print_hex_X(va_list args)
{
int count = 0, i = 0, hex[32];
unsigned int num = va_arg(args, unsigned int);

if (num == 0)
{
_putchar('0');
count++;
}
else
return (_print_hex_X(num));
}

/**
* print_custom_string - Prints the non printable chars in hex format.
* @args: A va_list containing a character
*
* Return: The count of characters printed.
*/
int print_custom_string(va_list args)
{
int count = 0;
char *str = va_arg(args, char *);

while (*str != '\0')
{
while (num > 0)
if ((*str > 0 && *str < 32) || *str >= 127)
{
hex[i] = num % 16;
num = num / 16;
i++;
}
count += _print_string("\\x");
if (*str < 16)
count += _putchar('0');

for (i = i - 1; i >= 0; i--)
{
if (hex[i] < 10)
_putchar(hex[i] + '0');
else
{
_putchar(hex[i] - 10 + 'A');
}
count++;
count += _print_hex_X(*str);
}
else
count += _putchar(*str);

str++;
}

return (count);
Expand Down
2 changes: 1 addition & 1 deletion utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ int _print_string(char *str)

while (*str != '\0')
{
write(1, str, 1);
_putchar(*str);
len++;
str++;
}
Expand Down
40 changes: 40 additions & 0 deletions utils_helpers.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include "main.h"

/**
* _print_hex_X - Prints the hexadecimal representation of an unsigned integer.
* @args: A va_list containing an unsigned integer
*
* Return: The count of characters printed.
*/
int _print_hex_X(unsigned int num)
{
int count = 0, i = 0, hex[32];

if (num == 0)
{
_putchar('0');
count++;
}
else
{
while (num > 0)
{
hex[i] = num % 16;
num = num / 16;
i++;
}

for (i = i - 1; i >= 0; i--)
{
if (hex[i] < 10)
_putchar(hex[i] + '0');
else
{
_putchar(hex[i] - 10 + 'A');
}
count++;
}
}

return (count);
}