У меня есть еще один вопрос по поводу моих программ шифрования кода Цезаря. Здесь я указал, что хочу брать только целое число. Но каким-то образом это происходит, когда в аргументах командной строки вводится 2x.
Ниже приведен полный код ... Я глубоко признателен, если кто-нибудь может помочь мне ответить на мой вопрос.
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#include <stdlib.h>
//one command line argument with the type int
int main(int argc, string argv[])
{
//main program
if (argc == 2 && isdigit(*argv[1]))
{
int k = atoi(argv[1]); //get Caesar key into a variable
string pltext = get_string("plaintext: "); //getting input for the plain text
char cptext[strlen(pltext) + 1];
for (int i = 0, n = strlen(pltext) ; i < n; i++) //turning pltext to integer
{
if (pltext[i] >= 'a' && pltext[i] <= 'z')
{
cptext[i] = ((pltext[i] - 'a' + k) % 26) + 'a'; //shifting the integer with k (lowercase)
}
else if (pltext[i] >= 'A' && pltext[i] <= 'Z')
{
cptext[i] = ((pltext[i] - 'A' + k) % 26) + 'A'; //shifting the integer with k (uppercase)
}
else
{
cptext[i] = pltext[i]; //other symbol stays
}
}
//print out result
printf("ciphertext: %s\n", cptext);
return 0;
}
//setting condition that : K = + ; if more or less than one, immediate error message, return 1
//if not decimal return = usage ./caesar. key, return value 1 to main
else if (argc != 2)
{
printf("Error 404 : \n");
return 1;
}
else
{
printf("Usage: ./caesar key\n");
return 2;
}
}