how can i do it without switch case loop(counter for example)

Viewed 81

I wrote this program that asks for the number of characters wish is alphabet in a single line and then calculates the possible configurations through that number..but the options are limited..so how can I make the selections unlimited in a domaine like for exp : you have maximum 4 characters in a line?`

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[])
 {
    int Nb_Alpha,P_Kida = 1,i,j,k,h,Power = 1,nb,P_lvl,counter = 1;
    printf("enter the number of alphabet in one line :\t\n");
    scanf("%d",&Nb_Alpha);
    for(i=1;i<=Nb_Alpha;i++)//this is the Exponent made by me
    {
        P_Kida *= 26;
     }
  
        switch(Nb_Alpha)
        {
            case 1: 
            {
                
                for(i=65;i<=90;i++)
                        {
                            printf("%c\n",i);
                        }
            
             }  
            break;
             
             case 2 : {
                
                printf("Double probabilytis !\n");
                for(i=65;i<=90;i++)
                {
                    for(j=65;j<=90;j++)
                    {
                        printf("%c%c\n",i,j);
                    }
                }
             }
             break;
            
            case 3: 
            {
                    printf("Triple probabilytis !\n");
                    for(i=65;i<=90;i++)
                    {
                        for(j=65;j<=90;j++)
                        {
                          for(k=65;k<=90;k++)
                          {
                                printf("%c%c%c\n",i,j,k);
                          }
                        }
                    }
            
            }
            break;
            
                case 4: 
            {
                    printf("Quadra probabilytis !\n");
                    for(i=65;i<=90;i++)
                    {
                        for(j=65;j<=90;j++)
                        {
                          for(k=65;k<=90;k++)
                          {
                            for(h=65;h<=90;h++)
                            {
                                printf("%c%c%c%c\n",i,j,k,h);
                              }
                                
                          }
                        }
                    }
            
            }
            break;
            
        }
        
        
        
            
        printf("that is the %d probabilytis that u can make with %d alphabets\n",P_Kida,Nb_Alpha);
    return 0;
}`
2 Answers

One way to solve this is to use recursion.

For instance:

#define MAX 32

void rec(unsigned int nb, char* s, size_t len)
{
    if (nb == 0)
    {
        puts(s);
        return;
    }
    
    for(int i = 'A'; i <= 'Z'; i++)
    {
        s[len] = i;
        rec(nb - 1, s, len + 1);
    }
    s[len] = '\0';
}


void foo(unsigned int nb)
{
    assert(nb <= MAX);
    char x[MAX + 1] = "";
    
    for(int i = 'A'; i <= 'Z'; i++)
    {
        x[0] = i;
        rec(nb - 1, x, 1);
    }
}

int main(void)
{
    foo(6);
    return 0;
}

Recursion seems like an overkill here. Think of your configurations as numbers.

How would you list all the k-digit numbers? Obviously, start with 0...0, and increment until it becomes 9...9.

Your problem is exactly the same. Start with a...a, and increment until it becomes z...z. Now the question is how to increment a string? Same way as a number. If the last character is not yet z, increment it; if it is make it a, and try to increment the previous one, etc.

I hope you can take it from here.

Related