Created
May 15, 2020 06:36
-
-
Save waynewallace/c5aa2f8ae8b590cd7bad8bc775a78856 to your computer and use it in GitHub Desktop.
Harvard CS50 Substitution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// substitution.c | |
// Implement a program that does a substitution cipher with a user supplied key | |
// Sample Usage | |
// $ ./substitution qwerTyuIopasDfghjKlzXcVbnm | |
// Text to encrypt: Wayne is the BEST programmer in the WORLD! | |
// plaintext:Wayne is the BEST programmer in the WORLD! | |
// ciphertext:Vqnft ol zit WTLZ hkgukqddtk of zit VGKSR! | |
#include <cs50.h> | |
#include <stdio.h> | |
#include <string.h> | |
#include <ctype.h> | |
string cipherKey; | |
void printCipher(string s, string cipherKeyValue); | |
bool isAlphaOnly(string s); | |
bool hasDuplicateCharacter(string s); | |
bool isCorrectLength(string s); | |
int main(int argc, string argv[]) | |
{ | |
if (argc == 2) | |
{ | |
cipherKey = argv[1]; | |
// A valid cipherKey is 26 alpha characters, no duplicate characters, no numbers or special characters | |
if (!isCorrectLength(cipherKey)) | |
{ | |
printf("Invalid cipherKey. Must be 26 characters. Substitution cipherKey must include 26 alpha characters only and must not contain any duplicates."); | |
return 1; | |
} | |
if (!isAlphaOnly(cipherKey)) | |
{ | |
printf("Invalid cipherKey. Number found. Substitution cipherKey must include 26 alpha characters only and must not contain any duplicates."); | |
return 1; | |
} | |
if (hasDuplicateCharacter(cipherKey)) | |
{ | |
printf("Invalid cipherKey. Duplicate character found. Substitution cipherKey must include 26 alpha characters only and must not contain any duplicates."); | |
return 1; | |
} | |
} | |
else | |
{ | |
// no cipher cipherKey provided | |
printf("You must supply a substitution cipherKey\n"); | |
return 1; | |
} | |
string userText = get_string("Text to encrypt: "); | |
fputs("plaintext:", stdout); | |
puts(userText); | |
printCipher(userText, cipherKey); | |
printf("\n"); | |
return 0; | |
} | |
// Print out new value after substitution | |
void printCipher(string s, string cipherKeyValue) | |
{ | |
fputs("ciphertext:", stdout); | |
char alphabet[] = "abcdefghijklmnopqrstuvwxyz"; | |
for (int i = 0, length = strlen(s); i < length; i++) | |
{ | |
int valueIndex; | |
// check if upper or lower case A..Z | |
if ((s[i] >= 65 && s[i] <= 90) || (s[i] >= 97 && s[i] <= 122)) | |
{ | |
if ((s[i] >= 65 && s[i] <= 90)) | |
{ | |
valueIndex = s[i]; | |
putchar(toupper(cipherKeyValue[valueIndex-65])); | |
} | |
else | |
{ | |
valueIndex = s[i]; | |
putchar(tolower(cipherKeyValue[valueIndex-97])); | |
} | |
} | |
else | |
{ | |
// if not A..Z output the given character | |
putchar(s[i]); | |
} | |
} | |
return; | |
} | |
// if string is exactly 26 characters in length return true | |
bool isCorrectLength(string s) | |
{ | |
if (strlen(s) == 26) | |
{ | |
return true; | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
// if string is only a..z or A..Z return true | |
bool isAlphaOnly(string s) | |
{ | |
bool result = false; | |
for (int i = 0, length = strlen(s); i < length; i++) | |
{ | |
if (isalpha(s[i])) | |
{ | |
result = true; | |
} | |
else | |
{ | |
result = false; | |
break; | |
} | |
} | |
return result; | |
} | |
// check for duplicates in string | |
bool hasDuplicateCharacter(string s) | |
{ | |
int count = 0; | |
for (int i = 0, length = strlen(s); i < length; i++) | |
{ | |
for (int j = 0; j < length; j++) | |
{ | |
if (s[i] == s[j]) | |
{ | |
count++; | |
} | |
} | |
// if count is over 1 we found a duplicate, exit | |
if (count > 1) | |
{ | |
return true; | |
} | |
else | |
{ | |
count = 0; | |
} | |
} | |
return false; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment