CS50x Caesar

Photo by Kai Dahms on Unsplash
$ ./caesar 1
plaintext: Hello
ciphertext: Ifmmp
$ ./caesar
Usage: ./caesar key
// Get Key
// Make sure parameters are valid
// Get text
// Encrypt with given formula
// Print ciphertext
#include <stdio.h>
#include <cs50.h>
#include <ctype.h>
#include <stdlib.h>
void caesarCode(char*, int);int main(int argc, char *argv[])
{
//Validate key here
char *plaintext = get_string("Enter text to encrypt: ");
caesarCode(plaintext, key);
}
void caesarCode(char* t, int k)
{

}
if (argc != 2)
{
printf("Usage: ./caesar key\n");
return 1;
}

for (int i = 0; argv[1][i] != '\0'; i++)
{
if (isalpha(argv[1][i]) != 0)
{
printf("Usage: ./caesar key\n");
return 1;
}
}
int key = atoi(argv[1]);
char *plaintext = get_string("Enter text to encrypt: ");
printf("ciphertext: ");

for (int i = 0; t[i] != '\0'; i++)
{
if (isalpha(t[i]) != 0)
{
if (isupper(t[i]) != 0)
{
printf("%c", ((t[i] - 'A' + k) % 26) + 'A');
}
else
{
printf("%c", ((t[i] - 'a' + k) % 26) + 'a');
}
}
else
{
printf("%c", t[i]);
}
}
printf("\n");
Given: key = 1         plaintext index = 1         plaintext = ABEL
% symbol means modulo
printf("%c", ((t[0] - 'A' + k) % 26) + 'A');
(65 - 65 + 1) % 26 + 65 <- Tis is ASCII
Tis the alpha index -> 1 % 26 + 65
1 + 65
now back to ASCII -> 66
ASCII 66 = B
Results generated by style50 v2.7.4
Looks good!
Results for cs50/problems/2020/x/caesar generated by check50 v3.1.2
:) caesar.c exists.
:) caesar.c compiles.
:) encrypts "a" as "b" using 1 as key
:) encrypts "barfoo" as "yxocll" using 23 as key
:) encrypts "BARFOO" as "EDUIRR" using 3 as key
:) encrypts "BaRFoo" as "FeVJss" using 4 as key
:) encrypts "barfoo" as "onesbb" using 65 as key
:) encrypts "world, say hello!" as "iadxp, emk tqxxa!" using 12 as key
:) handles lack of key
:) handles non-numeric key
:) handles too many arguments