I've finally finished my Caesar code as can be seen below. However, when I run the check50, it says that it fails, but the output is correct. What am I doing wrong here? Why is it failing if the output is correct?
#include <stdio.h>
#include <cs50.h>
#include <math.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
int main(int argc, string argv[])
{
int k = 0;
string key = argv[1];
if (argc == 2)
{
for (k = 0; k < strlen(key); k++)
{
if (!isdigit(key[k]))
{
printf("\nUsage: %s key\n", argv[0]);
return 1;
}
}
}
else
{
printf("\nUsage: %s key\n", argv[0]);
return 1;
}
string plaintext = get_string("\nplaintext: ");
printf("ciphertext: ");
for (int i = 0; i < strlen(plaintext); i++)
{
char c = plaintext[i];
int keycode = atoi(key);
if (isupper(c))
{
printf("%c", (((c + keycode) - 'A') % 26) + 'A');
}
if (islower(c))
{
printf("%c", (((c + keycode) - 'a') % 26) + 'a');
}
else if (isspace(c) || isdigit(c) || ispunct(c))
{
printf("%c", c);
}
}
}
The check50:
:) caesar.c exists.
:) caesar.c compiles.
:( encrypts "a" as "b" using 1 as key expected "ciphertext: b...", not "ciphertext: b"
:( encrypts "barfoo" as "yxocll" using 23 as key expected "ciphertext: yx...", not "ciphertext: yx..."
:( encrypts "BARFOO" as "EDUIRR" using 3 as key expected "ciphertext: ED...", not "ciphertext: ED..."
:( encrypts "BaRFoo" as "FeVJss" using 4 as key expected "ciphertext: Fe...", not "ciphertext: Fe..."
:( encrypts "barfoo" as "onesbb" using 65 as key expected "ciphertext: on...", not "ciphertext: on..."
:( encrypts "world, say hello!" as "iadxp, emk tqxxa!" using 12 as key expected "ciphertext: ia...", not "ciphertext: ia..."
:) handles lack of argv[1]