I just started learning computer science.
I'm studying through CS50 taught at Harvard online. Well, I'm working on this one problem where I need to get the key from the user in command line, then a plaintext, and then shift that text for key amount of numbers in ASCII to make a ciphertext.
here is what I've got so far.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
int main(int argc, string argv[])
{
if (argc != 2)
{
printf("Usage: ./caesar key\n");
}
{
string plaintext = get_string("plaintext: ");
int key = atoi(argv[1]);
int n = strlen(plaintext);
char chr[n];
printf("ciphertext: ");
for (int i = 0; i < n; i++)
{
chr[i] = plaintext[i];
if (isalpha(chr[i]))
{
if (isupper(chr[i]))
{
chr[i] = (chr[i] - 65 + key) % 26 + 65;
printf("%c",chr[i]);
}
else if (islower(chr[i]))
{
chr[i] = (chr[i] - 97 + key) % 26 + 97;
printf("%c",chr[i]);
}
}
else
{
printf("%c",chr[i]);
}
}
printf("\n");
}
}
well I know this seems very floppy but man it's my second week into programming while working full time.
anyways, I'm trying to have the user to run this program by using ./caesar "any number for the key".
if the user puts in any other things, then I'd like to print "Usage: ./caesar key\n"
so far the only way i can think of is making the if statement with argc != 2 so that I can at least make sure the user puts in just one command on top of the program's name.
but the problem is that if the user puts in other things such as ./caesar HELLO ./caesar YolO
the program still runs.
I'm trying to figure out what I can do in order to prevent that from happening.
Really appreciate your time for reading this and help.