-
Notifications
You must be signed in to change notification settings - Fork 0
/
substitution.c
81 lines (74 loc) · 1.51 KB
/
substitution.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
76
77
78
79
80
81
#include <cs50.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
bool check_key(string s);
bool check_duplicates(string s);
char substitution(char p, string c);
int main(int argc, string argv[])
{
if (argc == 1 || argc > 2 || check_key(argv[1]) == false || check_duplicates(argv[1]) == false)
{
printf("Usage: ./substitution key(with 26 different alphabets)\n");
return 1;
}
else
{
string plaintext = get_string("plaintext: ");
printf("ciphertext: ");
for (int i = 0, n = strlen(plaintext); i < n; i++)
{
printf("%c", substitution(plaintext[i], argv[1]));
}
printf("\n");
return 0;
}
}
bool check_key(string s)
{
bool flag = true;
if (strlen(s) != 26)
{
flag = false;
}
else
{
for (int i = 0, n = strlen(s); i < n; i++)
{
if (isalpha(s[i]) == 0)
{
flag = false;
break;
}
}
}
return flag;
}
bool check_duplicates(string s)
{
bool flag = true;
for (int i = 0, n = strlen(s); i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (s[i] == s[j])
{
flag = false;
break;
}
}
}
return flag;
}
char substitution(char p, string c)
{
if (isupper(p))
{
p = toupper(c[(int)p - 65]);
}
else if (islower(p))
{
p = tolower(c[(int)p - 97]);
}
return p;
}