-
Notifications
You must be signed in to change notification settings - Fork 2
/
substitution.c
102 lines (91 loc) · 2.44 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
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
int check_alpha(string input_string);
int check_repeats(string input_string);
string convert_to_ciphertext(string plaintext, string cipher);
int main(int argc, string argv[])
{
// Check if key is provided, and only one argument given
if (argc != 2)
{
printf("Usage: ./substitution key\n");
return 1;
}
// Check if key has 26 characters
else if (strlen(argv[1]) != 26)
{
printf("Key must contain 26 characters.\n");
return 1;
}
// Call function to check if only alphabets are present
else if (check_alpha(argv[1]) == 0)
{
printf("Key must contain only alphabets.\n");
return 1;
}
// Call function to check for repeats in key
else if (check_repeats(argv[1]) == 0)
{
printf("Key must contain each letter exactly once.\n");
return 1;
}
else
{
string plaintext = get_string("plaintext: ");
printf("ciphertext: %s\n", convert_to_ciphertext(plaintext, argv[1]));
}
return 0;
}
int check_alpha(string input_string)
{
// Loop through string until null character
for (int i = 0 ; input_string[i] != '\0' ; i++)
{
// If current character is not alphabet
if (isalpha(input_string[i]) == 0)
{
return 0;
}
}
// If all characters in string are alphabets
return 1;
}
int check_repeats(string input_string)
{
// Loop through plaintext string until null character
for (int i = 0 ; input_string[i] != '\0' ; i++)
{
// Compare current character with the rest of the characters
for (int j = i + 1 ; input_string[j] != '\0' ; j++)
{
// If current character match one of remaining characters, return 0
if (input_string[i] == input_string[j])
{
return 0;
}
}
}
return 1;
}
string convert_to_ciphertext(string text, string cipher)
{
int asciicode;
// Loop through plaintext string until null character
for (int i = 0 ; text[i] != '\0' ; i++)
{
if (isupper(text[i]) != 0)
{
asciicode = text[i];
text[i] = toupper(cipher[asciicode - 65]);
}
else if (islower(text[i]) != 0)
{
asciicode = text[i];
text[i] = tolower(cipher[asciicode - 97]);
}
}
return text;
}