-
Notifications
You must be signed in to change notification settings - Fork 0
/
Week2-Caesar
55 lines (50 loc) · 1.21 KB
/
Week2-Caesar
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//Caesar cipher that accepts a single numeric command line argument
#include <stdio.h>
#include <cs50.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, string argv[])
{
//Verifies two command line arguments
if (argc != 2)
{
printf("Usage: ./caesar key\n");
return 1;
}
//Stores length of user input and verifies is numeric
int largv = strlen(argv[1]);
for (int i = 0; i < largv; i++)
{
if (isdigit(argv[1][i]) == 0)
{
printf("Usage: ./caesar key,\n");
return 1;
}
}
//Converts valid input to int, prompts user for plaintext
int k = atoi(argv[1]);
//printf("key: %i\n", k);
string pt = get_string("plaintext: ");
int lpt = strlen(pt);
char ct[lpt];
printf("ciphertext: ");
for (int i = 0; i < lpt; i++)
{
if (isupper(pt[i]))
{
printf("%c", (((pt[i] + k) - 65) % 26) + 65);
}
else if (islower(pt[i]))
{
printf("%c", (((pt[i] + k) - 97) % 26) + 97);
}
else
{
printf("%c", pt[i]);
}
//ct[i] = (ct[i] + k) % 26;
}
printf("\n");
return 0;
}