itoa is pog

This commit is contained in:
LinlyBoi
2023-08-22 22:11:10 +03:00
parent 25ddd63fc9
commit 160a8f5813
2 changed files with 113 additions and 0 deletions

100
itoa.c Normal file
View 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);
}

13
tests/itoa.c Normal file
View 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);
}