I think the most of my code is sound, but when I run Check50, it looks like perhaps my newline escape is not where it needs to be. I've moved it around some, but know I don't want it in my for loop. I'm wondering if to pass the check I have to set up my for loop as a function? Thanks in advance for any help!
Here are the errors I'm getting:
:) caesar.c exists
:) caesar.c compiles
:( encrypts "a" as "b" using 1 as key
\ expected output, but not "ciphertext:b\u0000\n"
:( encrypts "barfoo" as "yxocll" using 23 as key
\ expected output, but not "ciphertext:yxocll\u0000\n"
:( encrypts "BARFOO" as "EDUIRR" using 3 as key
\ expected output, but not "ciphertext:EDUIRR\u0000\n"
:( encrypts "BaRFoo" as "FeVJss" using 4 as key
\ expected output, but not "ciphertext:FeVJss\u0000\n"
:( encrypts "barfoo" as "onesbb" using 65 as key
\ expected output, but not "ciphertext:onesbb\u0000\n"
:( encrypts "world, say hello!" as "iadxp, emk tqxxa!" using 12 as key
\ expected output, but not "ciphertext:iadxp, emk tqxxa!\u0000\n"
:) handles lack of argv[1]
And here is my code:
int main(int argc, string argv[])
{
//Tests that user input argv[1]
if (argc != 2)
{
printf("Please add a second command-line argument!\n");
return 1;
}
//Converts key from string to int
int k = atoi(argv[1]);
//Gets plaintext input from user
printf("plaintext:");
string p = GetString();
//Begins to print ciphertext result
printf("ciphertext:");
for (int i = 0, n = strlen(p); i <= n; i++)
{
char c = (p[i]);
if (isalpha(c))
{
if (isupper(c))
{
c = c - 65;
printf("%c", ((c + k) % 26) + 65);
}
else if (islower(c))
{
c = c - 97;
printf("%c", ((c + k) % 26) + 97);
}
}
else
{
printf("%c", c);
}
}
printf("\n");
return 0;
}