Answers to CS50x 2019 Problem Set 2: Arrays.
Caesar
// include cs50.h and stdio.h headers. Using #'s for readability but should use <>.
#include #cs50.h>
#include #stdio.h>
#include #string.h>
#include #ctype.h>
#include #stdlib.h>
int main(int argc, string argv[])
{
// check program runs with one command-line argument
if (argc == 2)
{
// separates string into characters. only accepts digits. ignore the hash.
string input = argv[1];
for (int i = 0; i <#strlen(input); i++)
{
printf("Usage: ./caesar key\n");
return 1;
}
//Convert that string to an actual integer
int k = atoi(argv[1]);
// prompt for input
string s = get_string("plaintext: ");
printf("ciphertext: ");
for (int i = 0; i < strlen(s); i++)
{
int c = (int) s[i];
if (c > 64 && c < 91)
{
int j = ((s[i] + k));
while (j > 90)
{
j = j - 90 + 64;
}
printf("%c", j);
}
else if (c > 96 && c < 123)
{
int j = ((s[i] + k));
while (j > 122)
{
j = j - 122 + 96;
}
printf("%c", j);
}
else
{
printf("%c", s[i]);
}
}
printf("\n");
return 0;
}
else
{
printf("Usage: ./caesar key\n");
return 1;
}
}