I'm doing caesar in pset 2 and it's working well when i enter ./caesar 1, or ./caesar a, or ./caesar 1 2, but when i try ./caesar 2h, it still runs the program. A couple of other people asked this question but none of the answers helped.Here's my code:
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
int main(int argc, string argv[])
{
//stop if more than one key and if not a digit
if (argc != 2 || isalpha(*argv[1]))
{
printf("Usage: ./caesar key\n");
return 1;
}
int key = atoi(argv[argc - 1]);
//ask user for string
string ptxt = get_string("Plaintext:");
//initialize the char ctxt to be as long as the string ptxt
char ctxt [strlen(ptxt)];
//run program, but keep uppercase/lowercase and non alphabetical characters
for (int i = 0; i < strlen(ptxt); i++)
if (isalpha(ptxt[i]))
{
if (isupper(ptxt[i]))
{
ctxt[i] = (((ptxt[i] - 65 + key) % 26) + 65);
}
else if (islower(ptxt[i]))
{
ctxt[i] = (((ptxt[i] - 97 + key) % 26) + 97);
}
}
else
{
ctxt[i] = ptxt[i];
}
//print the ciphertext
printf("ciphertext: %s", ctxt);
printf("\n");
return 0;
}