#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cs50.h>
#include <ctype.h>
int main(int argc, string argv[])
{
if (argc != 1)
{
// get key from command line argument
string key = argv[(argc - 1)];
// turn key into integer
int k = atoi(key);
if ( k != 0 )
{
// prompt for plaintext
string text = get_string("Please type your text: ");
int l = strlen(text);
// for each character in the plaintext string
if (l!= 0)
{
for (int i=0; i<l; i++)
{
// if alphabetic
if (isalpha(text[i]))
{
// preserve case
if(isupper(text[i]))
{
// shift plaintext character by key
// convert to alphabetical index
int o = text[i] - 'A';
// convert to ciphertext letter: c = (p+k)%26
int c = (o+k)%26;
// convert to ascii for upper case char
text[i] = c + 'A';
}
else
{
// shift plaintext character by key
// convert to alphabetical index
int o = text[i] - 'a';
// convert to ciphertext letter: c = (p+k)%26
int c = (o+k)%26;
// convert to ascii for lower case char
text[i] = c + 'a';
}
}
}
// print ciphertext
printf("The crypto-text is: %s\n", text);
}
}
}
}