0

All the check50 inputs are working fine, apart from the one where the input is "The quick brown fox jumps over the lazy dog". In this case my code works fine till 'jumps' and after that the code doesn't behave properly even though the same characters when encountered before yields the expected result. Could somebody help out on how to point the error. Here is my code:

include

include

include

include

int main(int argc, string argv[]) { // Checking if the number of arguments are correct

if (argc != 2)
{
    printf("Usage: %s KEY\n", argv[0]);
    return 1;
}

// Checking if the number of alphabets in the second argument are correct

string key = argv[1];
if (strlen(key) != 26)
{
    printf("Key must contain 26 alphabets.\n");
    return 1;
}

// Checking for non-alphabets

for (int i = 0; i < strlen(key); i++)
{
    if (!isalpha(key[i]))
    {
        printf("Key must only contain alphabets.\n");
        return 1;
    }
}

// Checking for repeatition of alphabets

int counter = 0;

for (int i = 0; i < strlen(key); i++)
{
    for (int j = 0; key[j] != '\0'; j++)
    {
        if (key[i] == key[j])
        {
            counter++;
        }
    }
}
if (counter != strlen(key))
{
    printf("Each alphabet must occur only once.\n");
    return 1;
}
else
{
    // getting Input from User
    string input = get_string("plaintext: ");
    printf("ciphertext: ");

    // If the key is uppercase converting to lowercase

    for (int i = 0; i < strlen(key); i++)
    {
        if (isupper(key[i]))
        {
            key[i] = key[i] + 32;
        }
        else
        {
            key[i] = key[i];
        }
    }

    // Encoding

    for (int i = 0; i < strlen(input); i++)
    {
        // If the input has alphabets

        if (isalpha(key[i]))
        {
            // When the input has uppercase

            // changing the ASCII value of character to a value that corresponds to new Key
            // Suppose input has 'H'. Now Ascii value of 'H' is 72. Subtracting 65 gives us 7.
            // changing 'h' to a character that is resides at 7th place of New Key
            // Subtracting 32 to the character as the original character was in uppercase

            if (isupper(input[i]))
            {
                int place_holder = (int) input[i] - 65;
                input[i] = key[place_holder];
                input[i] = input[i] - 32;
            }

            // When the input has lowercase

            // changing the ASCII value of character to a value that corresponds to new Key.
            // Suppose input has 'h'. Now Ascii value of 'h' is 104. Subtracting 97 gives us 7.
            // changing 'h' to a character that is resides at 7th place of New Key

            if (islower(input[i]))
            {
                int place_holder = (int) input[i] - 97;
                input[i] = key[place_holder];
            }
        }

        // When the input has characters other than alphabets

        else
        {
            input[i] = input[i];
        }
    }

    // Output

    printf("%s\n", input);
}

}

1 Answer 1

0
// Encoding

for (int i = 0; i < strlen(input); i++)
{
    // If the input has alphabets

    if (isalpha(key[i]))

In the last line of this section of your code, you're checking if the i-th character of the key is a letter, but should be checking if the i-th character of the input text is a letter. This is working for inputs with a length below 26, but you're having problems when the input is longer than 26 characters. You'd probably also have problems with inputs that have characters that aren't letters, but there aren't any in check50.

You must log in to answer this question.

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