44
_printf.c
Normal file
44
_printf.c
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
#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}
|
||||||
|
};
|
||||||
|
|
||||||
|
va_list args;
|
||||||
|
int i = 0, j, len = 0;
|
||||||
|
|
||||||
|
va_start(args, format);
|
||||||
|
if (format == NULL || (format[0] == '%' && format[1] == '\0'))
|
||||||
|
return (-1);
|
||||||
|
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
Normal file
12
_putchar.c
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
#include "main.h"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* _putchar - print character
|
||||||
|
* @c: character
|
||||||
|
*
|
||||||
|
* Return: 1 idk
|
||||||
|
*/
|
||||||
|
int _putchar(char c)
|
||||||
|
{
|
||||||
|
return (write(1, &c, 1));
|
||||||
|
}
|
||||||
39
exclusive.c
Normal file
39
exclusive.c
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
#include "main.h"
|
||||||
|
/**
|
||||||
|
* printf_exclusive_string - print exclusuives string.
|
||||||
|
* @val: argumen t.
|
||||||
|
* 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)
|
||||||
|
{
|
||||||
|
_putchar('\\');
|
||||||
|
_putchar('x');
|
||||||
|
len = len + 2;
|
||||||
|
cast = s[i];
|
||||||
|
if (cast < 16)
|
||||||
|
{
|
||||||
|
_putchar('0');
|
||||||
|
len++;
|
||||||
|
}
|
||||||
|
len = len + printf_HEX_aux(cast);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_putchar(s[i]);
|
||||||
|
len++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return (len);
|
||||||
|
}
|
||||||
57
main.h
57
main.h
@@ -1,18 +1,47 @@
|
|||||||
#ifndef MAIN_H_
|
#ifndef MAIN_H
|
||||||
#define MAIN_H_
|
#define MAIN_H
|
||||||
|
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdio.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
int _printf(const char *format, ...);
|
#include <stdlib.h>
|
||||||
int _contains(const char *str, char c);
|
#include <limits.h>
|
||||||
int _strlen(const char *str);
|
|
||||||
char *_strcpy(char *dest, char *src);
|
/**
|
||||||
char *append(char *str, char c);
|
* struct format - match the conversion specifiers
|
||||||
int _puts(char *str);
|
* @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 _putchar(char c);
|
||||||
char *_memset(char *adr, int bval);
|
int _printf(const char *format, ...);
|
||||||
int fmt(char c, va_list args);
|
int printf_oct(va_list val);
|
||||||
char *_str_reverse(char *str);
|
int printf_dec(va_list args);
|
||||||
char *_itoa(int n, int base);
|
int _strlen(char *s);
|
||||||
char *_uitoa(unsigned int n, int base);
|
int *_strcpy(char *dest, char *src);
|
||||||
char *str_up(char *s);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
36
man_3_printf
Normal file
36
man_3_printf
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
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
|
||||||
18
proven-point/main.h
Normal file
18
proven-point/main.h
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
#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
|
||||||
84
proven-point/strings.c
Normal file
84
proven-point/strings.c
Normal file
@@ -0,0 +1,84 @@
|
|||||||
|
#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);
|
||||||
|
}
|
||||||
BIN
proven-point/tests/a
Executable file
BIN
proven-point/tests/a
Executable file
Binary file not shown.
154
running-out-of-names.c
Normal file
154
running-out-of-names.c
Normal file
@@ -0,0 +1,154 @@
|
|||||||
|
#include "main.h"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* printf_dec - prints decimal
|
||||||
|
* @args: argument to print
|
||||||
|
* Return: number of characters printed
|
||||||
|
*/
|
||||||
|
|
||||||
|
int printf_dec(va_list args)
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
}
|
||||||
121
special.c
Normal file
121
special.c
Normal file
@@ -0,0 +1,121 @@
|
|||||||
|
#include "main.h"
|
||||||
|
/**
|
||||||
|
* printf_37 - prints the char 37.
|
||||||
|
* Return: 1.
|
||||||
|
*/
|
||||||
|
int printf_37(void)
|
||||||
|
{
|
||||||
|
_putchar(37);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* printf_rot13 - printf str to ROT13 place into buffer
|
||||||
|
* @args: type struct va_arg where is allocated printf arguments
|
||||||
|
* Return: counter
|
||||||
|
*/
|
||||||
|
int printf_rot13(va_list args)
|
||||||
|
{
|
||||||
|
int i, j, counter = 0;
|
||||||
|
int k = 0;
|
||||||
|
char *s = va_arg(args, char*);
|
||||||
|
char alpha[] = {"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"};
|
||||||
|
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 hexgecimal 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);
|
||||||
|
}
|
||||||
160
strings.c
160
strings.c
@@ -1,84 +1,120 @@
|
|||||||
#include "main.h"
|
#include "main.h"
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* _strlen - Takes string and return its length
|
* printf_char - prints a char.
|
||||||
*
|
* @val: arguments.
|
||||||
* @str: Address to the head of the string (Array of Characters) (Lost btw)
|
* Return: 1.
|
||||||
*
|
|
||||||
* Return: Length of String
|
|
||||||
*/
|
*/
|
||||||
int _strlen(const char *str)
|
int printf_char(va_list val)
|
||||||
{
|
{
|
||||||
if (*str)
|
char s;
|
||||||
return (1 + _strlen(++str));
|
|
||||||
else
|
s = va_arg(val, int);
|
||||||
return (0);
|
_putchar(s);
|
||||||
|
return (1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* _contains - gets number of unique identifiers
|
* printf_bin - prints a binary number.
|
||||||
*
|
* @val: arguments.
|
||||||
* @str: da string
|
* Return: 1.
|
||||||
* @c: basically almost always '%'
|
|
||||||
*
|
|
||||||
* Return: number of unique cases of "%*" that aren't "%%"
|
|
||||||
*/
|
*/
|
||||||
int _contains(const char *str, char c)
|
int printf_bin(va_list val)
|
||||||
{
|
{
|
||||||
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++)
|
||||||
{
|
{
|
||||||
if (*str == c && *(str + 1) != c && *(str - 1) != c)
|
p = ((a << (31 - i)) & num);
|
||||||
return (1 + _contains(str + 1, c));
|
if (p >> (31 - i))
|
||||||
else
|
flag = 1;
|
||||||
return (_contains(str + 1, c));
|
if (flag)
|
||||||
|
{
|
||||||
|
b = p >> (31 - i);
|
||||||
|
_putchar(b + 48);
|
||||||
|
cont++;
|
||||||
}
|
}
|
||||||
else
|
}
|
||||||
return (0);
|
if (cont == 0)
|
||||||
|
{
|
||||||
|
cont++;
|
||||||
|
_putchar('0');
|
||||||
|
}
|
||||||
|
return (cont);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* append - shoved character at the end
|
* printf_HEX - prints an hexgecimal number.
|
||||||
*
|
* @val: arguments.
|
||||||
* @str: main string
|
* Return: counter.
|
||||||
* @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;
|
||||||
|
|
||||||
for (i = 0; s[i] != '\0'; i++)
|
while (num / 16 != 0)
|
||||||
{
|
{
|
||||||
if (s[i] >= 'a' && s[i] <= 'z')
|
num /= 16;
|
||||||
s[i] = s[i] - 32;
|
counter++;
|
||||||
}
|
}
|
||||||
|
counter++;
|
||||||
|
arr = malloc(counter * sizeof(int));
|
||||||
|
|
||||||
return (s);
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
#include "main.h"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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
Normal file
122
strs2.c
Normal file
@@ -0,0 +1,122 @@
|
|||||||
|
#include "main.h"
|
||||||
|
/**
|
||||||
|
* _strlen - Returns the lenght of a string.
|
||||||
|
* @s: Type char pointer
|
||||||
|
* Return: c.
|
||||||
|
*/
|
||||||
|
int _strlen(char *s)
|
||||||
|
{
|
||||||
|
int c;
|
||||||
|
|
||||||
|
for (c = 0; s[c] != 0; c++)
|
||||||
|
;
|
||||||
|
return (c);
|
||||||
|
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* _strlenc - Strlen function but applied for constant char pointer s
|
||||||
|
* @s: Type 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: argumen t.
|
||||||
|
* 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 - prints 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);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user