0
int main(int argc, char *argv[])
{
char alphabet[52], check[4];

//checks to see there is exactly 2 command-line inputs

if (argc != 2 )
{
    printf("Please enter exactly two command-line inputs");
    return 1;
}

string pw = argv[1];

printf("%s\n", pw);
printf("%s\n", crypt(pw,"50"));



//sets alphabet array to letters of the alphabet
alphabet[0] = ' ';
for (int alpha = 1; alpha <= 26; alpha++)
{
    alphabet[alpha] = (alpha + 64);
}

for (int alpha = 27; alpha <= 52; alpha++)
{
    alphabet[alpha] = alpha + 70;
}


// variable to determine the number of alphabet starting with A the code will try decode
int count = 2;

for (int i = 1; i <= count; i++)
{
    //resets every other character to ' '
    for (int x = 1; x <=4; x++)
    {
           check[x] = ' ';
    }

    //changes the 1st letter of check to the i'th letter of the alphabet
    check[0] = alphabet[i];
    printf("%s\n", check);
    printf("Strcmp is %i\n", strncmp(pw,check,strlen(pw)));

    if (strncmp(pw,check,strlen(pw)) == 0)
    {
        printf("Password is %s\n", check);
        return 0;
    }

    for (int j = 0; j <= count; j++)
    {
        check[1] = alphabet[j];

        printf("%s\n", check);
        printf("Strcmp is %i\n", strncmp(pw,check,2));
    }
}

So the code currently creates a 53 character array called alphabet which creates a blank ' ' then followed by A - z.

Then what I've tried to do is create nested loops to create a 4 digit code (variable: check[4]) for which each letter of the code is drawn from a character in the alphabet array.

I've printed check to see what is stored in the variable - in debug50 it appears to be storing the correct characters but when I run the code the output of ./crack BA becomes

A    ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
Strcmp is 1
A    ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
Strcmp is 1
AA   ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
Strcmp is 1
AB   ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
Strcmp is 1
B    ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
Strcmp is 33
B    ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
Strcmp is 33
BA   ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
Strcmp is 0
BB   ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz 

I don't understand why when I try output check it has the correct output ("A", "AB" ... etc) followed by the entire pw variable (A-z)

Thank you for your help in advance

Alex

IP002
  • 471
  • 1
  • 5
  • 17
Alex
  • 9
  • 1
  • 2
    You have 2 major problems. Indexing starts at 0, so in `check[4]` a valid index would be 0, 1, 2, 3 (but not 4 or larger). Also to use a char array with `printf` or any `str...` functions, the array must contain a `'\0'` terminator. So when you try to display `check` the output continues until `printf` stumbles over some 0 byte, somewhere. – Bo Persson Nov 10 '17 at 15:03
  • This is a problem from the cs50 course, they have a stack exchange of their own, you might have better luck posting over there – Joel Banks Nov 10 '17 at 15:56

0 Answers0