CS50 Why can't my code crack a four digit password?
11:11 03 Nov 2018

I've encountered a problem in C that held me for the past 6 hours, and after research and studies, I decided to seek for help! Sorry, my English is not my first language and I am a beginner coder.

I am able to crack all passwords that are three digits long. However, it seems like I cannot crack passwords that are four digits long. I've tried my best to make some comprehension and figure out my mistakes in the code, but it seems I cannot do so. Please help!

Also, it would be very nice if there would be more explanations why I cannot crack a four digit long passwords instead of plain solution. Thank you in advance!

#define _XOPEN_SOURCE
#include 
#include 
#include 
#include 
#include 

int main (int argc, char* argv[])
{

    if (argc != 2){
        printf("Invalid input\n");
        return 1;
    }

    char* hash = argv[1];
    char code[4];
    char* alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    int length = strlen(alphabet);

    for (int a = 0; a <= length; a++)
    {
        code[0] = alphabet[a];
        printf("%s\n", code);
        if (strcmp(crypt(code, "50"), hash) == 0)
        {
            printf("%s\n", code);
            return 0;
        }

        for (int b = 0; b <= length; b++)
        {
            code[1] = alphabet[b];
            //printf("%s\n", code[1]);
            if (strcmp(crypt(code, "50"), hash) == 0)
            {
                printf("%s\n", code);
                return 0;
            }

            for (int c = 0; c <= length; c++)
            {
                code[2] = alphabet[c];
                //printf("%s\n", code[2]);
                if (strcmp(crypt(code, "50"), hash) == 0)
                {
                    printf("%s\n", code);
                    return 0;
                }

                for (int d = 0; d <= length; d++)
                {
                    code[3] = alphabet[d];
                    //printf("%s\n", code[3]);
                    if (strcmp(crypt(code, "50"), hash) == 0)
                    {
                        printf("%s\n", code);
                        return 0;
                    }
                }
            }
        }
    }
}
c cs50