aboutsummaryrefslogtreecommitdiff
path: root/c04/ex05/ft_atoi_base.c
diff options
context:
space:
mode:
Diffstat (limited to 'c04/ex05/ft_atoi_base.c')
-rw-r--r--c04/ex05/ft_atoi_base.c40
1 files changed, 34 insertions, 6 deletions
diff --git a/c04/ex05/ft_atoi_base.c b/c04/ex05/ft_atoi_base.c
index 5de5f81..9a89fc1 100644
--- a/c04/ex05/ft_atoi_base.c
+++ b/c04/ex05/ft_atoi_base.c
@@ -6,10 +6,36 @@
/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/07/06 08:07:17 by cacharle #+# #+# */
-/* Updated: 2019/07/06 12:47:25 by cacharle ### ########.fr */
+/* Updated: 2019/07/07 13:26:28 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
+int check_the_base(char *base)
+{
+ int i;
+ int j;
+
+ i = 0;
+ while (base[i])
+ {
+ if (base[i] == '-' || base[i] == '+' || base[i] == ' '
+ || base[i] == '\t' || base[i] == '\n' || base[i] == '\v'
+ || base[i] == '\f' || base[i] == '\r')
+ return (0);
+ j = 0;
+ while (base[j])
+ {
+ if (j != i && base[j] == base[i])
+ return (0);
+ j++;
+ }
+ i++;
+ }
+ if (i < 2)
+ return (0);
+ return (1);
+}
+
int my_pow(int base, int exponent)
{
int accumulator;
@@ -33,23 +59,25 @@ int position_in_base(char digit, char *base)
return (i);
}
-int ft_atoi_base(char *str, char *base)
+int ft_atoi_base(char *str, char *base)
{
int radix;
int i;
int j;
int nb;
+ if (!check_the_base(base))
+ return (0);
nb = 0;
radix = 0;
- while (base[++radix]);
+ while (base[radix])
+ radix++;
i = 0;
- while (str[++i]);
- printf("%s base %d (%s)\n", str, radix, base);
+ while (str[i])
+ i++;
j = 0;
while (--i >= 0)
{
- printf("%c: %d * (%d^%d) = %d\n", str[i], position_in_base(str[i], base), radix, i, my_pow(radix, i) * position_in_base(str[j], base));
nb += my_pow(radix, i) * position_in_base(str[j], base);
j++;
}