user8590061
user8590061

Reputation: 27

Error in generating a four letter character array

char i,j,k,l;
if(argc==2)
{

    char a[5]="\0";
    for(i='a';i<='z';i++)
    {
        a[0]=i;
        printf("%s\n",a);
        if(strcmp(crypt(a,"50"),argv[1])==0)
        {
         printf("%s\n", a);
         break;

        }
        else{
                for(j='a';j<='z';j++)
                {
                    a[1]=j;

                    if(strcmp(crypt(a,"50"), argv[1])==0)
                    {
                        printf("%s\n", a);
                        break;

                    }
                    else
                    {
                        for(k='a';k<='z';k++)
                        {
                            a[2]=k;
                            if(strcmp(crypt(a,"50"), argv[1])==0)
                            {
                                printf("%s\n", a);
                                break;

                            }
                            else
                            {
                                for(l='a';l<='z';l++)
                                {
                                    a[3]=l;


                                    if(strcmp(crypt(a,"50"), argv[1])==0)
                                    {
                                        printf("%s\n", a);
                                         break;

                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

The major portion of the code is to create a set of single, double, triple and four letter strings and then check their crypted hashes across the hashed code which is passed as input The hashing is done by the crypt function provided with c

And to check the error in my original program I used that printf line

printf("%s\n", a);

And the output which I expected was

a  
b  
c  
d  
e 
(...)

But the output was:

azzz  
bzzz  
czzz  
dzzz  
(...) 

Many of the people told me to use the printf line in the inner most loop but then too it would not change the original error that my program will not be able to produce the set of single, double an triple characters. It will only be able to create a four character string.

Upvotes: 1

Views: 67

Answers (1)

MultiSkill
MultiSkill

Reputation: 416

Most likely you are getting leftovers from the inner loops. So when the outer loop gets to b, a[1]..a[3] still have the 'z' character in them. Simple solution would be to do something like:

a[0]=i;
a[1]=0;

But I would simplify the loops altogether and do it that way:

const char END[]="zzzz"; // any word, e.g. END[]="end";
char a[sizeof(END)/sizeof(*END)]=""; // 4 chars + 1 '\0'
do{
    // increment a
    for(int i=0;i<(sizeof(a)/sizeof(*a)-1);i++){
        if(a[i]=='z'){a[i]='a';}
        else {
            if(a[i]) a[i]+=1;
            else     a[i]='a';
            break;
        }
    }

    if(strcmp(crypt(a, "50"), argv[1])==0){
        printf("%s\n", a);
        break;
    }
} while(strcmp(a, END)); // finish on a == END

Not the most elegant solution, but should work. I'll leave you to fill out the rest.

Good luck.

Bonus edit: made the code easier to configure by adding the const up top.

Upvotes: 1

Related Questions