I am tearing my hair out on this exercise. I cannot get it to pass a non-numeric key. I've played around with the code for 2 weeks now. I can either get it to handle a numeric key or handle a lack of key, but not both. I've played around with isdigit and isalpha, and do while and if else statements. I feel like im missing something really obvious but for the life of me I cant get it. I didnt want to print my entire code but I cant figure out where my error is to show you so I will have to. Thank you for any advice you can give me.
#include <cs50.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
int main(int argc, string argv[])
{
string Key = argv[1]; //name string
if (argc != 2) //if argc = 1 + 1??
{
printf("Usage: ./caesar Key \n");
return 1;
}
int k = atoi(Key) % 26; // convert string to int with stdlib and atoi
if (k <= 0) //if int has 0 value
{
printf("Usage: ./caesar Key \n");
return 1;
}
string Input = get_string("plaintext: "); //get unciphered text
printf("ciphertext: \n"); //return ciphertext
for (int i = 0, n = strlen(Input); i < n; i++) //loop to encrypt letters individually
{
int x = 0;
if (isupper(Input[i]))
{
x = (((int)Input[i] - 65 + k) % 26) + 65;
printf("%c", (char)x);
}
else if (islower(Input[i]))
{
x = (((int)Input[i] - 97 + k) % 26) + 97;
printf("%c", (char)x);
}
else
{
printf("%c", Input[i]);
}
}
printf("\n");
return 0;
}