piscine/c04/ex05/ft_atoi_base.c
Willem Haffmans 607ce08c18 all
2024-09-10 00:18:01 +02:00

89 lines
1.8 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi_base.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/12 09:17:21 by whaffman #+# #+# */
/* Updated: 2024/06/12 18:55:08 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
int ft_strlen(char *str)
{
int i;
i = 0;
while (*str++)
i++;
return (i);
}
int ft_isrepeating(char *str)
{
int i;
while (*str)
{
i = 1;
while (str[i])
{
if (str[0] == str[i])
return (1);
i++;
}
str++;
}
return (0);
}
int ft_indexof(char c, char *str)
{
int result;
result = 0;
while (str[result])
{
if (str[result] == c)
return (result);
result++;
}
return (-1);
}
int ft_atoi_base(char *str, char *base)
{
int sign;
int res;
while (ft_indexof(*str, " \f\n\r\t\v") >= 0)
str++;
res = 0;
sign = 1;
while (*str == '-' || *str == '+')
{
if (*str == '-')
sign *= -1;
str++;
}
while (ft_indexof(*str, base) >= 0)
res = ft_strlen(base) * res + ft_indexof(*str++, base);
return (sign * res);
}
#ifdef DEBUG
int main(void)
{
int r;
r = ft_atoi_base("-2147483648", "0123456789");
printf("%d", r);
return (0);
}
#endif