Skip to content

Commit

Permalink
Merge pull request #4 from melvinredondotanis/test_printf
Browse files Browse the repository at this point in the history
best printf reaction for special carac
  • Loading branch information
melvinredondotanis committed Jul 24, 2024
2 parents 3a95e45 + 2581756 commit 1e591c8
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 58 deletions.
66 changes: 41 additions & 25 deletions _printf.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
#include "main.h"

/**
* check_format - function that checks if a character is a format specifier
* @format: character to check
* Return: format specifier or 0 if not found
*/
char check_format(char format)
{
int i;
char types[4] = {'c', 's', 'd', 'i'};

for (i = 0; i < 4; i++)
{
if (format == types[i])
return (types[i]);
}
return (0);
}

/**
* _printf - function that prints a formatted string
* @format: string to print
Expand All @@ -9,43 +27,40 @@
int _printf(const char *format, ...)
{
int i = 0, j = 0, count = 0;

char tmp;
va_list args;

format_spec forms[] = {
{'d', put_d},
{'s', put_s},
{'c', put_c},
{'\0', NULL}};
format_spec forms[] = {{'c', put_c}, {'s', put_s}, {'d', put_d}, {'\0', NULL}};

va_start(args, format);

while (format[i])
{
if (format[i] == '\\' && format[i + 1] == '%')
{
_putchar('\\');
_putchar('%');
count += 2;
i += 2;
continue;
}
else if (format[i] == '%')
if (format[i] == '%')
{
i++;
if (format[i] == '%')
if (format[i + 1] == '%')
{
_putchar('%');
count++;
i++;
continue;
count++;
}
for (j = 0; forms[j].spec; j++)
else
{
if (format[i] == forms[j].spec)
tmp = check_format(format[i + 1]);
if (tmp)
{
i++;
for (j = 0; forms[j].spec; j++)
{
if (tmp == forms[j].spec)
{
count += forms[j].form(args);
break;
}
}
}
else
{
count += forms[j].form(args);
break;
_putchar(format[i]);
count++;
}
}
}
Expand All @@ -56,5 +71,6 @@ int _printf(const char *format, ...)
}
i++;
}
va_end(args);
return (count);
}
62 changes: 31 additions & 31 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,36 @@
*/
int main(void)
{
int len;
int len2;
unsigned int ui;
void *addr;
int len;
int len2;
unsigned int ui;
void *addr;

len = _printf("Let's try to printf a simple sentence.\n");
len2 = printf("Let's try to printf a simple sentence.\n");
ui = (unsigned int)INT_MAX + 1024;
addr = (void *)0x7ffe637541f0;
_printf("Length:[%d, %i]\n", len, len);
printf("Length:[%d, %i]\n", len2, len2);
_printf("Negative:[%d]\n", -762534);
printf("Negative:[%d]\n", -762534);
_printf("Unsigned:[%u]\n", ui);
printf("Unsigned:[%u]\n", ui);
_printf("Unsigned octal:[%o]\n", ui);
printf("Unsigned octal:[%o]\n", ui);
_printf("Unsigned hexadecimal:[%x, %X]\n", ui, ui);
printf("Unsigned hexadecimal:[%x, %X]\n", ui, ui);
_printf("Character:[%c]\n", 'H');
printf("Character:[%c]\n", 'H');
_printf("String:[%s]\n", "I am a string !");
printf("String:[%s]\n", "I am a string !");
_printf("Address:[%p]\n", addr);
printf("Address:[%p]\n", addr);
len = _printf("Percent:[%%]\n");
len2 = printf("Percent:[%%]\n");
_printf("Len:[%d]\n", len);
printf("Len:[%d]\n", len2);
_printf("Unknown:[%r]\n");
printf("Unknown:[%r]\n");
return (0);
len = _printf("Let's try to printf a simple sentence.\n");
len2 = printf("Let's try to printf a simple sentence.\n");
ui = (unsigned int)INT_MAX + 1024;
addr = (void *)0x7ffe637541f0;
_printf("Length:[%d, %i]\n", len, len);
printf("Length:[%d, %i]\n", len2, len2);
_printf("Negative:[%d]\n", -762534);
printf("Negative:[%d]\n", -762534);
_printf("Unsigned:[%u]\n", ui);
printf("Unsigned:[%u]\n", ui);
_printf("Unsigned octal:[%o]\n", ui);
printf("Unsigned octal:[%o]\n", ui);
_printf("Unsigned hexadecimal:[%x, %X]\n", ui, ui);
printf("Unsigned hexadecimal:[%x, %X]\n", ui, ui);
_printf("Character:[%c]\n", 'H');
printf("Character:[%c]\n", 'H');
_printf("String:[%s]\n", "I am a string !");
printf("String:[%s]\n", "I am a string !");
_printf("Address:[%p]\n", addr);
printf("Address:[%p]\n", addr);
len = _printf("Percent:[%%]\n");
len2 = printf("Percent:[%%]\n");
_printf("Len:[%d]\n", len);
printf("Len:[%d]\n", len2);
_printf("Unknown:[%r]\n");
printf("Unknown:[%r]\n");
return (0);
}
8 changes: 6 additions & 2 deletions puts.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
*/
int put_c(va_list args)
{
_putchar(va_arg(args, int));
char c = va_arg(args, int);
_putchar(c);
return (1);
}

Expand All @@ -18,8 +19,11 @@ int put_c(va_list args)
*/
int put_s(va_list args)
{
int i = 0;
char *str = va_arg(args, char *);
int i = 0;

if (str == NULL)
str = "(null)";

while (str[i])
{
Expand Down

0 comments on commit 1e591c8

Please sign in to comment.