#include <cs50.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
//create key
string key = argv[1];
//check if number of argument is not 2
if (argc != 2)
{
printf("Usage: %s key\n", argv[0]);
return 1;
}
//convert string to int
int k = atoi(key);
//prompt user for plaintext
string p = get_string("plaintext: ");
printf("ciphertext:");
int n = strlen(p);
//shift letters only
//preserve case and iterate over each character
//ASCII value then encipher back to ASCII
for (int i = 0; i < n; i++)
{
if (isalpha(p[i]))
{
if (isupper(p[i]))
{
printf("%c", (((p[i] + k) - 65) % 26) + 65);
}
if (islower(p[i]))
{
printf("%c", (((p[i] + k) - 97) % 26) + 97);
}
}
else //print character as is
{
printf("%c", p[i]);
}
}
//print newline
printf("\n");
//program exit
return 0;
}
This is almost done but not quite. I keep getting ":( handles non-numeric key; timed out while waiting for program to exit. I was thinking isdigit but I'm not sure that would work with atoi. Help please.