(Updated on 20170607. Please scroll down for the update)
I wrote the code about a year ago and it passed check50 before but I changed job so stopped the course for a while.
Now I resubmitted the program and it wouldn't pass check50 anymore.
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, string argv[])
{
// check arg number
if (argc == 2 || (int) argv[1] > 0)
{
string msg = GetString(); // message
int i; // loop for all characters
int n; // number of charaters
int key = atoi(argv[1]); // key to encrypt
// "rotate" all characters by "key"
for (i = 0, n = strlen(msg); i < n; i++)
{
int p = (msg[i] + key % 26); // character to print
// if input is symbols, print changed
if (msg[i] < 'A')
{
printf ("%c", msg[i]);
}
// if output outside of a-z & A-Z, rotate it back around and print
else if (p > 'z' || (p > 'Z' && p < 'a'))
{
printf ("%c", (char) (p - 26));
}
// if output within a-z or A-Z, print "rotated" output
else
{
printf ("%c", (char) p);
}
}
printf ("\n");
return 0;
}
// if too many arguments or input isn't a +int, yell and return 1
else
{
printf ("Usage: /home/cs50/pset2/caesar <number>\n");
return 1;
}
}
Even the "world, say hello!" got encrypted correctly:
~/workspace/pset2/ $ ./caesar 12
world, say hello!
iadxp, emk tqxxa!
However check50 comes back with this:
:) caesar.c exists
:) caesar.c compiles
:( encrypts "a" as "b" using 1 as key
\ expected output, not a prompt for input
:( encrypts "barfoo" as "yxocll" using 23 as key
\ expected output, not a prompt for input
:( encrypts "BARFOO" as "EDUIRR" using 3 as key
\ expected output, not a prompt for input
:( encrypts "BaRFoo" as "FeVJss" using 4 as key
\ expected output, not a prompt for input
:( encrypts "barfoo" as "onesbb" using 65 as key
\ expected output, not a prompt for input
:( encrypts "world, say hello!" as "iadxp, emk tqxxa!" using 12 as key
\ expected output, not a prompt for input
:) handles lack of argv[1]
Could anyone please tell me what went wrong?
P.S. I am a REAL BEGINNER and English is my third language. Please try to explain in very simple language. Thank you!
===
20170607 update:
Added a line to comply with the plaintext:
and ciphertext:
.
Here is the new code:
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, string argv[])
{
// check arg number
if (argc == 2 && (int) argv[1] > 0)
{
string msg = GetString(); // message
int i; // loop for all characters
int n; // number of charaters
int key = atoi(argv[1]); // key to encrypt
printf("plaintext: %s\nciphertext: ", msg);
// "rotate" all characters by "key"
for (i = 0, n = strlen(msg); i < n; i++)
{
int p = (msg[i] + key % 26); // character to print
// if input is symbols, print changed
if (msg[i] < 'A')
{
printf ("%c", msg[i]);
}
// if output outside of a-z & A-Z, rotate it back around and print
else if (p > 'z' || (p > 'Z' && p < 'a'))
{
printf ("%c", (char) (p - 26));
}
// if output within a-z or A-Z, print "rotated" output
else
{
printf ("%c", (char) p);
}
}
printf ("\n");
return 0;
}
// if too many arguments or input isn't a +int, yell and return 1
else
{
printf ("Usage: ./caesar k\n");
return 1;
}
}
Now my code ONLY WORKS SOMETIMES:
~/workspace/pset2/ $ ./caesar 13
Usage: ./caesar k
~/workspace/pset2/ $ ./caesar 13
Usage: ./caesar k
~/workspace/pset2/ $ ./caesar 13
world, say hello!
plaintext: world, say hello!
ciphertext: jbeyq, fnl uryyb!
~/workspace/pset2/ $ ./caesar 13
Usage: ./caesar k
Could anyone please help take a look at the program and tell me what went wrong? Thank you!