-
Notifications
You must be signed in to change notification settings - Fork 360
/
substitution.c
66 lines (59 loc) · 1.76 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
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <stdlib.h>
// This program hides a message received from the user using an alternative alphabet
// It takes a 26 digits string of unique letters and reassigns the alphabet using it as a key
int main(int argc, string argv[])
{
// validates user input, it must exist and be a single string of 26 letters
if (argc != 2 || strlen(argv[1]) != 26)
{
printf("Usage: ./substitution key\n");
return 1;
}
char newAlphabet[26];
char key[26];
// assigns the new values to the array newAlphabet and validates the key
for (int i = 0; i < 26; i++)
{
key[i] = toupper(argv[1][i]);
if (key[i] >= 65 && key[i] <= 90)
{
newAlphabet[i] = 65 + i - key[i];
}
// Returns an error if the key has non alphabetical characters
else
{
printf("Usage: ./substitution key\n");
return 1;
}
// This loop checks if the current letter of the key is a duplicate
for (int j = 0; j < i; j++)
{
if (key[i] == key[j])
{
printf("Usage: ./substitution key\n");
return 1;
}
}
}
string text = get_string("plaintext: ");
// converts the message using the new alphabet
for (int i = 0, n = strlen(text); i < n; i++)
{
if (text[i] >= 65 && text[i] <= 90)
{
text[i] = text[i] - newAlphabet[text[i] - 65];
}
else if (text[i] >= 97 && text[i] <= 122)
{
text[i] = text[i] - newAlphabet[text[i] - 97];
}
}
// prints out encrypted message
printf("ciphertext: %s\n", text);
return 0;
}