/
substitution.c
113 lines (104 loc) · 2.15 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
string key;
int check_alpha(string);
int check_unique(string);
string convert_text(string);
int convert_upper(string);
int main(int argc, string argv[])
{
key = argv[1];
if (argc == 1)
{
printf("Usage: ./substitution key");
return 1;
}
else if (argc > 2)
{
printf("Usage: ./substitution key");
return 1;
}
else if (strlen(key) != 26)
{
printf("Key must contain 26 characters.");
return 1;
}
else if (check_alpha(key) != 26)
{
printf("Key must contain alphabetic characters.");
return 1;
}
else if(check_unique(key) > 0)
{
printf("Key must contain unique characters.");
return 1;
}
else
{
convert_upper(key);
string plaintext = get_string("Plain text: ");
printf("ciphertext: %s\n", convert_text(plaintext));
return 0;
}
}
int check_alpha(string argv)
{
int check_alpha = 0;
for (int i = 0; i < strlen(argv); i++)
{
if (isalpha(argv[i]))
{
check_alpha++;
}
}
return check_alpha;
}
int check_unique(string keyu)
{
int check_unique = 0;
for (int i = 0; i < strlen(keyu); i++)
{
for (int j = i + 1; j < strlen(keyu); j++)
{
if (toupper(keyu[i]) == toupper(keyu[j]))
{
check_unique++;
return check_unique;
}
}
}
return 0;
}
int convert_upper(string ctu)
{
int i = 0;
while (i < strlen(ctu))
{
i++;
key[i] = toupper(ctu[i]);
}
return 0;
}
string convert_text(string conv)
{
string ciphertext = conv;
for (int i = 0; i < strlen(conv); i++)
{
if (!isalpha(conv[i]))
{
ciphertext[i] = conv[i];
}
else if (isupper(conv[i]))
{
ciphertext[i] = key[(conv[i]) - 65];
}
else if (islower(conv[i]))
{
ciphertext[i] = key[(conv[i]) - 97];
ciphertext[i] = tolower(ciphertext[i]);
}
}
return ciphertext;
}