Catching Professor David’s Life Quote | My Experience with CS50 Week 2

Yup! You got that right! While tuning in on the lecture, I suddenly did a double take of what I just watched when I thought I heard Professor David said something that resembled a life quote. Were you able to catch it too? If not, here is what he said that really caught me off guard and made me smile:

“It turns out that get string and some other functions in life don’t necessarily always return what you’re expecting.”

Anyway, week 2’s lecture for me was really helpful, and INTERESTING! Well, I bet anyone who read all the books of Dan Brown would be interested. After all, it’s about cryptography! 😊

Lecture

Here are some of the notes from the lecture that I would like to share (for newbies only!):

1. As quoted, there are times when get_string will not return the expected value. It might return some error, or value to indicate that something is wrong. The get_string function can return a value called NULL.

The following code would enable the program to not blindly get the string and use its return value, but to check first if the string is NOT EQUAL to NULL. (For non-technical readers, != is equivalent to NOT EQUAL.)

`if (s != NULL) {  put the function inside}`

2. In string0.c example, we can see that by putting i<strlen(s) as condition, we are asking the computer a question again and again: WHAT IS THE LENGTH OF THE STRING? This question gets asked repeatedly every time the loop iterates.

To counter this and not make the computer looked stupid, we can include the following code to store the answer to a variable so the computer won’t have to answer the question again and again:

`for (int i = 0, n = strlen(s); i < n; i++) {}`

3. Comments would help you remember the use of the code that you wrote. You never know, you not remember it’s use next week.

4. 32 is the gap between the upper case letters and their lower case equivalent in ascii.

5. There is a function called toupper() which capitalizes small letters. Its opposite is called tolower(). Both these functions need <ctype.h> in the header.

6. This code would tell the programmer or debugger what the exit code of the program was when it closed. This indicates the error (when it prematurely closed).

`echo \$?`

7. The last element in an array could be selected and indexed as n minus 1. (n) being the total number of arrays. This is due to C (and most computer languages) being zero-indexed. Meaning, they start counting at zero. So to reached the last item in the array, you have to subtract the total with 1.

Problem Sets

The problem sets were really fun, interesting and HARD! At least for me, at first. There were 4 challenges with the first one having 2 variations: Initials (could be less or more comfortable), Caesar (less comfortable), Vigenere (less comfortable) and Crack (more comfortable). You only have to do three. (For those who don’t know yet, the less comfortable is for coders with zero experience and knowledge. The more comfortable variation is the challenging version of it, more suitable for experienced individuals, or those with prior knowledge. You can do both if you like.)

Initials

What happened to me with Initials was really tricky. I’ll discuss more of it later.

With initials, you have to implement a program that, given a person’s name, prints a person’s initials. For example, if the user would put Juan Antonio Baltazar, the console should print JAB.

Sounds simple? It did to me at first, until I started to code it.

Basically, I needed to find a way to be able to access the first character per string of the user input.

Notes:

• Don’t put the inputted name to an array as it would only be used once.
• Put more restrictions on the condition to tell the program not to print the first char of the name if it is space..

After running it with `chec50`, I received this erorr:

`\x00\n`

I was like “WHAT”? I then searched for this kind of error and thanks to this reddit post, I understood what it meant. That is an error when the loop was not informed to not do anything if the char after the space is the NULL, signaling the end of string.

After having all green with cs50’s check, I shouted “YES”! I was so happy figuring it out that the euphoria of it took a bit more time to disappear.

Caesar

Caesar proved to be (n) times more difficult than the Initials problem. You have to implement a program that encrypts a message using a key that would be inputted in the command-line. Yayks!

To simplify this, Caesar’s algorithm encrypts a message by rotating the letter (in the original text) by the key positions. For example, if the key is 1, it means you have to increment the letter positions of each letter in the original message by 1 alphabetically. HELLO would become IFMMP. I is next to H, F is next to E so on and so forth.

Notes:

• Convert the input of the key to an integer.
• Use strlen() upon iterating through the plain message.
• The function isalpha() would be helpful in ciphering alphabets.
• Under specification, the letter casing of the plain message, whether in upper or lower case, should be inherited. The Use of isupper() with the if-else condition to control the casing of chars.
• To make it easier, the upper or lower index would then be subtracted from the char of message. The modulo operator also proved to be useful. It was tempting not to, but the lowest number must be below 26 (due to the alphabet having 26 letters). Why? What if ‘X’ is part of the message and the key given is 5. Following the equation above, it would be 88–65 + 5 = 28. The answer, 28, is beyond the alphabet. That’s why modulo operator would be used.

(For those not familiar with modulo, it is actually a way where you can get the remainder of two numbers you would divide one with another. It’s operator symbol is %.)

After that, boom! Thanks to Von Godinez for helping me with this project! `😊`

Vigenere

Vigenere is another cipher which is an improvement of Caesar. This time, the message is encrypted by a key that is a sequence of letters. This is more complicated since the key is a keyword. The number of the index of the specific letter (starting from zero being zero-indexed) would be what you have to add. For example, A is counted as 0 and Z is counted as 25. It also has to be looped if the key is shorter than the message. For example, HELLO with ABC as the key would become HFNLP. A is 0, so H would remain the same. B is 1, so F is the ciphered letter of E since F is directly next to F, so on and so forth.

Notes:

• To start, check if the key only contains letters. If it does not, meaning it contains a non-letter, the program would print an error.
• Put multiple initialization and increments of different value inside a for-loop.
• Created an equation to control the loop of the key indices and make a wrap on it. It would then end the loop when it reaches the last char of the key.

So, this is the end of my CS50 week 2 experience. Inspired? You can enroll here! It’s challenging, but it’s worth it!

I will continue posting my experiences with week 3 up as soon as I’m done with it. Hope this helps! 😊

If you wish to view the start of my CS50 journey, you can read it here:

If you like this post and it helped you in some way, please hit 👏 and share. If you want to follow me on my journey in becoming a Software Developer, feel free to follow me. 🙂

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.