int main(int argc, string argv[]) { //Declare variables
int main(int argc, string argv[])
{
//Declare variables
int k; //cipher key
int i; //counting variable
string plaintext;
/* Part I - verify that cipher code entered is a single command-line argument
and that is is a positive digit. */
if (argc != 2) // verify single command-line argument
{
printf("Usage: ./caesar key\n"); // if false, error message and program prematurely terminates
return 1;
}
else
{
for (i = 0; i < strlen(argv[1]); i++) // verify cipher is a digit
if (isdigit(argv[1][i]) && argv[1][i] > 0)
{
k = atoi(argv[1]); // if true, convert to a real integer
}
else
{
printf("Usage: ./caesar key\n"); // if false, print error message and terminate program prematurely
return 1;
}
}
/* Part II - get a message from user, convert it to ciphertext using the Caesar cipher (based on
ci=(pi=k) %26 ) and output coded message. */
//Input and Output
plaintext = get_string("Plaintext: "); // prompt user for message for encryption
printf("Ciphertext: "); // output of encrypted message
for (i = 0; i <= strlen(plaintext); i++) // iterate plaint text for ciphertext
{
if (isupper(plaintext[i])) // convert upper case letters to cipher wrapping from z to a
{
printf("%c", (((plaintext[i] - 'A') + k) % 26) + 'A');
}
else if (islower(plaintext[i])) // convert lower case letters to cipher wrapping from z to a
{
printf("%c", (((plaintext[i] - 'a') + k) % 26) + 'a');
}
else // leave all spaces and punctuation as is
{
printf("%c", plaintext[i]);
}
}
printf("\n");
return 0;
}
}