piscine-c00/ex08/ft_print_combn.c

69 lines
1.6 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_print_reverse_alphabet.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/15 21:50:37 by tischmid #+# #+# */
/* Updated: 2023/03/16 05:00:01 by tischmid ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
#include <stdio.h>
void print_ns(char *n_str, int n_str_len, int max_len)
{
char new_n_str[10];
char digit;
int i;
i = 0;
while (i < n_str_len)
{
new_n_str[i] = n_str[i];
i++;
}
if (i > 0)
digit = n_str[i - 1] + 1;
else
digit = '0';
if (n_str_len == max_len)
{
write(1, n_str, n_str_len);
if (n_str[0] != 10 - max_len + 0x30)
write(1, ", ", 2);
return ;
}
while (digit <= '9')
{
new_n_str[n_str_len] = digit;
new_n_str[n_str_len + 1] = '\0';
print_ns(new_n_str, n_str_len + 1, max_len);
digit++;
}
}
void ft_print_combn(int n)
{
char n_str[10];
int i;
i = 0;
while (i < 10)
{
n_str[i] = '\0';
i++;
}
print_ns(n_str, 0, n);
}
/* ////
int main(void)
{
ft_print_combn(9);
return (0);
}
*/ ////