0

After many many hours of trying to accomplish Caesar, check50 only yeilds one error:

  • :) caesar.c exists.
  • :) caesar.c compiles.
  • :) encrypts "a" as "b" using 1 as key
  • :) encrypts "barfoo" as "yxocll" using 23 as key
  • :) encrypts "BARFOO" as "EDUIRR" using 3 as key
  • :) encrypts "BaRFoo" as "FeVJss" using 4 as key
  • :( encrypts "barfoo" as "onesbb" using 65 as key expected "ciphertext: one...", not "ciphertext: ihy..."
  • :) encrypts "world, say hello!" as "iadxp, emk tqxxa!" using 12 as key
  • :) handles lack of argv[1]

I'm not sure why it's the only error. My code is:

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

if (argc != 2)
{
    printf("Error -102: Invalid User Input\n");
    return 1;
}
else
{

    string message;

    do
    {
        message = get_string("plaintext: ");
    }
    while (!message);
    printf("ciphertext: ");

    int key = atoi(argv[1]);

    char fin;

    for (int i = 0; i < strlen(message); i++) {

        char ch = message[i];
        int c = message[i];

        int add = c;
        if (add > 90)
        {
            add = add - 32;
        }
        add = add - 65;






       if (add < 0 || add >= 155)
       {
           fin = message[i];
           printf("%c", fin);
       }
       else
       {


            add = add + key;

            if ((add + 65) > 90) {
                add = add + 39;

            }
            else {
                add = add + 65;
            }

            fin = (char) add;
            if (islower(ch)) {
                printf("%c", tolower(fin));
            }
            else {
                printf("%c", toupper(fin));
            }




       }



    }
    printf("\n");

}

}

Thank you for any and all help you can offer!

1 Answer 1

1

When the key is sufficiently large enough, the algorithm that was designed here won't work. Imagine if the key were 1000. The results generated will not even be a valid ASCII code. A key of 65 is large enough to expose this problem but small enough to still generate letters as a result.

A rethink is in order. When adding the key, it's necessary to wrap around from z to a for every 26 count.

I'd recommend going back and reviewing the lectures, walkthroughs and other material related to this pset.

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

2
  • Thank you for your help! However, I would say that the instructions and walkthroughs for this problem are slightly misleading since they only talk about having to wrap around the alphabet once. Thanks again!
    – Rjspareiii
    Commented Sep 9, 2018 at 17:49
  • Well, consider this a lesson in what happens in the real world. What may seem straightforward can have scenarios not thought about initially, but turns up later on. It still has to be handled by the code before release. Or perhaps conditions are interpreted one way by one person and another way by another. (btw, I am not a staff member, just a programmer with a lot of experience.) ;-)
    – Cliff B
    Commented Sep 10, 2018 at 1:19

You must log in to answer this question.

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