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;
}
}