100
itoa.c
Normal file
100
itoa.c
Normal file
@@ -0,0 +1,100 @@
|
|||||||
|
#include "main.h"
|
||||||
|
#include <stdlib.h>
|
||||||
|
/**
|
||||||
|
* _str_reverse - reverse a string because I said so
|
||||||
|
* @str: da string
|
||||||
|
* Return: number in string form
|
||||||
|
*/
|
||||||
|
char *_str_reverse(char *str)
|
||||||
|
{
|
||||||
|
unsigned int i, len;
|
||||||
|
char *buffer;
|
||||||
|
|
||||||
|
len = _strlen(str);
|
||||||
|
buffer = malloc(len);
|
||||||
|
if (!buffer)
|
||||||
|
return (NULL);
|
||||||
|
for (i = 0; i < len; i++)
|
||||||
|
*(buffer + len - 1 - i) = *(str + i);
|
||||||
|
return (buffer);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* _itoa - turn NUMBA into a STRING
|
||||||
|
* @n: NUMBA
|
||||||
|
* @base: base
|
||||||
|
* Return: numba in string form
|
||||||
|
*/
|
||||||
|
char *_itoa(int n, int base)
|
||||||
|
{
|
||||||
|
int i, isNegative, rem;
|
||||||
|
char *str;
|
||||||
|
|
||||||
|
str = malloc(1024);
|
||||||
|
i = isNegative = 0;
|
||||||
|
if (n == 0)
|
||||||
|
{
|
||||||
|
*(str + i++) = '0';
|
||||||
|
*(str + i) = '\0';
|
||||||
|
return (str);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (n < 0 && base == 10)
|
||||||
|
{
|
||||||
|
isNegative = 1;
|
||||||
|
n = -n;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (n != 0)
|
||||||
|
{
|
||||||
|
rem = n % base;
|
||||||
|
*(str + i++) = (rem > 9) ? (rem - 10) + 'a' : rem + '0';
|
||||||
|
n = n / base;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isNegative)
|
||||||
|
*(str + i++) = '-';
|
||||||
|
|
||||||
|
*(str + i) = '\0';
|
||||||
|
|
||||||
|
str = _str_reverse(str);
|
||||||
|
|
||||||
|
return (str);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* _uitoa - turn NUMBA into a STRING
|
||||||
|
* @n: NUMBA
|
||||||
|
* @base: base
|
||||||
|
* Return: numba in string form
|
||||||
|
*/
|
||||||
|
char *_uitoa(unsigned int n, int base)
|
||||||
|
{
|
||||||
|
int i, rem;
|
||||||
|
char *str;
|
||||||
|
|
||||||
|
str = malloc(1024);
|
||||||
|
i = 0;
|
||||||
|
if (n == 0)
|
||||||
|
{
|
||||||
|
*(str + i++) = '0';
|
||||||
|
*(str + i) = '\0';
|
||||||
|
return (str);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
while (n != 0)
|
||||||
|
{
|
||||||
|
rem = n % base;
|
||||||
|
*(str + i++) = (rem > 9) ? (rem - 10) + 'a' : rem + '0';
|
||||||
|
n = n / base;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
*(str + i) = '\0';
|
||||||
|
|
||||||
|
str = _str_reverse(str);
|
||||||
|
|
||||||
|
return (str);
|
||||||
|
}
|
||||||
4
main.h
4
main.h
@@ -10,5 +10,9 @@ int _puts(char *str);
|
|||||||
int _putchar(char c);
|
int _putchar(char c);
|
||||||
char *_memset(char *adr, int bval);
|
char *_memset(char *adr, int bval);
|
||||||
int fmt(char c, va_list args);
|
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
|
||||||
|
|||||||
25
printf.c
25
printf.c
@@ -29,7 +29,7 @@ int _printf(const char *format, ...)
|
|||||||
{
|
{
|
||||||
printed += _puts(buffer);
|
printed += _puts(buffer);
|
||||||
buff_size -= _strlen(buffer);
|
buff_size -= _strlen(buffer);
|
||||||
_memset(buffer, 0);
|
buffer = _memset(buffer, 0);
|
||||||
buffer = (char *) malloc(buff_size);
|
buffer = (char *) malloc(buff_size);
|
||||||
if (!buffer)
|
if (!buffer)
|
||||||
return (-1);
|
return (-1);
|
||||||
@@ -43,7 +43,8 @@ int _printf(const char *format, ...)
|
|||||||
}
|
}
|
||||||
if (buffer)
|
if (buffer)
|
||||||
{
|
{
|
||||||
_puts(buffer);
|
printed += _puts(buffer);
|
||||||
|
buffer = _memset(buffer, 0);
|
||||||
free(buffer);
|
free(buffer);
|
||||||
}
|
}
|
||||||
return (printed);
|
return (printed);
|
||||||
@@ -64,8 +65,26 @@ int fmt(char c, va_list args)
|
|||||||
return (_putchar(va_arg(args, int)));
|
return (_putchar(va_arg(args, int)));
|
||||||
case '%': /*add 1 byte*/
|
case '%': /*add 1 byte*/
|
||||||
return (_putchar('%'));
|
return (_putchar('%'));
|
||||||
|
case 'd':
|
||||||
|
return (_puts(_itoa(va_arg(args, int), 10)));
|
||||||
|
case 'i':
|
||||||
|
return (_puts(_uitoa(va_arg(args, unsigned int), 10)));
|
||||||
|
case 'u':
|
||||||
|
return (_puts(_uitoa(va_arg(args, unsigned int), 10)));
|
||||||
|
case 'b':
|
||||||
|
return (_puts(_uitoa(va_arg(args, unsigned int), 2)));
|
||||||
|
case 'o':
|
||||||
|
return (_puts(_uitoa(va_arg(args, unsigned int), 8)));
|
||||||
|
case 'x':
|
||||||
|
return (_puts(_uitoa(va_arg(args, unsigned int), 16)));
|
||||||
|
case 'X':
|
||||||
|
return (_puts(str_up(_uitoa(va_arg(args, unsigned int), 16))));
|
||||||
|
case 'p':
|
||||||
|
return (_puts(_uitoa(va_arg(args, unsigned int), 16)));
|
||||||
default:
|
default:
|
||||||
return (0);
|
_putchar('%'); /* TODO make this cleaner */
|
||||||
|
_putchar(c);
|
||||||
|
return (2);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
20
strings.c
20
strings.c
@@ -62,3 +62,23 @@ char *append(char *str, char c)
|
|||||||
*(new_me + len + 1) = '\0';
|
*(new_me + len + 1) = '\0';
|
||||||
return (new_me);
|
return (new_me);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* string_toupper - 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);
|
||||||
|
}
|
||||||
|
|||||||
44
tests/chonk.c
Normal file
44
tests/chonk.c
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
#include "../main.h"
|
||||||
|
#include <limits.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* main - Entry point
|
||||||
|
*
|
||||||
|
* Return: Always 0
|
||||||
|
*/
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
}
|
||||||
13
tests/itoa.c
Normal file
13
tests/itoa.c
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
#include "../main.h"
|
||||||
|
#define shit 0
|
||||||
|
/**
|
||||||
|
* main - check if this works
|
||||||
|
*/
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
char *str;
|
||||||
|
|
||||||
|
str = _itoa(10, 2);
|
||||||
|
_printf("%s", str);
|
||||||
|
return (shit);
|
||||||
|
}
|
||||||
14
tests/num_test.c
Normal file
14
tests/num_test.c
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
#include "../main.h"
|
||||||
|
#define shit 0
|
||||||
|
#define fr ;
|
||||||
|
/**
|
||||||
|
* main - no cap fr %d
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
_printf("the number is: %d\n", 10)fr
|
||||||
|
_printf("the binary is: %b\n", 10)fr
|
||||||
|
_printf("the cursed i thing is: %i\n", 011);
|
||||||
|
return (shit)fr
|
||||||
|
}
|
||||||
@@ -1,3 +1,3 @@
|
|||||||
#!/usr/bin/bash
|
#!/usr/bin/bash
|
||||||
gcc -Wall -pedantic -Werror -Wextra -std=gnu89 $1 ../*.c -o a
|
gcc -Wno-format -Wall -pedantic -Werror -Wextra -std=gnu89 $1 ../*.c -o a
|
||||||
./a
|
./a
|
||||||
|
|||||||
Reference in New Issue
Block a user