0

So I've looked up a few posts with similar issues and taking the advice of commenters I've put printf after every For loop to see where the program gets stuck.

It was initially was printing one letter, then two letters, three letters and is now onto printing four letters. For example the last two words printed were 'MVwV, MVwU'. In my mind this is how I was hoping the program would work.

When I take the printf statements out though, the cursor just sits there and never prints the solution printf statement "Original password:....". Can anyone give me a hint as to why this might be?

#define _XOPEN_SOURCE
#include <crypt.h>
#include <cs50.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>


int main(int argc, string argv[])
{

int character;
int secondletter;
int thirdletter;
int fourthletter;
int fifthletter;
int letter;
char testpassword[6];
char salt[3];



if (argc!= 2)
{
    printf("Error: please enter valid hashed password\n");
    return 1;
}

else
{
    string hashedpassword = argv[1];

    salt[0] = hashedpassword[0];
    salt[1] = hashedpassword[1];
    salt[2] = '\0';

    for (letter = 65; letter < 123; letter++)
    {
        if (letter == 91)
        {
            letter = 97;
        }

        testpassword[0] = letter;
        testpassword[1] = '\0';
        printf("%s\n", testpassword);

        if (strcmp(hashedpassword, crypt(testpassword, salt)) == 0)
        {
            printf("Original password: %s \n", testpassword);
            return 0;
        }

    }

    for (letter = 65; letter < 123; letter++)
    {
        if (letter == 91)
        {
            letter = 97;
        }

        for (secondletter = 65; secondletter < 123; secondletter++)
        {
            if (secondletter == 91)
            {
                secondletter = 97;
            }

            testpassword[0] = letter;
            testpassword[1] = secondletter;
            testpassword[2] = '\0';
            printf("%s\n", testpassword);


            if (strcmp(hashedpassword, crypt(testpassword, salt)) == 0)
            {
                printf("Original password: %s \n", testpassword);
                return 0;
            }

        }

    }

    for (letter = 65; letter < 123; letter++)
    {
        if (letter == 91)
        {
            letter = 97;
        }

        for (secondletter = 65; secondletter < 123; secondletter++)
        {
            if (secondletter == 91)
            {
                secondletter = 97;
            }

            for (thirdletter = 65; thirdletter < 123; thirdletter++)
            {
                if (thirdletter == 91)
                (thirdletter = 97);


                    testpassword[0] = letter;
                    testpassword[1] = secondletter;
                    testpassword[2] = thirdletter;
                    testpassword[3] = '\0';
                    printf("%s\n", testpassword);


                    if (strcmp(hashedpassword, crypt(testpassword, salt)) == 0)
                    {
                        printf("Original password: %s \n", testpassword);
                        return 0;
                    }

            }
        }

    }

    for (letter = 65; letter < 123; letter++)
    {
        if (letter == 91)
        {
            letter = 97;
        }

        for (secondletter = 65; secondletter < 123; secondletter++)
        {
            if (secondletter == 91)
            {
                secondletter = 97;
            }

            for (thirdletter = 65; thirdletter < 123; thirdletter++)
            {
                if (thirdletter == 91)
                (thirdletter = 97);

                for (fourthletter = 65; fourthletter < 123; fourthletter++)
                {
                    if(fourthletter == 91)
                    (fourthletter = 97);

                    testpassword[0] = letter;
                    testpassword[1] = secondletter;
                    testpassword[2] = thirdletter;
                    testpassword[3] = fourthletter;
                    testpassword[4] = '\0';
                    printf("%s\n", testpassword);


                    if (strcmp(hashedpassword, crypt(testpassword, salt)) == 0)
                    {
                        printf("Original password: %s \n", testpassword);
                        return 0;
                    }

                }
            }
        }

    }

    for (letter = 65; letter < 123; letter++)
    {
        if (letter == 91)
        {
            letter = 97;
        }

        for (secondletter = 65; secondletter < 123; secondletter++)
        {
            if (secondletter == 91)
            {
                secondletter = 97;
            }

            for (thirdletter = 65; thirdletter < 123; thirdletter++)
            {
                if (thirdletter == 91)
                (thirdletter = 97);

                for (fourthletter = 65; fourthletter < 123; fourthletter++)
                {
                    if(fourthletter == 91)
                    (fourthletter = 97);

                    for (fifthletter = 65; fifthletter < 123; fifthletter++)
                    {
                        if (fifthletter == 91)
                        fifthletter = 97;

                        testpassword[0] = letter;
                        testpassword[1] = secondletter;
                        testpassword[2] = thirdletter;
                        testpassword[3] = fourthletter;
                        testpassword[4] = fifthletter;
                        testpassword[5] = '\0';
                        printf("%s\n", testpassword);


                        if (strcmp(hashedpassword, crypt(testpassword, salt)) == 0)
                        {
                            printf("Original password: %s \n", testpassword);
                            return 0;
                        }

                    }
                }
            }
        }

    }

}
}
0

This assignment is notorious for taking a very long time to run for more than 3 letters. Depending on the code, it can take minutes for an efficient program to days for a badly written one.

The printf statements "show" that it is still running. When you take them out, it actually runs faster (output takes much more time to do), but the program is still running. It's probably just taking a lot of time, unless it's stuck in an infinite loop.

If you really want to see what's going on, put unique printf statements inside each for loop. More importantly, print the loop counter to see that it's progressing. Be forewarned that this will slow the program down substantially. Better still, put the printf inside an if statement so it only prints every 100 or 500 increments. (You can decide what's appropriate for your code.)

You should be able to determine the maximum number of times each loop can execute and see if it's going beyond that count.

Lots of alternatives here to see what's happening, so I leave it to you to determine if it's just taking a long time or it's stuck in a loop!

The bottom line is that if it's processing 5 letters, it's going to take time. Waiting for the final output is something between watching a sloth run and watching paint dry.

However, if the program actually ends and goes back to the system prompt, that's a totally different issue. The if statement is failing. Is this what is happening?

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

1
  • thanks so much for your feedback - I didnt know output slows down the program a lot. I took all the unnecessary printf's out and I got a result for 7 out of the 10 hashed passwords provided to us, which is great. the remaining three I ran out of patience for. nearly all of the ones I got were capitalised, I'm not sure if there's a reason for that or not...? – huevosysalchicha Nov 29 '18 at 21:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .