Code:
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <ctype.h>
int main(int argc, string argv[])
{
int key = 0;
string plaintext;
if (argc != 2)
{
printf("Usage: ./caesar key \n");
}
else
{
key = atoi(argv[1]);
plaintext = get_string("Plaintext: ");
printf("\n");
printf("Ciphertext: ");
for (int j = 0, n = strlen(plaintext); j < n; j++)
{
if (isupper(plaintext[j]))
{
printf("%c", (((plaintext[j] + key) - 65) % 26) + 65);
}
else if (islower(plaintext[j]))
{
printf("%c", (((plaintext[j] + key) - 97) % 26) + 97);
}
else
{
printf("%c", plaintext[j]);
}
}
printf("\n");
}
}
Run log:
~/pset2/caesar/ $ ./caesar 1
Plaintext: a
Ciphertext: b
I am working on pset2, caesar.
How can I check if my key is numerical or not?
I have tried many ways but failed, does someone know of a way I can do that? I am a beginner.