cs50 Pset2: Vigenere walkthrough explained
Well, Vignere took me 3 hours to complete. It hurt me. cs50 did not give anymore hints because they were like “oh you can copy and paste the Caesar code over it’s pretty similar” FML.
Whilst I was working on this problem set my colleague who is a developer came over and was like what are you doing? He said he didn’t do Computer Science at uni and actually learnt through self-initiated projects and learnt as he went along researching things he needed. This is how I learnt HTML/CSS So I was thinking maybe I shouldn’t do a Masters? He asked me “Why are you learning this?” I said to create stuff and he asked: “But to create what?”. All very good questions which made me reflect on why I was learning this. I think I just enjoy…solving the problem sets lol. I don’t even know. When there’s an issue and you solve it the feeling after is proper good. Maybe that’s a bad thing like some sort of validation from achievements. I’m reading The Power of Now which talks a lot about getting validation from materialistic objects and sense of achievements. It talks a lot about the transience of it all. That it comes and it will go and dissintergrate into nothing. What we essentially are left with are outselves. Our skills will diminish and slowly will our bodies and things around us. SO I am like what and WHY am I trying to achieve this goal? I just… like..completing problem sets at the moment o_O it’s fun? I feel weird trying to defend my choices in life to… myself.
Anyways, let’s go over the issues I came across and how I solved this Problem set.

Things to check 

  1. Return error if there are more than 2 parameters entered for the program..
  2. Only accept characters in the alphabet

Then if that’s all okay I need to

  1. Iterate through each letter of word
  2. Check if upper or lower
  3. Use key to cipher then print

How to Cipher

  1. Iterate one by one through key
  2. Turn letter into ASCII value
  3. Check if upper or lower case
  4. Create new easier KEY by taking ‘A’ or ‘a’ depending on case from the ascii value of letters in key to see where the value is in the alphabet of values 0-25 (makes it easier to modulo) i.e KEY = 75, 69, 89; 75-65 = 10; 69 – 65 = 3, 89 – 65 = 24. So the new values are 10, 3, 24th spots in the 0-25 alphabetical order. With a = 0.
  5. Iterate through key for full length of word to change each letter of key
  6. Use each of the key to cipher each word
  7. Return back to start of key if word to cipher is longer length than key. AKA if word is HELLO WORLD and my key is KEY then I would need to cipher HELLO WORLD using KEYKE YKEYK
  8. Need to take into account non alpha units like numbers, characters and spaces and skip those but have the key continuous from where it works.

Using the key on each letter

First I need to do the same as I did for the Caesar cipher
  1. Grab string to cipher from get_string
  2. Iterate through each letter of words to cipher
  3. Check if upper or lower case
  4. Take the value of the letter take lower value of ‘A’ or ‘a’ depending on case
  5. Add on the key
Right so, take a deep breathe because as this is LONG AF. I don’t even know how I managed to resolve all the issues I had to keep going back and forth adding code in rejigging and fake “printing” everything out to see what happened. I’m still figuring out how to use the IDE debugger – not having any luck.
Let’s look at each section

Things to ‘Check’

    1. Return error if there are more than 2 parameters entered for the program..
    2. Only accept characters in the alphabet


if (argc != 2)
{
printf("Usage: ./caesar k\n");
return 1;
}
else
{
//Check that key is in the alphabet
for (int i = 0, n = strlen(argv[1]); i < n; i++)
{
if (isalpha(argv[1][i])) // check through individual chars in the key is in the alphabet
{
// doesn't do anything
}
else
{
printf("Usage: ./caesar k\n");
return 1;
}
}
}

Printing out the Ciphered word

  1. Iterate through each letter of word
  2. Check if upper or lower
  3. Use key to cipher then print converted word
  4. Print out any other characters that arent in the alphabet as they are


if (islower(s[i]))
{
print ciphered lowercase letter
}
else if (isupper(s[i]))
{
print ciphered uppercase letter
}
else
{
print character as it is
}

WORD TO CIPHER; HI = 72, 73
KEY
75, 69, 89
In order to get to the beginning a = 0. I need to minus the value of the ASCII of the ‘A’ or ‘a’ depending on the case.
A = 65
A needs to be 0
75 – 65 = 10 (this is where K is in the normal alphabet)
10 is where it is in the alphabet from the beginning 0.
Photo by Rich Tervet on Unsplash

Subscribe

Join my newsletter for the latest content

By checking this box, you confirm that you have read and are agreeing to our terms of use regarding the storage of the data submitted through this form.


Leave a Reply

Your email address will not be published. Required fields are marked *

*
*