Skip to content

Instantly share code, notes, and snippets.

@Tomi-3-0
Forked from Latishfaction/substitution.c
Last active January 9, 2023 11:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tomi-3-0/3e3f87f6a5dcf56afdc6161c4b4d534c to your computer and use it in GitHub Desktop.
Save Tomi-3-0/3e3f87f6a5dcf56afdc6161c4b4d534c to your computer and use it in GitHub Desktop.
CS50 (cs50x) pset2 Substitution Solution 2020
#include <stdio.h>
#include <cs50.h> // to get plain text
#include <string.h> // for strlen()
#include <ctype.h> // for string operation upper, lower
void Do_substitute(); // for further substitute
void alpha_arr_val(char pos, string key);
// to get the alphabet array value of each plain text element (ex:plaintext = h|alphabet array = 8 {a=0, b=1 .....z=26})
int main(int argc, string argv[])
{
if (argc == 2) // number of commands in terminal
{
if (strlen(argv[1]) == 26)
{
for (int i = 0; i < strlen(argv[1]) ; i++) // checking each element of string
{
if (! isalpha(argv[1][i])) // if contain non-alphabet
{
printf("Key must contain 26 characters.\n");
return 1;
}
for (int j = i + 1 ; j < strlen(argv[1]) ; j++) // checking to the next element of arg[i]
{
if (toupper(argv[1][j]) == toupper(argv[1][i])) // checking repeated element
{
printf("Key must not contain repeated alphabets.\n");
return 1;
}
}
}
Do_substitute(argv[1]);
}
else
{
printf("Key must contain 26 characters.\n");
return 1;
}
}
else
{
printf("Usage: ./substitution key\n");
return 1;
}
return 0;
}
void Do_substitute(string key)
{
string p = get_string("plaintext: ");
for (int i = 0; i < strlen(p); i++)
{
if (isalpha(p[i])) // checking p[i] is alphabet
{
char x = p[i];
if (islower(p[i]))
{
alpha_arr_val(tolower(x), key); // passing p[i] in lower-case
}
else
{
alpha_arr_val(toupper(x), key); // passing p[i] in UPPER-CASE
}
}
else
{
printf("ciphertext: %c", p[i]); // print the element as it is (such as space | , | special characters | ? | etc...)
}
}
printf("\n");
}
void alpha_arr_val(char pos, string key) // passing p[i] and upper/lower(alpha array)
{
string alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; // alphabet array
for (int i = 0; i < strlen(alpha); i++) // accessing each element in alpha array
{
if (islower(pos)) // p[i] is lower
{
if (pos == tolower(alpha[i]))
{
printf("%c", tolower(key[i]));
}
}
else // for UPPERCASE // p[i] is upper
{
if (pos == toupper(alpha[i]))
{
printf("%c", toupper(key[i]));
}
}
}
}
@Tomi-3-0
Copy link
Author

Tomi-3-0 commented Jan 9, 2023

Removed the check_key() function on line 6, seems redundant.
Removed printf("ciphetext: ") statement on line 60 and merged it on new line 78.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment