CS50x Substitution

From problem set 2

Photo by Raphael Schaller on Unsplash

This time we’re preparing to code a substitution cipher. Instead of getting a number for key, we’ll be getting a string. A 26 character long string to be more exact, where each character will replace it’s equivalent index on the alphabet.

“A key, for example, might be the string NQXPOMAFTRHLZGECYJIUWSKDVB. This 26-character key means that A (the first letter of the alphabet) should be converted into N (the first character of the key), B (the second letter of the alphabet) should be converted into Q (the second character of the key), and so forth.”

$ ./substitution YTNSHKVEFXRBAUQZCLWDMIPGJO
plaintext: HELLO
ciphertext: EHBBQ

Acquiring and validating the key should follow basically the same steps we saw in Caesar, with some tests needing a little more thinking through. Encrypting should be as easy or easier.

Some instructions before proceeding include that we should only modify alphabetical characters and that if the case is lower or upper doesn’t matter for the key, but the text must remain at the same case it was given. Any time you use return to terminate it must return 1. Remember, the key must be 26 characters long with no repetitions and alphabetical only.

// Validate the key
// Check argc for 2 and argv[1] for len and non-alpha chars or repetitions
// Ask user for plain text and store it
// Replace each char in text with the char with same index in key. Keep case and non-alpha

Seems simple enough, the core work here is making the code check check and check for every little thing. After some thinking, base code ended up like this:

// Ciphers a text using substitution cipher#include <stdio.h>
#include <cs50.h>
#include <ctype.h>
#include <string.h>
// Prototype functions
void subs(char *, char*);
int main(int argc, char *argv[])
{
//validate key here

string text = get_string("Enter Text: ");
subs(text, argv[1]);
}
void subs(char *t, char *k)
{

}

As I did in Caesar, I’m checking for argc and argv inside main function. I included ctype and string along with the other usual libraries. We have the proto for our only custom function and the get plaintext part is pretty much done. So we proceed to validate the key.

int main(int argc, char *argv[])
{
//validate key here
if (argc != 2)
{
printf("Usage: ./substitution key\n");
return 1;
}
if (strlen(argv [1]) != 26)
{
printf("Key must contain 26 characters.\n");
return 1;
}
for (int i = 0; i < strlen(argv[1]); i++)
{
if (isalpha(argv[1][i]) == 0)
{
printf("Key must only contain alphabetic characters. \n");
return 1;
}
for (int r = i + 1; r < strlen(argv[1]); r++)
{
if (toupper(argv[1][r]) == toupper(argv[1][i]))
{
printf("Key must only contain one of each alphabetic character. \n");
return 1;
}
}
}

First the code checks if argc is correct with 2 parameters (./substitution key) if not we explain the error and exit. Ok on that, we check for the key lenght, again if it’s not 26 we explain and exit. Now we need to check if every character inside the key is a alphabetic character, for this a loop iterates through the string checking with isalpha every character. Instructions also warned us about repetition, to check that we need a nested loop. With the nested loop we can keep the value from de first loop and compare it against every other character using value + 1 inside the nested loop. Paying attention that our key is not case sensitive we need to make sure both letter are upper or lower case, I chose upper for no particular reason, it’s easier to use toupper for both letters than to discover the case one value is using and copying it to the other. To end our key checks we return a error if we find a duplicated character, and exit.

From here we can test if our code is checking keys correctly. Grab a example from problem set and try to make it fail.

~/pset2/substitution/ $ ./substitution VcHpRzGjNtLsKfBdQwAxEuYmOi
Enter Text:
~/pset2/substitution/ $ ./substitution VcHpRzGjNtLsKfBdQwAxEuYmOiL
Key must contain 26 characters.
~/pset2/substitution/ $ ./substitution VcHpRzGjNtLsKfBdQwAxEuYmO5
Key must only contain alphabetic characters.
~/pset2/substitution/ $ ./substitution VcHpRzGjNtLsKfBdQwAxEuYmOv
Key must only contain one of each alphabetic character.
~/pset2/substitution/ $ ./substitution VcHpRzGjNtLsKfBdQwAxEuYmOi o
Usage: ./substitution key

Now that our key is working correctly for sure, we can proceed to encrypting the text. Strings t and k go for text and key respectively.

void subs(char *t, char *k)
{
printf("ciphertext: ");
for (int i = 0; i < strlen(t); i++)
{
if (isalpha(t[i]) != 0)
{
if (isupper(t[i]) != 0)
{
int index = t[i] - 'A';
printf("%c", toupper(k[index]));
}
else
{
int index = t[i] - 'a';
printf("%c", tolower(k[index]));
}
}
else
{
printf("%c", t[i]);
}
}
printf("\n");
}

The function begins printing “ciphertext”, no breaking line. Then a loop starts with value 0 to length of text. It first checks if the character in t[0] is alphabetic, then if it’s upper or lowercase. Inside the ifs checking the case, we use a formula very much like Caesars to get the alphabetical index of t[0]. Then it’s just a matter of printing that same index, but from the key string instead of text string. We do it for each single character on the text, one at a time. On the other hand, if it’s not alphabetical, we just print it as it is. To end the function and the program, just print a new line.

Staff tests and full code with comments bellow.

Results generated by style50 v2.7.4
Looks good!
Results for cs50/problems/2020/x/substitution generated by check50 v3.1.2
:) substitution.c exists
:) substitution.c compiles
:) encrypts "A" as "Z" using ZYXWVUTSRQPONMLKJIHGFEDCBA as key
:) encrypts "a" as "z" using ZYXWVUTSRQPONMLKJIHGFEDCBA as key
:) encrypts "ABC" as "NJQ" using NJQSUYBRXMOPFTHZVAWCGILKED as key
:) encrypts "XyZ" as "KeD" using NJQSUYBRXMOPFTHZVAWCGILKED as key
:) encrypts "This is CS50" as "Cbah ah KH50" using YUKFRNLBAVMWZTEOGXHCIPJSQD as key
:) encrypts "This is CS50" as "Cbah ah KH50" using yukfrnlbavmwzteogxhcipjsqd as key
:) encrypts "This is CS50" as "Cbah ah KH50" using YUKFRNLBAVMWZteogxhcipjsqd as key
:) encrypts all alphabetic characters using DWUSXNPQKEGCZFJBTLYROHIAVM as key
:) handles lack of key
:) handles invalid key length
:) handles invalid characters in key
:) handles duplicate characters in key
:) handles multiple duplicate characters in key

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store