0

My code seems to work correctly but doesn't pass check50. This is my first post.

int main(int argc, string argv[])

{
    //get the key from my 1st argument at cmd line (the 2nd in array, 1st argument being program name
    //'vigenere')
    string codeword =argv[1];

    // check for 2 arguments only
        if (argc != 2)
    {
    printf("Please enter only 1 text word with only alphabetic letters.\n");
    return 1;
    }

    for (int c= 0; c<strlen(codeword); c++)
    {
        char letter=codeword[c];
        if(!isalpha(letter))
        {
            printf("Please enter only 1 text word with only alphabetic letters.\n");
            return 1;
        }
    }

        //prompt for plain text to decipher
        string text = get_string("Please enter some text to decode with no numbers or special characters: \n");

    // initialize a counter to go through codeword array
        int c = 0;
    // loop through the string as an array of letters
        for (int i = 0; i < strlen(text); i++)
        {
            //loop through length of codeword instead of length of alphabet
            // ci=(pi+kj)%26 (model) loops through alphabet
            c = c % strlen(codeword);

            if (isalpha(text[i]))
            {
                //check if the text letter and codeword letter are both uppercase
                if ( isupper(text[i])  && isupper(codeword[c]) )
                    {
                        // then convert to alpha index, 66 being a in ascii, then covert back to ascii
                        printf("%c", ((( (text[i]-65) + (codeword[c]-65) ) %26 ) +65));
                        c++;
                    }
                else if ( (isupper(text[i])) && islower(codeword[c]) )
                    {
                //         // then convert to alpha index, 66 being a in ascii, then convert back to ascii
                        printf("%c", ((((text[i]-65) + (codeword[c]-97)) %26)+65));
                        c++;
                    }
                // //check if the text letter and codeword letter are both lowercase
                else if ( islower(text[i]) && islower(codeword[c]) )
                    {
                        // then convert to alpha index, 97 being a in ascii, then convert back to ascii
                        printf("%c", ((((text[i]-97) + (codeword[c]-97)) %26)+97));
                        c++;
                    }
                else if (islower(text[i]) && isupper(codeword[c]))
                    {
                        printf("%c", ((((text[i]-97) + (codeword[c]-65)) %26)+97));
                        c++;
                    }
            //close the -if isalpha condition
            }
                else
            {
                printf("%c", text[i]);
            }
        //close the for loop
        }
        printf("\n");
}
3
  • My code seems to work correctly but doesn't pass check50. This is my first post, and I used Ctrl-K as recommended to format code. My apologies in advance if I did something incorrectly. – Marie Urbina May 5 '18 at 18:13
  • To enter a code block properly, first copy the code to the question. Then highlight it. Finally, click on the {} curly braces above the entry window. That will move the entire block over 4 spaces so that it is displayed as a code block. Also, in the future, please include an explanation of the problem in the question itself. – Cliff B May 5 '18 at 18:53
  • Will do thanks. – Marie Urbina May 5 '18 at 20:11
0

"When all else fails, read the directions." ;-) There's a hidden lesson in the first few psets. "Write the code exactly to the specification, with no alterations or 'improvements'." This is particularly important in team programming environments where each person writes a piece of the code and it all has to fit together perfectly in the end.

Running your code produces the following partial result:

:( encrypts "a" as "a" using "a" as keyword
    expected "ciphertext: a\n", not "a\n"

Notice the difference between what's expected and what it saw? The output is supposed to say "ciphertext:" at the beginning of the output line.

As a side note, it would have also failed for another reason if a human were checking this. The code prompts for input with this:

Please enter some text to decode with no numbers or special characters:

Numbers and special characters are perfectly valid for plaintext input! Since check50 isn't set up to look at this, it passed.

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

1
  • Thank you! OMG, I can't believe I missed that, after I already had the "ciphertext: " in Caesar. You saved me a lot of headbanging! You are correct, that I also misunderstood at first that numbers and special characters can be in the plaintext, they just can't be in the key. – Marie Urbina May 5 '18 at 20:10

You must log in to answer this question.

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