-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode2.c
More file actions
85 lines (79 loc) · 2.08 KB
/
Copy pathcode2.c
File metadata and controls
85 lines (79 loc) · 2.08 KB
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
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <cs50.h>
#define STRLEN_ALLOCATED_IN_SWAP 8192
char substitute_char(char c, string k);
string perform_substitution(string s, string k);
int main(int argc, string argv[])
{
if (argc != 2)
{
printf("Usage: ./substitution <key>\n");
return 1;
}
if (strlen(argv[1]) != 26)
{
printf("Key must contain 26 characters\n");
return 1;
}
string key = argv[1];
char letters_used_in_key[26] = " ";
for (int i = 0, n = strlen(key); i < n; i++)
{
char this_char = key[i];
if (!isalpha(this_char))
{
printf("Key must be comprised of letters!\n");
return 1;
}
if (strchr(letters_used_in_key, this_char) != NULL)
{
printf("Only one of each letter is allowed! (You used %c twice!)\n", this_char);
return 1;
}
letters_used_in_key[i] = this_char;
}
string plaintext = get_string("plaintext: ");
if (strlen(plaintext) > STRLEN_ALLOCATED_IN_SWAP)
{
printf("Your plaintext is too long!\n");
return 1;
}
char *result = perform_substitution(plaintext, key);
printf("ciphertext: ");
for (int i = 0, n = strlen(result); i < n; i++)
{
printf("%c", result[i]);
}
printf("\n");
return 0;
}
char substitute_char(char c, string k)
{
if (isupper(c))
{
return toupper(k[c - 'A']);
}
else
{
return tolower(k[c - 'a']);
}
}
string perform_substitution(string s, string k)
{
char *output = (char *)malloc(sizeof(char) * STRLEN_ALLOCATED_IN_SWAP);
output[0] = '\0';
for (int i = 0, n = strlen(s); i < n; i++)
{
char this_char = s[i];
char to_append = this_char;
if (isalpha(this_char))
{
to_append = substitute_char(this_char, k);
}
strncat(output, &to_append, 1);
}
return output;
}