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 ourselves. 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
- Return error if there are more than 2 parameters entered for the program..
- Only accept characters in the alphabet
Then if that’s all okay I need to
- Iterate through each letter of word
- Check if upper or lower
- Use key to cipher then print
How to Cipher
- Iterate one by one through key
- Turn letter into ASCII value
- Check if upper or lower case
- 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.
- Iterate through key for full length of word to change each letter of key
- Use each of the key to cipher each word
- 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
- 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
- Grab string to cipher from get_string
- Iterate through each letter of words to cipher
- Check if upper or lower case
- Take the value of the letter take lower value of ‘A’ or ‘a’ depending on case
- Add on the key
Things to ‘Check’
-
- Return error if there are more than 2 parameters entered for the program..
- 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
- Iterate through each letter of word
- Check if upper or lower
- Use key to cipher then print converted word
- 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
}
Spooky walkcycle by Emanuele Colombo for Antimatter