forked from chinhungtseng/cs50x2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
substitution.c
78 lines (58 loc) · 1.58 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
/**
* CS50x 2021
* Problem Set 2: substitution.c
*/
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
#include <stdbool.h>
int main(int argc, char *argv[])
{
bool is_valid_key(char key[]);
void cipher(char plaintext[], char ciphertext[], char key[]);
// Argument counts validation.
if (argc != 2) {
printf("Usage: ./substitution key\n");
return 1;
}
// Key validation.
const string key = argv[1];
if (! (strlen(key) == 26 && is_valid_key(key))) {
printf("Key must contain 26 characters.\n");
return 1;
}
// Prompt user for plaintext string.
string plaintext = get_string("plaintext: ");
char ciphertext[strlen(plaintext)];
cipher(plaintext, ciphertext, key);
printf("ciphertext: %s\n", ciphertext);
return 0;
}
void cipher(char plaintext[], char ciphertext[], char key[])
{
int i = 0;
while (plaintext[i]) {
if (isupper(plaintext[i]))
ciphertext[i] = toupper(key[plaintext[i] - 'A']);
else if (islower(plaintext[i]))
ciphertext[i] = tolower(key[plaintext[i] - 'a']);
else
ciphertext[i] = plaintext[i];
i++;
}
ciphertext[i] = '\0';
}
bool is_valid_key(char key[])
{
for (int i = 0; key[i]; i++) {
// Check key is all alphabetic character.
if (! isalpha(key[i]))
return false;
// Check key don't contain duplicated character.
for (int j = i + 1; key[j]; j++)
if (key[i] == key[j])
return false;
}
return true;
}