0

One last issue with my Substitution code. I keep getting the following error when I run check50: :( handles multiple duplicate characters in key timed out while waiting for program to exit

My solution for the error code if a user inputs duplicate chars was to convert the key to all lowercase, and then to check whether the sum of all those letters' ASCII values was NOT 2847, which should be the sum of a through z. This worked, and threw the error code when I tried a few keys with duplicate chars. But not when check50 runs.

I should add that when I run only this part of my code as its own program, it works correctly. I returns the error message if i input keys with duplicates, and it doesn't return an error if I input a valid key.

Any thoughts?

Here's the code:

#include <cs50.h> #include <stdio.h> #include <ctype.h> #include <string.h>

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

int lenkey =  strlen(argv[1]);

int sumlower = 0;
for (int j = 0; j < lenkey; j++)
{
    if (isupper(argv[1][j]))
    {
        argv[1][j] += 32;
    }
    sumlower += argv[1][j];
}
if (sumlower != 2847)
{
    printf("Key must not use any letter more than once\n");
    return 1;
}

// Don't need this. Just to verify.
else
{
    printf("Valid key. First letter: %c\n", argv[1][0]);
    return 0;
}

}

1 Answer 1

0

I figured out what's wrong. I did not account for the possibility that a combination of 26 letters with duplicates in it could also add up to 2847. The key with duplicate chars that check50 uses to test whether my code will throw an error does exactly that! Haha. (The sum of the ASCII values of their key, mmccefghijklmnopqrqtuvwxez, is the same as the sum of abcdefg...z)

I need a new approach...

You must log in to answer this question.

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