honor restored
This commit is contained in:
46
_printf.c
46
_printf.c
@@ -1,46 +0,0 @@
|
|||||||
#include "main.h"
|
|
||||||
/**
|
|
||||||
* _printf - printf but slow fr
|
|
||||||
* @format: identifier.
|
|
||||||
* Return: length.
|
|
||||||
*/
|
|
||||||
int _printf(const char * const format, ...)
|
|
||||||
{
|
|
||||||
convert_match m[] = {
|
|
||||||
{"%s", printf_string}, {"%c", printf_char},
|
|
||||||
{"%%", printf_37}, {"%i", printf_int},
|
|
||||||
{"%d", printf_dec}, {"%r", printf_srev},
|
|
||||||
{"%R", printf_rot13}, {"%b", printf_bin}, {"%u", printf_unsigned},
|
|
||||||
{"%o", printf_oct}, {"%x", printf_hex}, {"%X", printf_HEX},
|
|
||||||
{"%S", printf_exclusive_string}, {"%p", printf_pointer}
|
|
||||||
};
|
|
||||||
|
|
||||||
/* init list*/
|
|
||||||
va_list args;
|
|
||||||
int i = 0, j, len = 0;
|
|
||||||
|
|
||||||
va_start(args, format);
|
|
||||||
if (format == NULL || (format[0] == '%' && format[1] == '\0'))
|
|
||||||
return (-1);
|
|
||||||
/*jump*/
|
|
||||||
Here:
|
|
||||||
while (format[i] != '\0')
|
|
||||||
{
|
|
||||||
j = 13;
|
|
||||||
while (j >= 0)
|
|
||||||
{
|
|
||||||
if (m[j].id[0] == format[i] && m[j].id[1] == format[i + 1])
|
|
||||||
{
|
|
||||||
len += m[j].f(args);
|
|
||||||
i = i + 2;
|
|
||||||
goto Here;
|
|
||||||
}
|
|
||||||
j--;
|
|
||||||
}
|
|
||||||
_putchar(format[i]);
|
|
||||||
len++;
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
va_end(args);
|
|
||||||
return (len);
|
|
||||||
}
|
|
||||||
12
_putchar.c
12
_putchar.c
@@ -1,12 +0,0 @@
|
|||||||
#include "main.h"
|
|
||||||
|
|
||||||
/**
|
|
||||||
* _putchar - print character
|
|
||||||
* @c: character
|
|
||||||
*
|
|
||||||
* Return: 1
|
|
||||||
*/
|
|
||||||
int _putchar(char c)
|
|
||||||
{
|
|
||||||
return (write(1, &c, 1));
|
|
||||||
}
|
|
||||||
41
exclusive.c
41
exclusive.c
@@ -1,41 +0,0 @@
|
|||||||
#include "main.h"
|
|
||||||
/**
|
|
||||||
* printf_exclusive_string - print exclusuives string.
|
|
||||||
* @val: argument.
|
|
||||||
* Return: the length of the string.
|
|
||||||
*/
|
|
||||||
|
|
||||||
int printf_exclusive_string(va_list val)
|
|
||||||
{
|
|
||||||
char *s;
|
|
||||||
int i, len = 0;
|
|
||||||
int cast;
|
|
||||||
|
|
||||||
s = va_arg(val, char *);
|
|
||||||
if (s == NULL)
|
|
||||||
s = "(null)";
|
|
||||||
for (i = 0; s[i] != '\0'; i++)
|
|
||||||
{
|
|
||||||
if (s[i] < 32 || s[i] >= 127)
|
|
||||||
{
|
|
||||||
/* speical */
|
|
||||||
_putchar('\\');
|
|
||||||
_putchar('x');
|
|
||||||
len = len + 2;
|
|
||||||
cast = s[i];
|
|
||||||
if (cast < 16)
|
|
||||||
{
|
|
||||||
_putchar('0');
|
|
||||||
len++;
|
|
||||||
}
|
|
||||||
/* epic hex */
|
|
||||||
len = len + printf_HEX_aux(cast);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
_putchar(s[i]);
|
|
||||||
len++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return (len);
|
|
||||||
}
|
|
||||||
59
main.h
59
main.h
@@ -1,49 +1,18 @@
|
|||||||
#ifndef MAIN_H
|
#ifndef MAIN_H_
|
||||||
#define MAIN_H
|
#define MAIN_H_
|
||||||
|
|
||||||
/* important libs */
|
|
||||||
/* they bussin frfr*/
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <stdlib.h>
|
|
||||||
#include <limits.h>
|
|
||||||
|
|
||||||
/**
|
|
||||||
* struct format - conversion specifiers
|
|
||||||
* @id:identifier
|
|
||||||
* @f: type pointer
|
|
||||||
*/
|
|
||||||
|
|
||||||
typedef struct format
|
|
||||||
{
|
|
||||||
char *id;
|
|
||||||
int (*f)();
|
|
||||||
} convert_match;
|
|
||||||
|
|
||||||
int printf_pointer(va_list val);
|
|
||||||
int printf_hex_aux(unsigned long int num);
|
|
||||||
int printf_HEX_aux(unsigned int num);
|
|
||||||
int printf_exclusive_string(va_list val);
|
|
||||||
int printf_HEX(va_list val);
|
|
||||||
int printf_unsigned(va_list args);
|
|
||||||
int printf_bin(va_list val);
|
|
||||||
int printf_srev(va_list args);
|
|
||||||
int printf_rot13(va_list args);
|
|
||||||
int printf_int(va_list args);
|
|
||||||
int printf_hex(va_list val);
|
|
||||||
int _strlenc(const char *s);
|
|
||||||
int rev_string(char *s);
|
|
||||||
int _strlenc(const char *s);
|
|
||||||
int printf_37(void);
|
|
||||||
int printf_char(va_list val);
|
|
||||||
int printf_string(va_list val);
|
|
||||||
int _putchar(char c);
|
|
||||||
int _printf(const char *format, ...);
|
int _printf(const char *format, ...);
|
||||||
int printf_oct(va_list val);
|
int _contains(const char *str, char c);
|
||||||
int printf_dec(va_list args);
|
int _strlen(const char *str);
|
||||||
int _strlen(char *s);
|
char *_strcpy(char *dest, char *src);
|
||||||
int *_strcpy(char *dest, char *src);
|
char *append(char *str, char c);
|
||||||
|
int _puts(char *str);
|
||||||
|
int _putchar(char c);
|
||||||
|
char *_memset(char *adr, int bval);
|
||||||
|
int fmt(char c, va_list args);
|
||||||
|
char *_str_reverse(char *str);
|
||||||
|
char *_itoa(int n, int base);
|
||||||
|
char *_uitoa(unsigned int n, int base);
|
||||||
|
char *str_up(char *s);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
36
man_3_printf
36
man_3_printf
@@ -1,36 +0,0 @@
|
|||||||
NAME
|
|
||||||
_printf
|
|
||||||
LIBRARY
|
|
||||||
nonstandard library (libmjork)
|
|
||||||
SYNOPSIS
|
|
||||||
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdarg.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <limits.h>
|
|
||||||
int printf_pointer(va_list val);
|
|
||||||
int printf_hex_aux(unsigned long int num);
|
|
||||||
int printf_HEX_aux(unsigned int num);
|
|
||||||
int printf_exclusive_string(va_list val);
|
|
||||||
int printf_HEX(va_list val);
|
|
||||||
int printf_unsigned(va_list args);
|
|
||||||
int printf_bin(va_list val);
|
|
||||||
int printf_srev(va_list args);
|
|
||||||
int printf_rot13(va_list args);
|
|
||||||
int printf_int(va_list args);
|
|
||||||
int printf_hex(va_list val);
|
|
||||||
int _strlenc(const char *s);
|
|
||||||
int rev_string(char *s);
|
|
||||||
int _strlenc(const char *s);
|
|
||||||
int printf_37(void);
|
|
||||||
int printf_char(va_list val);
|
|
||||||
int printf_string(va_list val);
|
|
||||||
int _putchar(char c);
|
|
||||||
int _printf(const char *format, ...);
|
|
||||||
int printf_oct(va_list val);
|
|
||||||
int printf_dec(va_list args);
|
|
||||||
int _strlen(char *s);
|
|
||||||
int *_strcpy(char *dest, char *src);
|
|
||||||
DESCRIPTION
|
|
||||||
I PROVED A POINT AND THATS WHAT MATTERS
|
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
#ifndef MAIN_H_
|
|
||||||
#define MAIN_H_
|
|
||||||
#include <stdarg.h>
|
|
||||||
int _printf(const char *format, ...);
|
|
||||||
int _contains(const char *str, char c);
|
|
||||||
int _strlen(const char *str);
|
|
||||||
char *_strcpy(char *dest, char *src);
|
|
||||||
char *append(char *str, char c);
|
|
||||||
int _puts(char *str);
|
|
||||||
int _putchar(char c);
|
|
||||||
char *_memset(char *adr, int bval);
|
|
||||||
int fmt(char c, va_list args);
|
|
||||||
char *_str_reverse(char *str);
|
|
||||||
char *_itoa(int n, int base);
|
|
||||||
char *_uitoa(unsigned int n, int base);
|
|
||||||
char *str_up(char *s);
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,84 +0,0 @@
|
|||||||
#include "main.h"
|
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
/**
|
|
||||||
* _strlen - Takes string and return its length
|
|
||||||
*
|
|
||||||
* @str: Address to the head of the string (Array of Characters) (Lost btw)
|
|
||||||
*
|
|
||||||
* Return: Length of String
|
|
||||||
*/
|
|
||||||
int _strlen(const char *str)
|
|
||||||
{
|
|
||||||
if (*str)
|
|
||||||
return (1 + _strlen(++str));
|
|
||||||
else
|
|
||||||
return (0);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* _contains - gets number of unique identifiers
|
|
||||||
*
|
|
||||||
* @str: da string
|
|
||||||
* @c: basically almost always '%'
|
|
||||||
*
|
|
||||||
* Return: number of unique cases of "%*" that aren't "%%"
|
|
||||||
*/
|
|
||||||
int _contains(const char *str, char c)
|
|
||||||
{
|
|
||||||
if (*str)
|
|
||||||
{
|
|
||||||
if (*str == c && *(str + 1) != c && *(str - 1) != c)
|
|
||||||
return (1 + _contains(str + 1, c));
|
|
||||||
else
|
|
||||||
return (_contains(str + 1, c));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
return (0);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* append - shoved character at the end
|
|
||||||
*
|
|
||||||
* @str: main string
|
|
||||||
* @c: character getting shoved
|
|
||||||
*
|
|
||||||
* Return: da string but appended
|
|
||||||
*/
|
|
||||||
|
|
||||||
char *append(char *str, char c)
|
|
||||||
{
|
|
||||||
int len, i;
|
|
||||||
char *new_me;
|
|
||||||
|
|
||||||
len = _strlen(str);
|
|
||||||
|
|
||||||
new_me = malloc(len + 1);
|
|
||||||
i = -1;
|
|
||||||
while (*(str + ++i))
|
|
||||||
*(new_me + i) = *(str + i);
|
|
||||||
|
|
||||||
*(new_me + i++) = c;
|
|
||||||
*(new_me + len + 1) = '\0';
|
|
||||||
return (new_me);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* str_up - changes all lowercase letters of a string
|
|
||||||
* to uppercase
|
|
||||||
* @s: string to modify
|
|
||||||
*
|
|
||||||
* Return: the resulting string
|
|
||||||
*/
|
|
||||||
char *str_up(char *s)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
|
|
||||||
for (i = 0; s[i] != '\0'; i++)
|
|
||||||
{
|
|
||||||
if (s[i] >= 'a' && s[i] <= 'z')
|
|
||||||
s[i] = s[i] - 32;
|
|
||||||
}
|
|
||||||
|
|
||||||
return (s);
|
|
||||||
}
|
|
||||||
Binary file not shown.
@@ -1,155 +0,0 @@
|
|||||||
#include "main.h"
|
|
||||||
|
|
||||||
/**
|
|
||||||
* printf_dec - decimal
|
|
||||||
* @args: argument rint
|
|
||||||
* Return: number of characters printed
|
|
||||||
*/
|
|
||||||
|
|
||||||
int printf_dec(va_list args)
|
|
||||||
{
|
|
||||||
/* initvars*/
|
|
||||||
int n = va_arg(args, int);
|
|
||||||
int num, last = n % 10, digit;
|
|
||||||
int i = 1;
|
|
||||||
int exp = 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);
|
|
||||||
}
|
|
||||||
|
|
||||||
#include "main.h"
|
|
||||||
|
|
||||||
/**
|
|
||||||
* printf_oct - prints an octal number.
|
|
||||||
* @val: arguments.
|
|
||||||
* Return: counter.
|
|
||||||
*/
|
|
||||||
int printf_oct(va_list val)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
int *array;
|
|
||||||
int counter = 0;
|
|
||||||
unsigned int num = va_arg(val, unsigned int);
|
|
||||||
unsigned int temp = num;
|
|
||||||
|
|
||||||
while (num / 8 != 0)
|
|
||||||
{
|
|
||||||
num /= 8;
|
|
||||||
counter++;
|
|
||||||
}
|
|
||||||
counter++;
|
|
||||||
array = malloc(counter * sizeof(int));
|
|
||||||
|
|
||||||
for (i = 0; i < counter; i++)
|
|
||||||
{
|
|
||||||
array[i] = temp % 8;
|
|
||||||
temp /= 8;
|
|
||||||
}
|
|
||||||
for (i = counter - 1; i >= 0; i--)
|
|
||||||
{
|
|
||||||
_putchar(array[i] + '0');
|
|
||||||
}
|
|
||||||
free(array);
|
|
||||||
return (counter);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* printf_hex_aux - prints an hexgecimal number.
|
|
||||||
* @num: arguments.
|
|
||||||
* Return: counter.
|
|
||||||
*/
|
|
||||||
int printf_hex_aux(unsigned long int num)
|
|
||||||
{
|
|
||||||
long int i;
|
|
||||||
long int *array;
|
|
||||||
long int counter = 0;
|
|
||||||
unsigned long int temp = num;
|
|
||||||
|
|
||||||
while (num / 16 != 0)
|
|
||||||
{
|
|
||||||
num /= 16;
|
|
||||||
counter++;
|
|
||||||
}
|
|
||||||
counter++;
|
|
||||||
array = malloc(counter * sizeof(long int));
|
|
||||||
|
|
||||||
for (i = 0; i < counter; i++)
|
|
||||||
{
|
|
||||||
array[i] = temp % 16;
|
|
||||||
temp /= 16;
|
|
||||||
}
|
|
||||||
for (i = counter - 1; i >= 0; i--)
|
|
||||||
{
|
|
||||||
if (array[i] > 9)
|
|
||||||
array[i] = array[i] + 39;
|
|
||||||
_putchar(array[i] + '0');
|
|
||||||
}
|
|
||||||
free(array);
|
|
||||||
return (counter);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* printf_hex - prints an hexgecimal number.
|
|
||||||
* @val: arguments.
|
|
||||||
* Return: counter.
|
|
||||||
*/
|
|
||||||
int printf_hex(va_list val)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
int *array;
|
|
||||||
int counter = 0;
|
|
||||||
unsigned int num = va_arg(val, unsigned int);
|
|
||||||
unsigned int temp = num;
|
|
||||||
|
|
||||||
while (num / 16 != 0)
|
|
||||||
{
|
|
||||||
num /= 16;
|
|
||||||
counter++;
|
|
||||||
}
|
|
||||||
counter++;
|
|
||||||
array = malloc(counter * sizeof(int));
|
|
||||||
|
|
||||||
for (i = 0; i < counter; i++)
|
|
||||||
{
|
|
||||||
array[i] = temp % 16;
|
|
||||||
temp /= 16;
|
|
||||||
}
|
|
||||||
for (i = counter - 1; i >= 0; i--)
|
|
||||||
{
|
|
||||||
if (array[i] > 9)
|
|
||||||
array[i] = array[i] + 39;
|
|
||||||
_putchar(array[i] + '0');
|
|
||||||
}
|
|
||||||
free(array);
|
|
||||||
return (counter);
|
|
||||||
}
|
|
||||||
123
special.c
123
special.c
@@ -1,123 +0,0 @@
|
|||||||
#include "main.h"
|
|
||||||
/**
|
|
||||||
* printf_37 - prints %
|
|
||||||
* Return: 1.
|
|
||||||
*/
|
|
||||||
int printf_37(void)
|
|
||||||
{
|
|
||||||
_putchar(37);
|
|
||||||
return (1);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* printf_rot13 - ROT13 CAESAR's AMAZING CIPHEER
|
|
||||||
* @args: STRING
|
|
||||||
* Return:coun
|
|
||||||
*/
|
|
||||||
int printf_rot13(va_list args)
|
|
||||||
{
|
|
||||||
int i, j, counter = 0;
|
|
||||||
int k = 0;
|
|
||||||
char *s = va_arg(args, char*);
|
|
||||||
/* basic alphabet */
|
|
||||||
char alpha[] = {"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"};
|
|
||||||
/* shifted by 13 */
|
|
||||||
char beta[] = {"nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM"};
|
|
||||||
|
|
||||||
if (s == NULL)
|
|
||||||
s = "(null)";
|
|
||||||
for (i = 0; s[i]; i++)
|
|
||||||
{
|
|
||||||
k = 0;
|
|
||||||
for (j = 0; alpha[j] && !k; j++)
|
|
||||||
{
|
|
||||||
if (s[i] == alpha[j])
|
|
||||||
{
|
|
||||||
_putchar(beta[j]);
|
|
||||||
counter++;
|
|
||||||
k = 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!k)
|
|
||||||
{
|
|
||||||
_putchar(s[i]);
|
|
||||||
counter++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return (counter);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* printf_pointer - prints an hexdecimal number.
|
|
||||||
* @val: arguments.
|
|
||||||
* Return: counter.
|
|
||||||
*/
|
|
||||||
int printf_pointer(va_list val)
|
|
||||||
{
|
|
||||||
void *p;
|
|
||||||
char *s = "(nil)";
|
|
||||||
long int a;
|
|
||||||
int b;
|
|
||||||
int i;
|
|
||||||
|
|
||||||
p = va_arg(val, void*);
|
|
||||||
if (p == NULL)
|
|
||||||
{
|
|
||||||
for (i = 0; s[i] != '\0'; i++)
|
|
||||||
{
|
|
||||||
_putchar(s[i]);
|
|
||||||
}
|
|
||||||
return (i);
|
|
||||||
}
|
|
||||||
|
|
||||||
a = (unsigned long int)p;
|
|
||||||
_putchar('0');
|
|
||||||
_putchar('x');
|
|
||||||
b = printf_hex_aux(a);
|
|
||||||
return (b + 2);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* printf_int - prints integer
|
|
||||||
* @args: argument to print
|
|
||||||
* Return: number of characters printed
|
|
||||||
*/
|
|
||||||
|
|
||||||
int printf_int(va_list args)
|
|
||||||
{
|
|
||||||
int n = va_arg(args, 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);
|
|
||||||
}
|
|
||||||
159
strings.c
159
strings.c
@@ -1,119 +1,84 @@
|
|||||||
#include "main.h"
|
#include "main.h"
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* printf_char - char.
|
* _strlen - Takes string and return its length
|
||||||
* @val: args
|
*
|
||||||
* Return: 1
|
* @str: Address to the head of the string (Array of Characters) (Lost btw)
|
||||||
|
*
|
||||||
|
* Return: Length of String
|
||||||
*/
|
*/
|
||||||
int printf_char(va_list val)
|
int _strlen(const char *str)
|
||||||
{
|
{
|
||||||
char s;
|
if (*str)
|
||||||
|
return (1 + _strlen(++str));
|
||||||
s = va_arg(val, int);
|
else
|
||||||
_putchar(s);
|
return (0);
|
||||||
return (1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* printf_bin - binary number.
|
* _contains - gets number of unique identifiers
|
||||||
* @val: arguments.
|
*
|
||||||
* Return: 1
|
* @str: da string
|
||||||
|
* @c: basically almost always '%'
|
||||||
|
*
|
||||||
|
* Return: number of unique cases of "%*" that aren't "%%"
|
||||||
*/
|
*/
|
||||||
int printf_bin(va_list val)
|
int _contains(const char *str, char c)
|
||||||
{
|
{
|
||||||
/* inits */
|
if (*str)
|
||||||
int flag = 0;
|
|
||||||
int cont = 0;
|
|
||||||
int i, a = 1, b;
|
|
||||||
unsigned int num = va_arg(val, unsigned int);
|
|
||||||
unsigned int p;
|
|
||||||
|
|
||||||
for (i = 0; i < 32; i++)
|
|
||||||
{
|
{
|
||||||
p = ((a << (31 - i)) & num);
|
if (*str == c && *(str + 1) != c && *(str - 1) != c)
|
||||||
if (p >> (31 - i))
|
return (1 + _contains(str + 1, c));
|
||||||
flag = 1;
|
else
|
||||||
if (flag)
|
return (_contains(str + 1, c));
|
||||||
{
|
|
||||||
b = p >> (31 - i);
|
|
||||||
_putchar(b + 48);
|
|
||||||
cont++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (cont == 0)
|
else
|
||||||
{
|
return (0);
|
||||||
cont++;
|
|
||||||
_putchar('0');
|
|
||||||
}
|
|
||||||
return (cont);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* printf_HEX - prints an hexdecimal number.
|
* append - shoved character at the end
|
||||||
* @val: arguments.
|
*
|
||||||
* Return: counter.
|
* @str: main string
|
||||||
|
* @c: character getting shoved
|
||||||
|
*
|
||||||
|
* Return: da string but appended
|
||||||
*/
|
*/
|
||||||
int printf_HEX(va_list val)
|
|
||||||
|
char *append(char *str, char c)
|
||||||
|
{
|
||||||
|
int len, i;
|
||||||
|
char *new_me;
|
||||||
|
|
||||||
|
len = _strlen(str);
|
||||||
|
|
||||||
|
new_me = malloc(len + 1);
|
||||||
|
i = -1;
|
||||||
|
while (*(str + ++i))
|
||||||
|
*(new_me + i) = *(str + i);
|
||||||
|
|
||||||
|
*(new_me + i++) = c;
|
||||||
|
*(new_me + len + 1) = '\0';
|
||||||
|
return (new_me);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* str_up - changes all lowercase letters of a string
|
||||||
|
* to uppercase
|
||||||
|
* @s: string to modify
|
||||||
|
*
|
||||||
|
* Return: the resulting string
|
||||||
|
*/
|
||||||
|
char *str_up(char *s)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
int *arr;
|
|
||||||
int counter = 0;
|
|
||||||
unsigned int num = va_arg(val, unsigned int);
|
|
||||||
unsigned int temp = num;
|
|
||||||
|
|
||||||
while (num / 16 != 0)
|
for (i = 0; s[i] != '\0'; i++)
|
||||||
{
|
{
|
||||||
num /= 16;
|
if (s[i] >= 'a' && s[i] <= 'z')
|
||||||
counter++;
|
s[i] = s[i] - 32;
|
||||||
}
|
}
|
||||||
counter++;
|
|
||||||
arr = malloc(counter * sizeof(int));
|
|
||||||
|
|
||||||
for (i = 0; i < counter; i++)
|
return (s);
|
||||||
{
|
|
||||||
arr[i] = temp % 16;
|
|
||||||
temp /= 16;
|
|
||||||
}
|
|
||||||
for (i = counter - 1; i >= 0; i--)
|
|
||||||
{
|
|
||||||
if (arr[i] > 9)
|
|
||||||
arr[i] = arr[i] + 7;
|
|
||||||
_putchar(arr[i] + '0');
|
|
||||||
}
|
|
||||||
free(arr);
|
|
||||||
return (counter);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* printf_HEX_aux - prints an hexdecimal number.
|
|
||||||
* @num: number to print.
|
|
||||||
* Return: counter.
|
|
||||||
*/
|
|
||||||
int printf_HEX_aux(unsigned int num)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
int *arr;
|
|
||||||
int counter = 0;
|
|
||||||
unsigned int temp = num;
|
|
||||||
|
|
||||||
while (num / 16 != 0)
|
|
||||||
{
|
|
||||||
num /= 16;
|
|
||||||
counter++;
|
|
||||||
}
|
|
||||||
counter++;
|
|
||||||
arr = malloc(counter * sizeof(int));
|
|
||||||
|
|
||||||
for (i = 0; i < counter; i++)
|
|
||||||
{
|
|
||||||
arr[i] = temp % 16;
|
|
||||||
temp /= 16;
|
|
||||||
}
|
|
||||||
for (i = counter - 1; i >= 0; i--)
|
|
||||||
{
|
|
||||||
if (arr[i] > 9)
|
|
||||||
arr[i] = arr[i] + 7;
|
|
||||||
_putchar(arr[i] + '0');
|
|
||||||
}
|
|
||||||
free(arr);
|
|
||||||
return (counter);
|
|
||||||
}
|
}
|
||||||
|
|||||||
122
strs2.c
122
strs2.c
@@ -1,122 +0,0 @@
|
|||||||
#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: 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
|
|
||||||
* Return: number of chars 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: va_arg 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);
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user