반응형
1. Readability
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
|
#import <cs50.h>
#import <stdio.h>
#import <string.h>
#import <stdlib.h>
int main(int argc, char *argv[]){
int z;
if(argc <= 1 || argc >= 3){
printf("Usage: ./caesar key\n");
return 1;
} else if((z = atoi(argv[1])) == 0 ){
printf("Usage: ./caesar key\n");
return 1;
}
z = atoi(argv[1]);
char *plaintext = get_string("plaintext: ");
for(int i = 0, n = strlen(plaintext); i < n; i++){
char letter = plaintext[i];
//this prevents converting spaces and commas
if(letter >= 'A'){
int temp = z;
//prevents inputs higher than 26
while(temp > 26){
temp -= 26;
}
//for lower case that exceeds z
if(letter + z > 'z'){
if(letter + temp > 'z') {
temp -= ('z' - letter);
plaintext[i] = 'a' - 1 + temp;
} else {
plaintext[i] += temp;
}
//for capital letters that exceeds z
} else if (letter + z > 'Z' && letter < 'a'){
if(letter + temp > 'Z') {
temp -= ('Z' - letter);
plaintext[i] = 'A' - 1 + temp;
} else {
plaintext[i] += temp;
}
//if it is normal, just convert
} else {
plaintext[i] += z;
}
}
}
//print vowalla
printf("ciphertext: %s\n", plaintext);
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
|
2. 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
|
#import <cs50.h>
#import <stdio.h>
#import <string.h>
#import <stdlib.h>
int main(int argc, char *argv[]){
int z;
if(argc <= 1 || argc >= 3){
printf("Usage: ./caesar key\n");
return 1;
} else if((z = atoi(argv[1])) == 0 ){
printf("Usage: ./caesar key\n");
return 1;
}
z = atoi(argv[1]);
char *plaintext = get_string("plaintext: ");
for(int i = 0, n = strlen(plaintext); i < n; i++){
char letter = plaintext[i];
//this prevents converting spaces and commas
if(letter >= 'A'){
int temp = z;
//prevents inputs higher than 26
while(temp > 26){
temp -= 26;
}
//for lower case that exceeds z
if(letter + z > 'z'){
if(letter + temp > 'z') {
temp -= ('z' - letter);
plaintext[i] = 'a' - 1 + temp;
} else {
plaintext[i] += temp;
}
//for capital letters that exceeds z
} else if (letter + z > 'Z' && letter < 'a'){
if(letter + temp > 'Z') {
temp -= ('Z' - letter);
plaintext[i] = 'A' - 1 + temp;
} else {
plaintext[i] += temp;
}
//if it is normal, just convert
} else {
plaintext[i] += z;
}
}
}
//print vowalla
printf("ciphertext: %s\n", plaintext);
}
|
2. Substitution
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
char *encipher(char *t, char *argv[]);
bool validate_key(char *argv[]);
int main(int argc, char *argv[]){
//Get key O
//validate key O
//check key length O
//chekc for non-alphabetic characters O
//check for repeated characters(case-insensitive) O
//get plaintext O
//encipher O
//for each alphabetic character, determine what letter it maps to O
//preserve case O
//leave non-alphabetic characters as-is O
//print ciphertext O
//validates key's length and invalid keys
if(!(argc > 1 && argc < 3)){
printf("Usage: ./substitution key\n");
return 1;
} else if(!(validate_key(argv))) return 1;
//get plaintext
char *t = get_string("plaintext: ");
//encipher & print ciphertext
printf("ciphertext: %s\n", encipher(t, argv));
}
bool validate_key(char *argv[]){
//validate key
//check key length
//check for non-alphabetic characters
//check for repeated characters(case-insensitive)
char alphabet[] = {'A','B','C','D','E','F','G','H',
'I','J','K','L','M','N','O','P',
'Q','R','S','T','U','V','W','X','Y','Z'};
//sets checkRepeat to all false;
bool checkRepeat[strlen(argv[1])];
for(int i = 0, n = strlen(argv[1]); i < n; i++){
checkRepeat[i] = false;
}
//checks for key's length
if(strlen(argv[1]) != 26){
printf("Key must contain 26 characters.\n");
return 0;
}
//checks for repeated value or incorrect value
for(int i = 0, n = strlen(argv[1]); i < n; i++){
if((argv[1][i] >= 'a' && argv[1][i] <= 'z') || (argv[1][i] >= 'A' && argv[1][i] <= 'Z')){
for(int j = 0, m = strlen(alphabet); j < m; j++){
if(argv[1][i] == alphabet[j] || argv[1][i] - ('a' - 'A') == alphabet[j]){
if(!checkRepeat[j]) {
checkRepeat[j] = true;
printf("it entered here on %i\n", j);
break;
}else if (checkRepeat[j]){
printf("Key must not contain repeated characters.\n");
return 0;
}
}
}
} else {
printf("Key must only contain alphabetic characters.\n");
return 0;
}
}
//converts all lower case to upper case
for(int i = 0, n = strlen(argv[1]); i < n; i++){
if(argv[1][i] >= 'a') argv[1][i] -= ('a' - 'A');
}
return 1;
}
char *encipher(char *t, char *argv[]){
//encipher
//for each alphabetic character, determine what letter it maps to
//preserve case
//leave non-alphabetic characters as-is
//stores inputted letter placement from the alphabet
int a = strlen(t);
int numbers[a];
int upperLowerDifference = 'a' - 'A';
//alphabet array to find out letter placement
char alphabet[] = {'A','B','C','D','E','F','G','H',
'I','J','K','L','M','N','O','P',
'Q','R','S','T','U','V','W','X','Y','Z'};
//stores letter placement into numbers array
for(int i = 0; i < a; i++){
for(int j = 0, z = strlen(alphabet); j < z; j++){
if(t[i] == alphabet[j]) {
numbers[i] = j;
break;
}else if(t[i] == alphabet[j] + upperLowerDifference){
numbers[i] = j + upperLowerDifference;
break;
}
}
}
//replaces plaintext into enciphered text
for(int i = 0; i < a; i++){
if((t[i] >= 'a' && t[i] <= 'z') || (t[i] >= 'A' && t[i] <= 'Z')){
if(numbers[i] >= upperLowerDifference ) t[i] = argv[1][numbers[i] - upperLowerDifference] + upperLowerDifference;
else t[i] = argv[1][numbers[i]];
}
}
return t;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
|
반응형
'IT > ETC' 카테고리의 다른 글
01/20/20 notes(mac java dw, java download and setup) (0) | 2020.01.20 |
---|---|
Algorithms(week3, problemset) (0) | 2020.01.06 |
C Programming Language(week1, problem set) (0) | 2019.12.30 |
Memory (week3) (0) | 2019.12.27 |
Arrays and Sorting Algorithms (week2) (0) | 2019.12.26 |