aboutsummaryrefslogtreecommitdiff
path: root/c04/ex04
diff options
context:
space:
mode:
Diffstat (limited to 'c04/ex04')
-rw-r--r--c04/ex04/ft_putnbr_base.c47
1 files changed, 26 insertions, 21 deletions
diff --git a/c04/ex04/ft_putnbr_base.c b/c04/ex04/ft_putnbr_base.c
index 6827475..764c42b 100644
--- a/c04/ex04/ft_putnbr_base.c
+++ b/c04/ex04/ft_putnbr_base.c
@@ -6,28 +6,33 @@
/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/07/06 07:47:44 by cacharle #+# #+# */
-/* Updated: 2019/07/06 08:37:59 by cacharle ### ########.fr */
+/* Updated: 2019/07/06 11:12:25 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
+#include <unistd.h>
+
int check_base(char *base)
{
int i;
+ int j;
- if (!*base)
- return (0);
- while (*base)
+ i = 0;
+ while (base[i])
{
- if (*base == '-' || *base == '+')
+ if (base[i] == '-' || base[i] == '+')
return (0);
- i = 0;
- while (base[i])
+ j = 0;
+ while (base[j])
{
- if (base[i] == *base)
+ if (j != i && base[j] == base[i])
return (0);
- i++;
+ j++;
}
+ i++;
}
+ if (i < 2)
+ return (0);
return (1);
}
@@ -36,32 +41,32 @@ int my_strlen(char *str)
int counter;
counter = 0;
- while (*str)
- {
+ while (str[counter])
counter++;
- str++;
- }
return (counter);
}
void ft_putnbr_base(int nbr, char *base)
{
- int radix;
- int i;
- int nb;
- int rev_digits[1024];
+ int radix;
+ int i;
+ int nb;
+ char rev_digits[1024];
if (!check_base(base))
return;
radix = my_strlen(base);
- nb = 0;
i = 0;
- while (nb > 0)
+ /*printf("\n%d base %d (%s)\n", nbr, radix, base);*/
+ while (nbr > 0)
{
- rev_digits[i] = base[nb % radix];
- nb /= radix;
+ /*printf("%d %d %c\n", nbr, nbr % radix, base[nbr % radix]);*/
+ rev_digits[i] = base[nbr % radix];
+ nbr /= radix;
i++;
}
+ /*for (int k = 0; k < 10; k++)*/
+ /*printf("%d ", rev_digits[k]);*/
while (i > 0)
{
i--;