#include <stdio.h>
#include <cs50.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
int cypher(string k, char p);
int main(int argc, string argv[])
{
if (argc != 2)
{
printf("Usage: ./caesar key\n");
return 1;
}
int l = 0;
l = strlen(argv[1]);
for (int i = 0; i < l; i++)
{
if (isalpha(argv[1][i]) != 0)
{
printf("Usage: ./caesar key\n");
return 1;
}
else if (isdigit(argv[1][i]) == 0)
{
printf("Usage: ./caesar key\n");
return 1;
}
}
string a = get_string("plaintext: ");
int x = strlen(a);
char b[x];
char c[x];
for (int j = 0; j < x; j++)
{
b[j] = a[j];
c[j] = cypher(argv[1], b[j]);
}
printf("ciphertext: %s\n", c);
} // Main ends
int cypher(string kk, char pp)
{
int aa = pp - 0;
int ff;
if (isalpha(pp) == 0)
{
return aa;
}
else if (pp >= 'A' && pp <= 'Z')
{
int bb = aa - 65;
int cc = atoi(kk);
int dd = bb + cc;
int ee = dd % 26; // Remainder added to 26 alphabets.
ff = ee + 65;
return ff;
}
else
{
int bb = aa - 97;
int cc = atoi(kk);
int dd = bb + cc;
int ee = dd % 26;
ff = ee + 97;
return ff;
}
}
OUTPUT
:) 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
output not valid ASCII text
:) handles lack of key
:) handles non-numeric key
:) handles too many arguments