Episode 3: Give To Caesar What Is Caesar's

Episode 3: Give To Caesar What Is Caesar's

CS50 Week 2: Arrays

Β·

3 min read

char *Caesar[n];

Caesar[0] = "A Dad Joke";

Arrays have an array of useful applications. 😁 (smirk of satisfaction)

Caesar[1] = "Some Coding Wisdom";

There's a concept in the world of programming called 'Rubber Duck Debugging'. This is where after running into problems with your code, you literally talk to a rubber duck explaining to it what your code is supposed to do. More often than not, articulating the problem verbally helps you find a solution. A friend or colleague might as well suffice in the absence of ducks but you get the point, right? I think it's a useful technique everywhere, not just in coding.

Albeit a different context, in his book '12 Rules For Life' Jordan Peterson says something that would validate this principle:

Thinking is listening to yourself. It’s difficult. To think, you have to be at least two people at the same time. Then you have to let those people disagree.

Caesar[2] = "Problem Set - Readability";

How do you tell if a book is best suited for a reader in grade 1, grade 6 or high school level? It might be intuition for most of us, but there are actual readability tests which have been developed such as the Coleman-Liau index which is based on the average number of letters per 100 words(L) and the average number of sentences per 100 words(S).

To help you practice working with arrays, this problem simply expects you to loop through a text, counting the number of letters, words and sentences. You then get the corresponding values of L & S and feed those values into the Coleman-Liau formula to obtain your index.

index = 0.0588 * L - 0.296 * S - 15.8

What this exercise tells you is that a word (a string) can essentially be defined as an array of letters (characters).

Caesar[3] = "Problem Set - Caesar's Cipher";

In cryptography there's a famous encryption technique named after Julius Caesar because he apparently used it a lot. It works by shifting every letter in a word by a specific number of spaces in the alphabet. So if the plain text is [Will Smith slaps Chris Rock] and the key is [1], the cipher text which would get to the recipient is [Xjmm Tnjui tmbqt Disjt Spdl]. Pretty straightforward but the security and accuracy entirely depends on the recipient having the correct key by which to decipher the text.

Anyway, this is a lesson on the use of arrays and cryptography just happens to be one of its applications. In this coding problem, you need to loop through a text (again, an array of characters), rotate each letter by a key of the user's choosing and return the correct cipher text.

Here we get another chance to appreciate the usefulness of the modulo operator (%) that we talked about earlier in Episode 2 of this CS50 series. The modulo operator returns the remainder of a division (E.g. 27 % 26 = 1). How do you ensure that the program wraps around the alphabet correctly so that with a key of 1, [Z] becomes [A]? Yes, you're right; you call in the big guns!

ciphertext = (plaintext + key) % 26

Caesar[4] = "The End";

So what exactly are we giving to Caesar? Credit for the cipher perhaps? (Though it wouldn't be fitting since he only used the cipher not invent it) Let's just say that the title of this article is not a cipher (disappointingly); it simply has a nice ring to it.

Β