0

First post, so let me know if anything is wrong with the post.

My code passes every test except for this one.

:( encrypts all alphabetic characters using DWUSXNPQKEGCZFJBTLYROHIAVM as key expected "ciphertext: Rq...", not "ciphertext: Rq..."

Anyone have a clue what's going on here?

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

    int main(int argc, string argv[])
    {
        if (argc != 2)
        {
            printf("NO KEY");
            return 1;
        }
        else
        {
            string alphabet = "abcdefghijklmnopqrstuvwxyz";
            string inputr = argv[1];
            int input[26];
            int i = 0;
    while (inputr[i] != '\0')
    {
        input[i] = tolower(inputr[i]);
        i = i + 1;
    }

    i = 0;
    int j = 0;
    int lettercount = 1;

    while (alphabet[j] != '\0')
    {
        if (lettercount != 1)
        {
        printf("error3");
            return 1;
        }
        else
        {
            j = j + 1;
            i = 0;
            lettercount = 0;
            while (input[i] != '\0')
            {
                if (alphabet[j] == input[i])
                {
                    lettercount = lettercount + 1;
                    i = i + 1;
                }
                else
                {
                    i = i + 1;
                }
            }
        }
    }

    i = 0;
    while (input[i] != '\0')
    {
        i = i + 1;
    }

    i = 0;
    int scrambler[26];
    while (input[i] != '\0')
    {
        scrambler[i] = input[i] - alphabet[i];
        i = i + 1;
    }

    string plaintext = get_string("plaintext: ");
    printf("ciphertext: ");
    i = 0;
    j = 0;
    int returntext[26];
    while (plaintext[i] != '\0')
    {
        while (alphabet[j] != '\0' && plaintext[i] != '\0')
        {
            if (tolower(plaintext[i]) > 'z' || tolower(plaintext[i]) < 'a')
            {
                returntext[i] = plaintext[i];
                printf("%c", returntext[i]);
                i = i + 1;
                j = 0;
            }
            else if (tolower(plaintext[i]) == ' ')
            {
                returntext[i] = plaintext[i];
                printf("%c", returntext[i]);
                i = i + 1;
                j = 0;
            }
            else if (tolower(plaintext[i]) == alphabet[j])
            {
                returntext[i] = plaintext[i] + scrambler[j];
                printf("%c", returntext[i]);
                i = i + 1;
                j = 0;
            }
            else
            {
                j = j + 1;
            }
        }
    }
    printf("\n");
    return 0;
}

}

0

Interesting that the only test that fails is failing because the encoding is incomplete. It encodes 27 chars. That should be very conspicuous. It's 26 letters, plus 1. Are there any strings in this program that are 27 chars long, like 26 letters plus an end of string marker? Or more than one, and if so, which one related to the issue and why? ;-)

Yes, it is significant. Take a close look at your encoding loops. I have two questions.

First, is there a variable that should be reinitialized on each pass of the outer while loop?

Second, why is returntext[] limited to 26 letters? It holds the encoded text. Shouldn't it be the same length as the input plain text?

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

You must log in to answer this question.

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