-
Notifications
You must be signed in to change notification settings - Fork 46
/
caesar.c
75 lines (63 loc) · 1.41 KB
/
caesar.c
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
int validate_key(int argc, string argv[]);
int main(int argc, string argv[])
{
int key;
if (validate_key(argc, argv) == -1)
{
printf("Usage: ./caesar key\n");
return 1;
}
else
{
key = validate_key(argc, argv);
}
string plain_txt = get_string("plaintext: ");
printf("ciphertext: ");
for (int i = 0, n = strlen(plain_txt); i < n; i++)
{
if (isalpha(plain_txt[i]))
{
int cipher = (plain_txt[i] + key);
int shift;
if (isupper(plain_txt[i]))
{
shift = 65;
}
else
{
shift = 97;
}
// calculates new char position (A=0, B=1..), accounts for wrapping back from Z to A
cipher = (cipher - shift) % 26;
// add shift back to print ASCII value as char
printf("%c", cipher + shift);
}
else
{
printf("%c", plain_txt[i]);
}
}
printf("\n");
return 0;
}
int validate_key(int argc, string argv[])
{
if (argc != 2)
{
return -1;
}
for (int i = 0, n = strlen(argv[1]); i < n; i++)
{
if (!isdigit(argv[1][i]))
{
return -1;
}
}
int key = atoi(argv[1]);
return key;
}