0

I coded this, and all works fine when I insert the same examples given in the specs, but somehow the CS50 checkers reads the "plaintext: " string as the solution! Anybody wise and kind to give a hand?

# include <stdio.h>
# include <cs50.h>
# include <string.h>
# include <ctype.h>
# include <stdlib.h>
# define ALPHABET 26
# define ASCIIUPPER_A 65
# define ASCIILOWER_a 97 

int main(int argc, string argv[])
{
    if (argc == 2)
    {
        printf ("plain text: ");
        string p = get_string();
        int k = atoi(argv[1]);
        printf("ciphertext: ");
        for (int i = 0, n = strlen(p); i < n; i++)
            {
                /* ith char of the array p assigned to letter */
                int letter = p[i];
                /* upper and lower case ASCII chars indexed on alphabetical index starting at 0 = a or A */
                int ASCII_uppercase_on_alphabetical_index = (letter - ASCIIUPPER_A);
                int ASCII_lowercase_on_alphabetical_index = (letter - ASCIILOWER_a);
                /* indexed letter moved by the key on alphabetical index */
                int uppercase_encrypted_on_alphabetical_index = (ASCII_uppercase_on_alphabetical_index + k) % ALPHABET;
                int lowercase_encrypted_on_alphabetical_index = (ASCII_lowercase_on_alphabetical_index + k) % ALPHABET;
                /* letter encrypted in ASCII */
                int ASCII_uppercase_encrypted = (ASCIIUPPER_A + uppercase_encrypted_on_alphabetical_index);
                int ASCII_lowercase_encrypted = (ASCIILOWER_a + lowercase_encrypted_on_alphabetical_index);

                if (isalpha(letter) && isupper(letter))
                {
                    printf("%c", (char)ASCII_uppercase_encrypted);
                }
                else if (isalpha(letter) && islower(letter))
                {
                    printf("%c", (char)ASCII_lowercase_encrypted);
                }
                else 
                {
                    printf("%c", letter);
                }
            }
        return 0;
    }
    else 
    {
        printf("Usage: ./ceasar k \n");
        return 1;
    }
}

1 Answer 1

1

There is a subtle but significant difference between what the spec requires and what your program produces.

What the spec requires:

Your program must output plaintext: (without a newline) and then prompt the user

What your program produces:

printf ("plain text:");

3
  • Hey! Thanks for your answer. I still don't get it though. Is it not exactly what my code does? If arg2 = 2 then output "plaintext: " and prompt the user for a string that gets assigned to p? Jan 17, 2017 at 14:46
  • Space, the final frontier. There is a difference between plaintext: and plain text: Jan 17, 2017 at 14:48
  • Dear dear Lord. Jan 17, 2017 at 14:51

You must log in to answer this question.

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