122 lines
1.7 KiB
C
122 lines
1.7 KiB
C
#include "main.h"
|
|
/**
|
|
* _strlen - Returns the length.
|
|
* @s: char pointer
|
|
* Return: c.
|
|
*/
|
|
int _strlen(char *s)
|
|
{
|
|
int c;
|
|
for (c = 0; s[c] != 0; c++)
|
|
;
|
|
return (c);
|
|
|
|
}
|
|
/**
|
|
* _strlenc - char pointer s
|
|
* @s: char pointer
|
|
* Return: c
|
|
*/
|
|
int _strlenc(const char *s)
|
|
{
|
|
int c;
|
|
|
|
for (c = 0; s[c] != 0; c++)
|
|
;
|
|
return (c);
|
|
}
|
|
|
|
/**
|
|
* printf_string - print a string.
|
|
* @val: argument.
|
|
* Return: the length of the string.
|
|
*/
|
|
|
|
int printf_string(va_list val)
|
|
{
|
|
char *s;
|
|
int i, len;
|
|
|
|
s = va_arg(val, char *);
|
|
if (s == NULL)
|
|
{
|
|
s = "(null)";
|
|
len = _strlen(s);
|
|
for (i = 0; i < len; i++)
|
|
_putchar(s[i]);
|
|
return (len);
|
|
}
|
|
else
|
|
{
|
|
len = _strlen(s);
|
|
for (i = 0; i < len; i++)
|
|
_putchar(s[i]);
|
|
return (len);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* printf_unsigned - integer
|
|
* @args: argument to print
|
|
* Return: number of characters printed
|
|
*/
|
|
int printf_unsigned(va_list args)
|
|
{
|
|
unsigned int n = va_arg(args, unsigned int);
|
|
int num, last = n % 10, digit, exp = 1;
|
|
int i = 1;
|
|
|
|
n = n / 10;
|
|
num = n;
|
|
|
|
if (last < 0)
|
|
{
|
|
_putchar('-');
|
|
num = -num;
|
|
n = -n;
|
|
last = -last;
|
|
i++;
|
|
}
|
|
if (num > 0)
|
|
{
|
|
while (num / 10 != 0)
|
|
{
|
|
exp = exp * 10;
|
|
num = num / 10;
|
|
}
|
|
num = n;
|
|
while (exp > 0)
|
|
{
|
|
digit = num / exp;
|
|
_putchar(digit + '0');
|
|
num = num - (digit * exp);
|
|
exp = exp / 10;
|
|
i++;
|
|
}
|
|
}
|
|
_putchar(last + '0');
|
|
|
|
return (i);
|
|
}
|
|
|
|
/**
|
|
* printf_srev - function that prints a str in reverse
|
|
* @args: type struct va_arg where is allocated printf arguments
|
|
* Return: the string
|
|
*/
|
|
int printf_srev(va_list args)
|
|
{
|
|
|
|
char *s = va_arg(args, char*);
|
|
int i;
|
|
int j = 0;
|
|
|
|
if (s == NULL)
|
|
s = "(null)";
|
|
while (s[j] != '\0')
|
|
j++;
|
|
for (i = j - 1; i >= 0; i--)
|
|
_putchar(s[i]);
|
|
return (j);
|
|
}
|