/03/3-2-ft_atoi_base/ft_atoi_base_withmain.c
https://github.com/fwuensche/42-exam-miner · C · 53 lines · 46 code · 7 blank · 0 comment · 22 complexity · f85e6ece6d959ac2d9b8218f84b5df29 MD5 · raw file
- #include <stdio.h>//
- #include <stdlib.h>//
- int isblank(char c)
- {
- if (c <= 32)
- return (1);
- return (0);
- }
- int isvalid(char c, int base)
- {
- char digits[17] = "0123456789abcdef";
- char digits2[17] = "0123456789ABCDEF";
- while (base--)
- if (digits[base] == c || digits2[base] == c)
- return (1);
- return (0);
- }
- int value_of(char c)
- {
- if (c >= '0' && c <= '9')
- return (c - '0');
- else if (c >= 'a' && c <= 'f')
- return (c - 'a' + 10);
- else if (c >= 'A' && c <= 'F')
- return (c - 'A' + 10);
- return (0);
- }
- int ft_atoi_base(const char *str, int str_base)
- {
- int result;
- int sign;
- result = 0;
- while (isblank(*str))
- str++;
- sign = (*str == '-') ? -1 : 1;
- (*str == '-' || *str == '+') ? ++str : 0;
- while (isvalid(*str, str_base))
- result = result * str_base + value_of(*str++);
- return (result * sign);
- }
- int main(int ac, char **av)//
- {//
- if (ac == 3)//
- printf("result: %d\n", ft_atoi_base(av[1], atoi(av[2])));//
- return (0);//
- }//